malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     long long p;     cin >> p;     long long keys[2001], people[1001];     for (int i = 0; i < n; i++) cin >> people[i];     for (int i = 0; i < k; i++) cin >> keys[i];     sort(keys, keys + k);     sort(people, people + n);     long long fin = 3e9;     for (int i = 0; i + n <= k; i++) {       long long ans = -1;       for (int j = 0; j < n; j++)         ans = max(ans, abs(people[j] - keys[i + j]) + abs(keys[i + j] - p));       fin = min(fin, ans);     }     cout << fin << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[100100], d[100100];   int main() {     cin >> n;     int mn = 1000111222, mx = 0;     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       mn = min(mn, a[i]);       mx = max(mx, a[i]);     }     for (int i = 0; i < n; i++) {       a[i] -= mn;       if (a[i] > n) {         puts( NO );         return 0;       }       d[a[i]]++;     }     mx -= mn;     d[0]--;     d[mx]--;     for (int i = 1; i <= mx - 1; i++)       if (d[i] < 2) {         puts( NO );         return 0;       } else         d[i] -= 2;     for (int i = 0; i <= mx; i++)       if (d[i]) {         if (d[i] > d[i + 1]) {           puts( NO );           return 0;         }         d[i + 1] -= d[i];       }     puts( YES );   }
#include <bits/stdc++.h>   using namespace std;   void read() { freopen( input.txt ,  r , stdin); }   bool check(string s) {     long long ans = s[0] -  0 ;     ans = ans * 10ll + (s[1] -  0 );     if (ans % 4 == 0) return true;     return false;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     string s;     cin >> s;     long long ans = 0;     for (long long i = 0; i < (long long)s.size(); i++) {       if ((s[i] -  0 ) % 4 == 0) ans++;     }     string temp =   ;     for (long long i = 0; i < (long long)s.size() - 1; i++) {       temp += s.substr(i, 2);       if (check(temp)) {         ans += i + 1;       }       temp =   ;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   inline long long quick(long long mul, long long b) {     long long c = 1;     while (b) {       if (b & 1) {         c = c * mul % mod;       }       mul = mul * mul % mod;       b /= 2;     }     return c;   }   int main() {     long long n, m;     cin >> n >> m;     cout << (quick(2 * n + 2, m) * (n - m + 1) % mod) * quick(n + 1, mod - 2) %                 mod;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   namespace FGF {   int n, m;   int read() {     int s = 0, w = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) w = -w;       ch = getchar();     }     while (isdigit(ch)) {       s = s * 10 + ch -  0 ;       ch = getchar();     }     return s * w;   }   const int N = 1e5 + 5;   const int mo = 1e9 + 7;   char s[N];   int num[60];   inline int getnum(char c) { return c >=  a  ? c -  a  + 1 : c -  A  + 27; }   long long f[N], fac[N], inv[N], g[N], h[N], ans[60][60];   void init() {     fac[0] = inv[0] = inv[1] = 1;     for (int i = 1; i <= n / 2; i++) fac[i] = fac[i - 1] * i % mo;     for (int i = 2; i <= n / 2; i++) inv[i] = inv[mo % i] * (mo - mo / i) % mo;     for (int i = 2; i <= n / 2; i++) inv[i] = inv[i - 1] * inv[i] % mo;   }   void work() {     scanf( %s , s + 1);     n = strlen(s + 1);     for (int i = 1; i <= n; i++) num[getnum(s[i])]++;     init();     f[0] = 1;     for (int i = 1; i <= 52; i++)       for (int j = n / 2; j >= num[i]; j--)         if (num[i]) f[j] = (f[j] + f[j - num[i]]) % mo;     for (int i = 1; i <= 52; i++)       if (num[i]) {         ans[i][i] = f[n / 2];         for (int j = 0; j <= n / 2; j++)           if (j >= num[i])             g[j] = (f[j] - g[j - num[i]] + mo) % mo;           else             g[j] = f[j];         for (int j = i + 1; j <= 52; j++)           if (num[j]) {             for (int k = 0; k <= n / 2; k++)               if (k >= num[j])                 h[k] = (g[k] - h[k - num[j]] + mo) % mo;               else                 h[k] = g[k];             ans[j][i] = ans[i][j] = h[n / 2] * 2 % mo;           }       }     long long res = fac[n / 2] * fac[n / 2] % mo;     for (int i = 1; i <= 52; i++) res = res * inv[num[i]] % mo;     m = read();     while (m--) {       int x = read(), y = read();       printf( %lld n , res * ans[getnum(s[x])][getnum(s[y])] % mo);     }   }   }  // namespace FGF   int main() {     FGF::work();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = (int)1e5 + 55;   const int INF = (int)1e9 + 77;   const int MOD = (int)1e9 + 7;   const double PI = 2 * acos(0.0);   const double EPS = 1e-7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     string s;     cin >> n >> m >> s;     for (int i = 0; i < m; ++i) {       int l, r;       char ch1, ch2;       cin >> l >> r >> ch1 >> ch2;       --l;       --r;       for (int j = l; j <= r; ++j) {         if (s[j] == ch1) s[j] = ch2;       }     }     cout << s <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct edge {     int t;     edge *next;   };   struct obj {     int v, a, b;     obj() {}     obj(int a0, int b0, int v0) { v = v0, a = a0, b = b0; }   };   inline bool cmpObj(obj a, obj b) { return a.v < b.v; }   const int maxn = 1010;   const int maxm = 4010;   obj o[maxm];   edge *ep, *head[maxn];   int n, m, v[maxn], vis[maxn], vo, s;   void addEdge(int u, int v) {     ep->t = v;     ep->next = head[u];     head[u] = ep++;   }   bool reach(int st, int te) {     queue<int> q;     q.push(st);     vis[st] = vo;     while (!q.empty() && vis[te] != vo) {       int p = q.front();       q.pop();       for (edge *g = head[p]; g; g = g->next)         if (vis[g->t] != vo) {           vis[g->t] = vo;           q.push(g->t);         }     }     return vis[te] == (vo++);   }   int main() {     memset(vis, -1, sizeof(vis));     vo = 0;     ep = new edge[maxm];     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , v + i);     for (int i = 0; i < m; i++) {       int a, b;       scanf( %d%d , &a, &b);       o[i << 1] = obj(a, b, v[b]);       o[(i << 1) + 1] = obj(b, a, v[a]);     }     sort(o, o + m * 2, cmpObj);     s = 0;     for (int i = 0; i < m * 2; i++)       if (!reach(o[i].b, o[i].a)) {         s += o[i].v;         addEdge(o[i].a, o[i].b);       }     printf( %d n , s);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string in;     cin >> in;     int sz = in.size();     long long max = -1;     for (int i = 1; i <= sz - 2; i++) {       for (int j = 1; j <= sz - 1 - i; j++) {         long long x, y, z;         {           stringstream ss(in.substr(0, i));           ss >> x;         }         {           stringstream ss;           ss << in.substr(i, j);           ss >> y;         }         {           stringstream ss;           ss << in.substr(i + j, sz - i - j);           ss >> z;         }         if ((in.substr(0, i)[0] ==  0  && i > 1) ||             (in.substr(i, j)[0] ==  0  && j > 1) ||             (in.substr(i + j, sz - i - j)[0] ==  0  && sz - i - j > 1))           continue;         if (x <= 1000000 && y <= 1000000 && z <= 1000000 && x + y + z > max)           max = x + y + z;       }     }     cout << max;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = (1e5) + 15;   const int mod = (1e9) + 7;   const int inf = (1e9) + 3;   long long int n, k, ans;   string s;   long long int a[maxn];   int main() {     ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     ;     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 1; i < n; i++) {       if (a[i] + a[i - 1] < k)         ans += (k - a[i] - a[i - 1]), a[i] += (k - a[i] - a[i - 1]);     }     cout << ans << endl;     for (int i = 0; i < n; i++) cout << a[i] <<    ;     cout << endl;     return 0;   }
#include<bits/stdc++.h>   using namespace std;   typedef long long ll;   #define F first   #define S second   const int MXN = 200040;   int cnt[MXN];   bool b[MXN];      ll n,d;   int main(){          int t;       cin >> t;       while(t--){                      string s;           cin >> s;           int n = s.size();           int ans = 0;           bool b[n+3];           for(int i = 0 ; i < n+3 ; i++){               b[i] = 0;           }           for(int i = 1 ; i < n ; i++){               if(i == 1){                   if(s[i] == s[i-1]){                       b[i] = 1;                       ans++;                   }                   continue;               }               if(s[i] != s[i-1] && s[i] != s[i-2])continue;               if(s[i] == s[i-1] && s[i] == s[i-2]){                   if(b[i-1] && b[i-2])continue;                   b[i] = 1;                   ans++;                   continue;               }               if(s[i] == s[i-1] && s[i] != s[i-2]){                   if(b[i-1])continue;                   b[i] = 1;                   ans++;                   continue;               }               if(s[i] == s[i-2] && s[i] != s[i-1]){                   if(b[i-2])continue;                   b[i] = 1;                   ans++;                   continue;               }           }                      cout << ans <<   n ;                  }      }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const int N = 1e6 + 5;   long long n, pw[N];   int t = 3;   void Solve() {     pw[n + 1] = 1;     for (long long i = n; i >= 3; i--) pw[i] = (pw[i + 1] * i) % mod;     long long res = 0;     res = (pw[3] * (n - 1)) % mod;     for (long long i = n - 2; i >= 1; i--) {       res = res + ((pw[t + 1] * (t - 1)) % mod * i) % mod;       t++;     }     cout << (res + 1) % mod <<   n ;   }   int main() {     cin >> n;     Solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   typedef long long ll;   typedef unsigned long long ull;   typedef unsigned int uint;   #define endl   n    #define lowbit(x) (x & -x)   #define ls(x) (x << 1)   #define rs(x) (x << 1 | 1)   #define ck(x) (x >= mod ? x - mod : x)   #define inf 0x3f3f3f3f   #define eps 1e-6   #define maxn 300200   #define maxm 10000100   #define G 3   #define invG 332748118   const int mod = 1e9 + 7;   const double PI = 4 * atan2(1.0, 1.0);   int t, n, dis1[maxn], dis2[maxn], dis3[maxn], dis4[maxn];   char s1[maxn], s2[maxn];   signed main() {       ios::sync_with_stdio(false);       cin.tie(nullptr);       cout.tie(nullptr);       cin >> t;       while (t--) {           cin >> n;           cin >> s1 + 1;           for (int i = 1; i <= n; i++) {               s2[i] = s1[i] ==  L  ?  R  :  L ;           }           for (int i = 0; i <= n; i++) {               dis1[i] = dis2[i] = dis3[i] = dis4[i] = 0;           }           dis1[0] = dis2[0] = 0;           for (int i = 1; i <= n; i++) {               if (s1[i] ==  L ) dis1[i] = dis2[i - 1] + 1;               if (s2[i] ==  L ) dis2[i] = dis1[i - 1] + 1;           }           dis3[n] = dis4[n] = 0;           for (int i = n; i >= 1; i--) {               if (s1[i] ==  R ) dis3[i - 1] = dis4[i] + 1;               if (s2[i] ==  R ) dis4[i - 1] = dis3[i] + 1;           }           for (int i = 0; i <= n; i++) {               cout << dis1[i] + dis3[i] + 1 <<    ;           }           cout << endl;       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   using namespace rel_ops;   using ll = int64_t;   using Pii = pair<int, int>;   using ull = uint64_t;   using Vi = vector<int>;   void run();   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     cout << fixed << setprecision(10);     run();     return 0;   }   int uplg(int n) { return 32 - __builtin_clz(n); }   int uplg(ll n) { return 64 - __builtin_clzll(n); }   int cmp(double a, double b, double eps = 1e-9) {     return (a > b + eps) - (a + eps < b);   }   char board[2001][2001];   int n, k;   int dp[2001][2001];   int lex[2001][2001];   bool toRight[2001][2001];   void run() {     cin >> n >> k;     for (int i = (0); i < (n); i++) cin >> board[i];     for (int i = (0); i < (n); i++)       for (int j = (0); j < (n); j++) {         if (i == 0 && j == 0) {           dp[i][j] = 0;         } else {           dp[i][j] = INT_MAX;         }         if (i > 0) {           dp[i][j] = min(dp[i][j], dp[i - 1][j]);         }         if (j > 0) {           dp[i][j] = min(dp[i][j], dp[i][j - 1]);         }         if (board[i][j] !=  a ) {           dp[i][j]++;         }       }     if (dp[n - 1][n - 1] <= k) {       for (int i = (0); i < (n * 2 - 1); i++) cout <<  a ;       cout << endl;       return;     }     lex[n][n] = 0;     Pii from{0, 0};     for (int sum = n * 2 - 1; sum >= 0; sum--) {       vector<pair<Pii, int>> pairs;       int best = INT_MAX;       Pii bestPos = {-1, -1};       for (int i = (0); i < (n); i++) {         int j = sum - i;         if (j < 0 || j >= n) continue;         int better = INT_MAX;         if (j + 1 < n) better = min(better, lex[i][j + 1]);         if (i + 1 < n) {           better = min(better, lex[i + 1][j]);           if (better == lex[i + 1][j]) toRight[i][j] = 1;         }         pairs.push_back({{board[i][j], better}, i});         if (dp[i][j] <= k) {           if (better < best) {             best = better;             bestPos = {i, j};           }         }       }       if (best < INT_MAX) {         from = bestPos;         if (toRight[bestPos.first][bestPos.second])           from.first++;         else           from.second++;         break;       }       sort((pairs).begin(), (pairs).end());       Pii last = {-1, -1};       int ind = 0;       for (auto& p : (pairs)) {         if (p.first != last) {           ind++;           last = p.first;         }         lex[p.second][sum - p.second] = ind;       }     }     for (int i = (0); i < (from.first + from.second); i++) cout <<  a ;     while (from.first != n - 1 || from.second != n - 1) {       cout << board[from.first][from.second];       if (toRight[from.first][from.second])         from.first++;       else         from.second++;     }     cout << board[n - 1][n - 1] << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n, l;   double d;   double arr[1001];   int main() {     cin >> n >> l;     for (int i = 0; i < n; i++) cin >> arr[i];     sort(arr, arr + n);     d = 0;     for (int i = 0; i < n - 1; i++) d = max(d, arr[i + 1] - arr[i]);     cout << fixed << setprecision(9) << max(d / 2, max(arr[0], l - arr[n - 1]));   }
#include <bits/stdc++.h>   using namespace std;   int mod = 1e9 + 7;   const int N = 1005;   vector<pair<int, int>> graph[N];   vector<int> ans;   int vis[N];   int v[N];   void dfs(int pos) {     vis[pos] = 1;     v[pos] = 1;     for (auto& e : graph[pos]) {       if (vis[e.first] == 0) {         if (e.second == 0) {           int c = max(ans[e.first], ans[pos]);           ans[e.first] = c;           ans[pos] = c;         } else {           if (ans[e.first] <= ans[pos]) {             ans[e.first] = ans[pos] + 1;           }         }         dfs(e.first);       }     }     vis[pos] = 0;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       int n;       cin >> n;       string str;       cin >> str;       int cnt = 1;       for (int i = 0; i < n; i++) {         ans.push_back(1);       }       for (int i = 0; i < str.length(); i++) {         if (str[i] ==  = ) {           graph[cnt].push_back({cnt - 1, 0});           graph[cnt - 1].push_back({cnt, 0});         } else if (str[i] ==  L ) {           graph[cnt].push_back({cnt - 1, 1});         } else {           graph[cnt - 1].push_back({cnt, 1});         }         cnt++;       }       for (int i = 0; i < n; i++) {         if (v[i] == 0) {           dfs(i);         }       }       for (auto& e : ans) {         cout << e <<    ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 0x3f3f3f3f3f3f3f3f;   const long long N = 5000;   long long n, m, s, t;   vector<pair<long long, long long> > nei[N + 1];   vector<long long> rv[N + 1];   void ae(long long x, long long y, long long z) {     nei[x].push_back(make_pair(y, z)), nei[y].push_back(make_pair(x, 0));     rv[x].push_back(nei[y].size() - 1), rv[y].push_back(nei[x].size() - 1);   }   long long dep[N + 1];   bool bfs() {     memset(dep, -1, sizeof(dep));     dep[s] = 0;     queue<long long> q;     q.push(s);     while (q.size()) {       long long x = q.front();       q.pop();       for (long long i = 0; i < nei[x].size(); i++) {         long long y = nei[x][i].first, c = nei[x][i].second;         if (c && !~dep[y]) dep[y] = dep[x] + 1, q.push(y);       }     }     return ~dep[t];   }   long long now[N + 1];   long long dfs(long long x = s, long long flw = inf) {     if (x == t || !flw) return flw;     long long res = 0, tmp;     for (long long &i = now[x]; i < nei[x].size(); i++) {       long long y = nei[x][i].first, &c = nei[x][i].second;       if (dep[y] == dep[x] + 1 && (tmp = dfs(y, min(flw, c)))) {         res += tmp, flw -= tmp;         c -= tmp, nei[y][rv[x][i]].second += tmp;         if (!flw) break;       }     }     return res;   }   long long dinic() {     long long res = 0;     while (bfs()) {       memset(now, 0, sizeof(now));       res += dfs();     }     return res;   }   long long a[N], b[N], c[N], d[N];   vector<long long> nums_x, nums_y;   void discrete() {     sort(nums_x.begin(), nums_x.end()), sort(nums_y.begin(), nums_y.end());     nums_x.resize(unique(nums_x.begin(), nums_x.end()) - nums_x.begin());     nums_y.resize(unique(nums_y.begin(), nums_y.end()) - nums_y.begin());     for (long long i = 1; i <= n; i++)       a[i] = lower_bound(nums_x.begin(), nums_x.end(), a[i] - 1) -              nums_x.begin() + 1,       b[i] = lower_bound(nums_y.begin(), nums_y.end(), b[i] - 1) -              nums_y.begin() + 1,       c[i] = lower_bound(nums_x.begin(), nums_x.end(), c[i]) - nums_x.begin(),       d[i] = lower_bound(nums_y.begin(), nums_y.end(), d[i]) - nums_y.begin();   }   long long row[N], col[N];   signed main() {     cin >> m >> n;     for (long long i = 1; i <= n; i++) {       cin >> a[i] >> b[i] >> c[i] >> d[i];       nums_x.push_back(a[i] - 1), nums_x.push_back(c[i]);       nums_y.push_back(b[i] - 1), nums_y.push_back(d[i]);     }     discrete();     s = 1, t = 2;     long long now = 2;     for (long long i = 1; i < nums_x.size(); i++)       row[i] = ++now, ae(s, now, nums_x[i] - nums_x[i - 1]);     for (long long i = 1; i < nums_y.size(); i++)       col[i] = ++now, ae(now, t, nums_y[i] - nums_y[i - 1]);     for (long long i = 1; i <= n; i++) {       for (long long j = a[i]; j <= c[i]; j++)         for (long long k = b[i]; k <= d[i]; k++) ae(row[j], col[k], inf);     }     cout << dinic();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string s;   int dp[50][2000], r[50], n;   const int inf = 1e6;   int abs(int a) { return a > 0 ? a : -a; }   int getTime(int id, int k) {     if (k <= 0) return 0;     if (dp[id][k]) return dp[id][k];     dp[id][k] = inf;     for (int i = 0; i < n; i++) {       if (s[i] == s[id] or r[i] <= r[id]) continue;       dp[id][k] = min(dp[id][k], abs(i - id) + getTime(i, k - r[i]));     }     return dp[id][k];   }   int solve(int st, int k) {     int ans = inf;     for (int id = 0; id < n; id++)       ans = min(ans, abs(st - id) + getTime(id, k - r[id]));     return ans == inf ? -1 : ans;   }   int main() {     int st, k;     cin >> n >> st >> k;     for (int i = 0; i < n; i++) cin >> r[i];     cin >> s;     cout << solve(st - 1, k) << endl;   }
#include <bits/stdc++.h>   using namespace std;   char s[1000500];   int ones[1000500], zeros[1000500], dp[1000500];   inline int solve(const vector<int> &vt) {     int n = vt.size();     int cnt = 0;     for (int i = 0; i < n; i++) {       if (!vt[i]) cnt++;       zeros[i] = cnt;     }     cnt = 0;     for (int i = n - 1; i >= 0; i--) {       ones[i] = cnt;       if (vt[i]) cnt++;     }     cnt = n;     for (int i = n - 1; i >= 0; i--) {       cnt = min(cnt, zeros[i] + ones[i]);       dp[i] = cnt;     }     int ret = n;     cnt = 0;     dp[n] = n;     for (int i = 0; i <= n; i++) {       ret = min(ret, dp[i] + cnt);       if (vt[i]) cnt++;       if (!vt[i]) cnt--;     }     return ret;   }   int main() {     int test;     scanf( %d , &test);     while (test--) {       int n, k;       scanf( %d %d , &n, &k);       scanf( %s , s);       int cnt = 0;       for (int i = 0; i < n; i++) {         if (s[i] ==  1 ) cnt++;       }       int ans = n;       for (int i = 0; i < k; i++) {         int tem = 0;         vector<int> vt;         for (int j = i; j < n; j += k) {           if (s[j] ==  1 ) tem++;           vt.push_back((int)s[j] -  0 );         }         ans = min(ans, solve(vt) + cnt - tem);       }       printf( %d n , ans);       ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline void intialize() {     cin.tie(0);     ios_base::sync_with_stdio(0);     cout.tie(0);   }   void clearArr(int arr[], int n);   bool arr[105][105];   bool farr[105];   queue<int> q;   queue<int> done;   int main() {     intialize();     string ans[2] = { No ,  Yes };     string s;     cin >> s;     int ss = s.length(), i;     stack<int> st;     int t = 0;     for (i = 0; i < ss; ++i) {       if (st.empty())         st.push(s[i]);       else {         if (s[i] == st.top()) {           t = (t + 1) % 2;           st.pop();         } else {           st.push(s[i]);         }       }     }     cout << ans[t] << endl;   }   void clearArr(int arr[], int n) {     for (int i = 0; i < n; ++i) arr[i] = 0;   }
#include <bits/stdc++.h>   int f[10005], c, d, n, m, k;   inline int min(int x, int y) { return x < y ? x : y; }   int main() {     scanf( %d %d %d %d %d , &c, &d, &n, &m, &k);     if (n * m <= k) {       puts( 0 );       return 0;     }     memset(f, 0x3f, sizeof(f));     f[0] = 0;     for (int i = 1; i <= n * m; ++i) {       if (i >= n) f[i] = f[i - n] + c;       f[i] = min(f[i], f[i - 1] + d);     }     int ans = 0x3f3f3f3f;     for (int i = n * m - k; i <= n * m; ++i)       if (f[i] < ans) ans = f[i];     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e6 + 1;   struct node {     int ans = 0;     int o = 0;     int c = 0;     node() : ans(0), o(0), c(0) {}     node(int x) : ans(0), o((x == 1 ? 1 : 0)), c((x == 1 ? 0 : 1)) {}   };   node t[2 * MAX];   int n;   node combine(node x, node y) {     node cur;     int t = min(x.o, y.c);     cur.ans = t + x.ans + y.ans;     cur.o = x.o + y.o - t;     cur.c = x.c + y.c - t;     return cur;   }   int query(int l, int r) {     node resl, resr;     for (l += n, r += n; l < r; l >>= 1, r >>= 1) {       if (l & 1) resl = combine(resl, t[l++]);       if (r & 1) resr = combine(t[--r], resr);     }     return combine(resl, resr).ans;   }   int main() {     string s;     cin >> s;     n = s.length();     for (int i = 0; i < n; i++) {       int ch = (s[i] ==  (  ? 1 : 0);       t[i + n] = node(ch);     }     for (int i = n - 1; i > 0; i--) {       int x = i << 1;       int y = i << 1 | 1;       if (x > y) {         t[i] = combine(t[y], t[x]);       } else {         t[i] = combine(t[x], t[y]);       }     }     int m;     cin >> m;     for (int i = 0; i < m; i++) {       int l, r;       scanf( %d%d , &l, &r);       printf( %d n , query(l - 1, r) * 2);     }     return 0;   }