malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, n, arr[26];     string s;     memset(&arr, -1, sizeof(arr));     cin >> s;     int l = s.length();     if (l < 26) {       cout << -1;       return 0;     }     i = 0;     j = 0;     while (j < l) {       if (j - i == 26) break;       if (s[j] ==  ? )         j++;       else if (arr[s[j] -  A ] != -1) {         int temp = i;         i = arr[s[j] -  A ] + 1;         for (; temp < i; temp++)           if (s[temp] !=  ? ) arr[s[temp] -  A ] = -1;       } else if (s[j] !=  ?  && arr[s[j] -  A ] == -1) {         arr[s[j] -  A ] = j;         j++;       }     }     if (j - i == 26) {       int temp = i, temp2;       for (temp2 = 0; temp2 < 26; temp2++) {         if (arr[temp2] == -1) {           while (s[temp] !=  ?  && temp < j) temp++;           s[temp] =  A  + temp2;         }       }       while (i--) {         if (s[i] ==  ? ) s[i] = s[i + 26];       }       while (j < l) {         if (s[j] ==  ? ) s[j] = s[j - 26];         j++;       }       cout << s;     } else       cout << -1;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double a, v, l, d, w, ans, L, t1, t2, v_2;   int main() {     scanf( %lf%lf%lf%lf%lf , &a, &v, &l, &d, &w);     L = l - d;     double A = w * w;     if (v <= w || A >= 2 * a * d) {       if (2 * a * l >= v * v) {         ans = v / a;         ans += (l - v * v / 2 / a) / v;       } else         ans = sqrt(2 * l / a);     } else {       double V = sqrt((2 * a * d + w * w) / 2);       if (V >= v) {         t1 = v / a + (v - w) / a;         t1 += (d - (2 * v * v - w * w) / 2 / a) / v;       } else {         t1 = V / a + (V - w) / a;       }       if (w * w + 2 * a * L <= v * v) {         v_2 = sqrt(w * w + 2 * a * L);         t2 = (v_2 - w) / a;       } else {         t2 = (v - w) / a;         t2 += (L - (v * v - w * w) / (2 * a)) / v;       }       ans = t1 + t2;     }     printf( %.12lf , ans);     return 0;   }
// shirley smokes weed   #include <bits/stdc++.h>   #define sz(v) ((int)(v).size())   #define all(v) (v).begin(), (v).end()   using namespace std;   using lint = long long;   using pi = pair<int, int>;   const int MAXN = 400;      int n, m, s[MAXN], e[MAXN], x[MAXN];   int dp[1 << 18], indep[1 << 18], w[1 << 18], tk[1 << 18];      int main(){    scanf( %d %d ,&n,&m);    for(int i=0; i<m; i++){     scanf( %d %d %d ,&s[i],&e[i],&x[i]);     s[i]--; e[i]--;    }    for(int i=1; i<(1<<n); i++){     indep[i] = 1;     for(int j=0; j<m; j++){      int b1 = (i >> s[j]) & 1;      int b2 = (i >> e[j]) & 1;      if(b1 && b2) indep[i] = 0;      if(b1 == b2) continue;      if(b1 == 1 && b2 == 0) w[i] = 1e9;      else w[i] += x[j];     }    }    dp[0] = 0;    for(int i=1; i<(1<<n); i++){     dp[i] = 1e9;     if(w[i] > 1e8) continue;     for(int j=i; j; j=(j-1)&i){      if(indep[j] && dp[i] > dp[i ^ j]){       dp[i] = dp[i ^ j];       tk[i] = j;      }     }     dp[i] += w[i];    }    vector<int> a(n);    for(int i = (1 << n) - 1; i; i ^= tk[i]){     for(int j = 0; j < n; j++){      if((i >> j) & 1) a[j]++;     }    }    for(int i=0; i<n; i++) printf( %d  , n + 1 - a[i]);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long int n;     cin >> n;     string s;     cin >> s;     long long int p = 0, q = 0, t = 0, r = 0, l = 0;     for (long long int i = 0; s[i] !=   0 ; i++) {       if (s[i] ==  1  || s[i] ==  2  || s[i] ==  3 ) {         p = 1;       }       if (s[i] ==  1  || s[i] ==  4  || s[i] ==  7 ) {         r = 1;       }       if (s[i] ==  7  || s[i] ==  0  || s[i] ==  9 ) {         q = 1;       }       if (s[i] ==  3  || s[i] ==  6  || s[i] ==  9 ) {         t = 1;       }       if (s[i] ==  0 ) {         l = 1;       }     }     if (q == 1 && p == 1 && r == 1 && t == 1) {       cout <<  YES  << endl;     } else if (l == 1 && p == 1) {       cout <<  YES  << endl;     } else {       cout <<  NO  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int N, M;   char A[2020][2020];   int B[2020][2020];   int D[2020][2020];   queue<int> qx;   queue<int> qy;   int dx[4] = {1, 0, -1, 0};   int dy[4] = {0, 1, 0, -1};   long long ans;   int main() {     scanf( %d%d , &N, &M);     for (int i = 1; i <= N; i++) {       scanf( %s , A[i] + 1);     }     ans = N * M;     for (int i = 2; i < N; i++) {       for (int j = 2; j < M; j++) {         B[i][j] = 1;         for (int d = 0; d < 4; d++) {           if (A[i][j] != A[i + dx[d]][j + dy[d]]) B[i][j] = 0;         }       }     }     for (int i = 1; i <= N; i++) {       for (int j = 1; j <= M; j++) {         if (B[i][j]) {           int c = 0;           for (int d = 0; d < 4; d++) {             c += B[i + dx[d]][j + dy[d]];           }           if (c < 4) {             D[i][j] = 1;             qx.push(i);             qy.push(j);           }         }       }     }     while (qx.size()) {       int x = qx.front();       qx.pop();       int y = qy.front();       qy.pop();       for (int i = 0; i < 4; i++) {         int nx = x + dx[i], ny = y + dy[i];         if (!B[nx][ny] || D[nx][ny]) continue;         D[nx][ny] = D[x][y] + 1;         qx.push(nx);         qy.push(ny);       }     }     for (int i = 1; i <= N; i++)       for (int j = 1; j <= M; j++) ans += D[i][j];     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > lang(105);   vector<vector<int> > emp(105);   vector<int> visited(105, false);   int n, m;   void dfs(int i) {     if (!visited[i]) {       visited[i] = true;       int n_lang = emp[i].size();       for (int k = 0; k < n_lang; k++) {         int lg = emp[i][k];         int n_emp = lang[lg].size();         for (int x = 0; x < n_emp; x++) {           dfs(lang[lg][x]);         }       }     }   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       int n_l;       cin >> n_l;       for (int j = 0; j < n_l; j++) {         int lg;         cin >> lg;         lg--;         lang[lg].push_back(i);         emp[i].push_back(lg);       }     }     bool flag = false;     for (int i = 0; i < m; i++) {       if (lang[i].size() != 0) {         flag = true;         break;       }     }     if (flag == false) {       cout << n << endl;       return 0;     }     int cc = 0;     for (int i = 0; i < n; i++) {       if (!visited[i]) {         cc++;         dfs(i);       }     }     cout << cc - 1 << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 505;   int x[N];   int n, m;   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> x[i];     double ret = 0;     while (m--) {       int u, v, c;       cin >> u >> v >> c;       u--, v--;       ret = max(1.0 * (x[u] + x[v]) / c, ret);     }     cout << fixed << setprecision(12) << ret <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[205], cnt[105], used[105], ans[205], sz[3];   int push(int x) {     if (x) {       sz[x]++;       return x;     } else if (sz[1] < sz[2]) {       sz[1]++;       return 1;     } else {       sz[2]++;       return 2;     }   }   int main() {     cin >> n;     for (int i = 1; i <= n * 2; i++) {       cin >> a[i];       cnt[a[i]]++;     }     for (int i = 1; i <= n * 2; i++)       if (cnt[a[i]] > 1 && used[a[i]] < 2) ans[i] = push(++used[a[i]]);     for (int i = 1; i <= n * 2; i++)       if (cnt[a[i]] == 1) ans[i] = push(0);     cout << sz[1] * sz[2] <<   n ;     for (int i = 1; i <= n * 2; i++) {       if (!ans[i]) ans[i] = push(0);       cout << ans[i] <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   int A[maxn][2];   bool vis[maxn];   vector<int> v[maxn];   int main() {     int T;     cin >> T;     while (T--) {       int n;       cin >> n;       for (int i = 1; i <= n; i++) {         v[i].clear();       }       for (int i = 1; i <= n; i++) {         scanf( %d , &A[i][0]);         v[A[i][0]].push_back(i);       }       for (int i = 1; i <= n; i++) {         scanf( %d , &A[i][1]);         v[A[i][1]].push_back(i);       }       int bad = 0;       for (int i = 1; i <= n; i++) {         vis[i] = 0;         if (v[i].size() != 2) {           bad = 1;           break;         }       }       if (bad) {         printf( -1 n );         continue;       }       vector<int> ans;       ans.clear();       for (int i = 1; i <= n; i++) {         if (A[i][0] == A[i][1]) {           vis[i] = 1;           continue;         }         if (vis[i]) {           continue;         }         int now = i;         vector<int> v1, v2;         int st = now;         v1.push_back(now);         while (!vis[now]) {           vis[now] = 1;           int b = A[now][1];           int nex;           if (v[b][0] == now)             nex = v[b][1];           else             nex = v[b][0];           now = nex;           if (now == st) break;           if (A[now][0] == b) {             v1.push_back(now);           } else {             v2.push_back(now);             swap(A[now][0], A[now][1]);           }         }         if (v1.size() < v2.size()) {           for (auto j : v1) {             ans.push_back(j);           }         } else {           for (auto j : v2) {             ans.push_back(j);           }         }       }       printf( %d n , ans.size());       for (auto i : ans) {         printf( %d  , i);       }       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int parent[100010];   int Rank[100010];   int cnt;   int st[100010];   int st_cnt = 0;   void rollback(int id) {     if (id < 0)       Rank[-id - 1]--;     else {       parent[id] = id;       cnt++;     }   }   int find(int cur) {     if (parent[cur] == cur) return cur;     return find(parent[cur]);   }   void Union(int x, int y, bool revert = false) {     int xroot = find(x);     int yroot = find(y);     if (xroot == yroot) return;     cnt--;     if (Rank[xroot] < Rank[yroot]) swap(xroot, yroot);     if (revert) st[st_cnt++] = yroot;     parent[yroot] = xroot;     if (Rank[yroot] == Rank[xroot]) {       Rank[xroot]++;       if (revert) st[st_cnt++] = -xroot - 1;     }   }   void init(int n) {     st_cnt = 0;     cnt = n;     for (int i = 0; i <= n; ++i) {       Rank[i] = 0;       parent[i] = i;     }   }   int x[100010], y[100010], Out[100010];   vector<int> List[600][600];   vector<int> befor[100010], after[100010];   int main() {     int n, k, m, q, i, j;     scanf( %d , &n);     scanf( %d , &k);     scanf( %d , &m);     while (m--) {       int x, y;       scanf( %d , &x);       scanf( %d , &y);       --x;       --y;       if (x < y) {         after[x].push_back(y);         befor[y].push_back(x);       } else {         after[y].push_back(x);         befor[x].push_back(y);       }     }     init(n);     scanf( %d , &q);     for (i = 0; i < q; i++) {       scanf( %d , &x[i]);       scanf( %d , &y[i]);       --x[i];       --y[i];       if (x[i] >= n) {         Out[i] = 0;         continue;       }       if (y[i] >= n) y[i] = n - 1;       int sb = x[i] / 500;       if (sb * 500 != x[i]) sb++;       int eb = y[i] / 500;       if ((eb + 1) * 500 != y[i] + 1) eb--;       if (eb < sb) {         for (j = x[i]; j <= y[i]; j++)           for (auto z : befor[j])             if (z >= x[i]) Union(j, z, true);         Out[i] = cnt;         while (st_cnt) {           rollback(st[--st_cnt]);         }       } else         List[sb][eb].push_back(i);     }     for (i = 0; i * 500 < n; i++) {       init(n);       for (j = i; (j + 1) * 500 <= n; j++) {         for (int k = j * 500; k < (j + 1) * 500; k++)           for (auto x : befor[k])             if (x >= i * 500) Union(k, x);         for (int k = 0; k < List[i][j].size(); k++) {           for (int l = x[List[i][j][k]]; l < i * 500; l++)             for (auto x : after[l])               if (x < (j + 1) * 500) Union(l, x, true);           for (int l = (j + 1) * 500; l <= y[List[i][j][k]]; l++)             for (auto z : befor[l])               if (z >= x[List[i][j][k]]) Union(l, z, true);           Out[List[i][j][k]] = cnt;           while (st_cnt) {             rollback(st[--st_cnt]);           }         }       }     }     for (i = 0; i < q; i++) printf( %d n , Out[i] - (n - (y[i] - x[i] + 1)));   }
#include <bits/stdc++.h>   using namespace std;   const long long OO = (long long)1e9 + 7;   using namespace std;   long long inv(long long a, long long b) {     return 1 < a ? b - inv(b % a, a) * b / a : 1;   }   long long Pow(long long B, long long P) {     long long R = 1;     while (P > 0) {       if (P & 1) R = (R * B);       P /= 2;       B = (B * B);     }     return R;   }   long long GCD(long long a, long long b) {     while (b) {       b ^= a ^= b ^= a %= b;     }     return a;   }   long long LCM(long long a, long long b) { return (a / GCD(a, b) * b); }   long long BigMod(long long B, long long P, long long M) {     long long R = 1;     while (P > 0) {       if (P & 1) {         R = (R * B) % M;       }       P /= 2;       B = (B * B) % M;     }     return (long long)R;   }   vector<long long> primes;   void factorization(long long n) {     for (long long i = 2; i * i <= n; ++i)       while (n % i == 0) primes.push_back(i), n /= i;     if (n > 1) primes.push_back(n);   }   int main() {     ios::sync_with_stdio(0);     long long n;     cin >> n;     factorization(n);     if (primes.size() == 2) {       cout << 2;     } else {       if (n == 1 || primes.size() == 1) {         cout <<  1 n0 ;       } else {         cout << 1 << endl;         cout << primes[0] * primes[1];       }     }     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, m, i, j, s;     char a[12][11];     scanf( %d%d , &n, &m);     for (i = 0; i < n; i++) scanf( %s , a[i]);     s = 0;     for (i = 0; i < n; i++)       for (j = 0; j < m; j++)         if (a[i][j] ==  W ) {           if (a[i - 1][j] ==  P ) {             s++;             a[i - 1][j] =  . ;             continue;           }           if (a[i + 1][j] ==  P ) {             s++;             a[i + 1][j] =  . ;             continue;           }           if (a[i][j - 1] ==  P ) {             s++;             a[i][j - 1] =  . ;             continue;           }           if (a[i][j + 1] ==  P ) {             s++;             a[i][j + 1] =  . ;             continue;           }         }     printf( %d , s);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, pair<int, int> > > V;   const int maxn = 1010;   int n, N;   int a[maxn][maxn], r[maxn], c[maxn];   void sr(int x, int y) {     if (x == y) return;     V.push_back(make_pair(1, pair<int, int>(x, y)));     for (int i = 1; i <= N; ++i) swap(a[x][i], a[y][i]);     swap(r[x], r[y]);   }   void sc(int x, int y) {     if (x == y) return;     V.push_back(make_pair(2, pair<int, int>(x, y)));     for (int i = 1; i <= N; ++i) swap(a[i][x], a[i][y]);     swap(c[x], c[y]);   }   int main() {     int x, y, i, j;     scanf( %d , &n), N = n;     for (i = 1; i < n; ++i) {       scanf( %d%d , &x, &y);       a[x][y] = 1, ++r[x], ++c[y];     }     while (n > 1) {       for (i = 1; i <= n; ++i) {         for (j = i; j <= n; ++j)           if (a[i][j]) break;         if (a[i][j]) break;       }       if (!a[i][j]) break;       sr(i, n), sc(j, 1);       for (i = 1; i <= n; ++i)         if (!c[i]) break;       sc(i, n);       for (i = 1; i <= n; ++i) c[i] -= a[n][i], r[i] -= a[i][n];       --n;     }     printf( %d n , V.size());     for (typeof(V.end()) i = V.begin(); i != V.end(); ++i)       printf( %d %d %d n , i->first, (i->second).first, (i->second).second);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mxn = 100000;   long long n, k;   int a[2][mxn], first[mxn];   vector<pair<int, int> > v;   void sol() {     cout << k <<   n ;     for (pair<int, int> i : v) cout << (i.first + 1) <<     << i.second <<   n ;     exit(0);   }   void add(int x, int y) {     if (a[0][x + 1] + y < 0 || a[0][x + 1] + y > 9) add(x + 1, -y);     if (v.size() == mxn) sol();     a[0][x] += y, a[0][x + 1] += y;     v.push_back({x, y});   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < 2; i++)       for (int j = 0; j < n; j++) {         char c;         cin >> c;         a[i][j] = c -  0 ;       }     memcpy(first, a[0], sizeof(first));     for (int i = 0; i < n - 1; i++) {       int x = a[1][i] - first[i];       k += abs(x), first[i] += x, first[i + 1] += x;     }     if (a[1][n - 1] != first[n - 1]) k = -1, sol();     for (int i = 0; i < n - 1; i++)       while (a[0][i] != a[1][i]) {         add(i, 2 * (a[0][i] < a[1][i]) - 1);       }     sol();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool dbg = 0;   int main() {     int N, K;     cin >> N >> K;     long long Count = 0;     int x, y;     for (int n = 0; n < N; ++n) {       scanf( %d%d , &x, &y);       Count += (long long)(y - x + 1);     }     cout << (K - Count % K) % K << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, s, s1, m, k, sum = 0, l, r, a, a1, p;   const int N = 1e5 + 1;   int main() {     int q;     cin >> q;     while (q--) {       cin >> l >> r;       if (l % 2) {         if (r % 2) {           p = (r / 2) - (l / 2) + 1;           s = (l + r) * p;           s /= 2;           a = l + 1;           a1 = r - 1;           p--;           s1 = (a + a1) * p;           s1 /= 2;           sum = s1 - s;           cout << sum << endl;         } else {           a1 = r - 1;           p = (r / 2) - (l / 2);           s = (l + a1) * p;           s /= 2;           a = l + 1;           s1 = (a + r) * p;           s1 /= 2;           sum = s1 - s;           cout << sum << endl;         }       } else {         if (r % 2) {           a = l + 1;           a1 = r;           p = (r / 2) + 1;           p = p - (l / 2);           s = (a + a1) * p;           s /= 2;           a1 = r - 1;           s1 = (l + a1) * p;           s1 /= 2;           sum = s1 - s;           cout << sum << endl;         } else {           p = (r / 2) - (l / 2) + 1;           s1 = (l + r) * p;           s1 /= 2;           p--;           l++;           r--;           s = (l + r) * p;           s /= 2;           sum = s1 - s;           cout << sum << endl;         }       }     }     return 0;   }
#include <bits/stdc++.h>   struct Swap {     int i;     int j;   };   std::vector<Swap> swaps;   void solve4(int a, int b, int c, int d, bool firstLast) {     if (firstLast) swaps.push_back(Swap{a, b});     swaps.push_back(Swap{a, c});     swaps.push_back(Swap{b, d});     swaps.push_back(Swap{a, d});     swaps.push_back(Swap{b, c});     if (firstLast) swaps.push_back(Swap{c, d});   }   void solve5(int a, int b, int c, int d, int e, bool firstLast) {     if (firstLast) {       swaps.push_back(Swap{a, b});       swaps.push_back(Swap{a, c});       swaps.push_back(Swap{b, c});     }     swaps.push_back(Swap{b, e});     swaps.push_back(Swap{a, d});     swaps.push_back(Swap{c, e});     swaps.push_back(Swap{c, d});     swaps.push_back(Swap{a, e});     swaps.push_back(Swap{b, d});     if (firstLast) swaps.push_back(Swap{d, e});   }   int main(void) {     int n;     scanf( %d , &n);     if (n == 1) {       printf( YES n );     } else if (n % 4 <= 1) {       if (n % 4 == 0) {         int k = n / 4;         for (int i = 0; i < k; i++) {           solve4(4 * i + 1, 4 * i + 2, 4 * i + 3, 4 * i + 4, true);         }         for (int i = 0; i + 1 < k; i++) {           for (int j = i + 1; j < k; j++) {             solve4(4 * i + 1, 4 * i + 2, 4 * j + 1, 4 * j + 2, false);             solve4(4 * i + 1, 4 * i + 2, 4 * j + 3, 4 * j + 4, false);             solve4(4 * i + 3, 4 * i + 4, 4 * j + 1, 4 * j + 2, false);             solve4(4 * i + 3, 4 * i + 4, 4 * j + 3, 4 * j + 4, false);           }         }       } else {         int k = (n - 5) / 4;         solve5(1, 2, 3, 4, 5, true);         for (int i = 0; i < k; i++) {           solve4(5 + 4 * i + 1, 5 + 4 * i + 2, 5 + 4 * i + 3, 5 + 4 * i + 4,                  true);         }         for (int i = 0; i < k; i++) {           solve5(1, 2, 3, 5 + 4 * i + 1, 5 + 4 * i + 2, false);           solve5(1, 2, 3, 5 + 4 * i + 3, 5 + 4 * i + 4, false);           solve4(4, 5, 5 + 4 * i + 1, 5 + 4 * i + 2, false);           solve4(4, 5, 5 + 4 * i + 3, 5 + 4 * i + 4, false);         }         for (int i = 0; i + 1 < k; i++) {           for (int j = i + 1; j < k; j++) {             solve4(5 + 4 * i + 1, 5 + 4 * i + 2, 5 + 4 * j + 1, 5 + 4 * j + 2,                    false);             solve4(5 + 4 * i + 1, 5 + 4 * i + 2, 5 + 4 * j + 3, 5 + 4 * j + 4,                    false);             solve4(5 + 4 * i + 3, 5 + 4 * i + 4, 5 + 4 * j + 1, 5 + 4 * j + 2,                    false);             solve4(5 + 4 * i + 3, 5 + 4 * i + 4, 5 + 4 * j + 3, 5 + 4 * j + 4,                    false);           }         }       }       printf( YES n );       for (Swap swap : swaps) printf( %d %d n , swap.i, swap.j);       printf(  n );     } else {       printf( NO n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, k, res, xx, yy;   int main() {     scanf( %d%d%d , &n, &m, &k);     while (k--) {       scanf( %d%d , &xx, &yy);       int mn = xx - 1;       mn = min(mn, n - xx);       mn = min(mn, yy - 1);       mn = min(mn, m - yy);       res = max(res, (int)(mn <= 4));     }     if (res)       printf( YES );     else       printf( NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, ap[3009], dp[3009], how[3009];   char sir[3009];   bool cons(char c) {     if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ) return 0;     return 1;   }   int main() {     gets(sir + 1), N = strlen(sir + 1);     dp[0] = 0;     for (int i = 1; i <= N; i++) {       char mini = sir[i], maxi = sir[i];       int L = cons(sir[i]);       dp[i] = dp[i - 1] + 1, how[i] = i;       for (int j = i - 1; j >= 1; j--) {         if (cons(sir[j]) == 0)           L = 0;         else {           if (L == 0)             mini = maxi = sir[j], L = 1;           else             L++, mini = min(mini, sir[j]), maxi = max(maxi, sir[j]);         }         if (L >= 3 && mini < maxi) break;         if (dp[j - 1] + 1 < dp[i]) dp[i] = dp[j - 1] + 1, how[i] = j;       }     }     int pos = N;     while (pos > 0) pos = how[pos] - 1, ap[pos] = 1;     for (int i = 1; i <= N; i++) {       printf( %c , sir[i]);       if (ap[i]) printf(   );     }     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char s[1000002];   int n, m, dp[3][22][1000002] = {0}, ml[100005], mr[100005];   int main(int argc, char **argv) {     std::ios::sync_with_stdio(false);     cin >> (s + 1);     cin >> m;     for (int i = 1; i <= m; i++) cin >> ml[i] >> mr[i];     n = strlen(s + 1);     for (int i = 0; i < 22; i++) {       for (int j = 1; j + (1 << i) - 1 <= n; j++) {         if (i == 0) {           if (s[j] ==  ) ) dp[0][0][j] = 1;           if (s[j] ==  ( ) dp[1][0][j] = 1;         } else {           if (dp[1][i - 1][j] <= dp[0][i - 1][j + (1 << (i - 1))]) {             dp[0][i][j] = dp[0][i - 1][j] + dp[0][i - 1][j + (1 << (i - 1))] -                           dp[1][i - 1][j];             dp[1][i][j] = dp[1][i - 1][j + (1 << (i - 1))];           } else {             dp[0][i][j] = dp[0][i - 1][j];             dp[1][i][j] = dp[1][i - 1][j + (1 << (i - 1))] + dp[1][i - 1][j] -                           dp[0][i - 1][j + (1 << (i - 1))];           }           dp[2][i][j] = dp[2][i - 1][j] + dp[2][i - 1][j + (1 << (i - 1))];           dp[2][i][j] += min(dp[1][i - 1][j], dp[0][i - 1][j + (1 << (i - 1))]);         }       }     }     for (int i = 1; i <= m; i++) {       int l = ml[i], lb = 0, rb = 0, r = mr[i], cnt = 0, len = r - l + 1;       for (int i = 0; i <= 22; i++) {         if (len & (1 << i)) {           r -= (1 << i);           cnt += dp[2][i][r + 1];           if (i == 0) {             lb = dp[0][i][r + 1];             rb = dp[1][i][r + 1];           } else {             cnt += min(dp[1][i][r + 1], lb);             if (dp[1][i][r + 1] > lb) {               rb += dp[1][i][r + 1] - lb;               lb = dp[0][i][r + 1];             } else               lb = dp[0][i][r + 1] + lb - dp[1][i][r + 1];           }         }       }       cout << cnt * 2 << endl;     }     return 0;   }