malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int inf = 1000000007;   int d[210][210][510][2];   short vis[210][210][510][2];   int q, n, k, b, cas;   int nex[210][22], out[210];   int fal[210];   int s[210];   int dp(int pos, int len, int w, int m) {     w = min(w, k + 5);     if (vis[pos][len][w][m] == cas) return d[pos][len][w][m];     vis[pos][len][w][m] = cas;     if (w > k) return d[pos][len][w][m] = 0;     if (len == n) return d[pos][len][w][m] = 1;     int res = 0;     for (int i = 0; i < b; ++i) {       if (m == 0 && i > s[len]) break;       int nst = nex[pos][i], mm = m | i < s[len];       int ww = w + out[nst];       res += dp(nst, len + 1, ww, mm);       if (res >= inf) res -= inf;     }     return d[pos][len][w][m] = res;   }   int nn;   void add(int len, int w) {     int pos = 0, i = 0;     while (i < len) {       int ii = s[i++];       if (!nex[pos][ii]) nex[pos][ii] = ++nn;       pos = nex[pos][ii];     }     out[pos] += w;   }   void bfs() {     queue<int> q;     q.push(0);     while (!q.empty()) {       int x = q.front();       q.pop();       for (int i = 0; i < b; ++i) {         if (!nex[x][i])           nex[x][i] = nex[fal[x]][i];         else {           int y = nex[x][i];           if (x == 0)             fal[y] = 0;           else             fal[y] = nex[fal[x]][i];           out[y] += out[fal[y]];           q.push(y);         }       }     }   }   int aa[210], bb[210];   int lena, lenb;   int down() {     int res = 0;     for (int pos = 0, i = 0; i < lena; ++i) {       pos = nex[pos][aa[i]];       res += out[pos];     }     return res <= k;   }   int ask() {     ++cas;     long long ans = 0;     for (int i = 1; i < s[0]; ++i) ans += dp(nex[0][i], 1, out[nex[0][i]], 1);     ans += dp(nex[0][s[0]], 1, out[nex[0][s[0]]], 0);     for (int i = 1; i < n; ++i)       for (int j = 1; j < b; ++j) ans += dp(nex[0][j], i + 1, out[nex[0][j]], 1);     return ans % inf;   }   int main() {     scanf( %d %d %d , &q, &b, &k);     scanf( %d , &lena);     for (int i = 0; i < lena; ++i) scanf( %d , &aa[i]);     scanf( %d , &lenb);     for (int i = 0; i < lenb; ++i) scanf( %d , &bb[i]);     int len, w;     while (q--) {       scanf( %d , &len);       for (int i = 0; i < len; ++i) scanf( %d , &s[i]);       scanf( %d , &w);       add(len, w);     }     bfs();     cas = 1;     n = lena;     for (int i = 0; i < lena; ++i) s[i] = aa[i];     int res = ask();     cas = 2;     n = lenb;     for (int i = 0; i < lenb; ++i) s[i] = bb[i];     int ans = ask();     cout << (ans + down() + inf - res) % inf << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int MOD = (long long int)1e9 + 7;   long long int powmod(long long int a, long long int b) {     long long int res = 1;     while (b > 0) {       if (b & 1) res = (res * a) % MOD;       a = (a * a) % MOD;       b = b >> 1;     }     return res % MOD;   }   long long int num_swaps(vector<long long int> &arr, long long int k) {     long long int n = arr.size();     vector<long long int> pos(n);     for (long long int i = 0; i < n; i++) {       long long int val = (n + arr[i] + k) % n;       pos[val] = i;     }     vector<long long int> vis(n, 0);     long long int cnt_cycles = 0;     for (long long int i = 0; i < n; i++) {       if (vis[i] == 0) {         cnt_cycles++;         long long int cur_node = i;         while (1) {           cur_node = pos[cur_node];           if (vis[cur_node] == 1) {             break;           }           vis[cur_node] = 1;         }       }     }     return (n - cnt_cycles);   }   void solve(long long int testnum) {     long long int n, m;     cin >> n >> m;     vector<long long int> a(n), b(n, 0), ans;     for (long long int i = 0; i < n; i++) {       cin >> a[i];       a[i]--;     }     for (long long int i = 0; i < n; i++) {       long long int tmp = (n + i - a[i]) % n;       b[tmp]++;     }     long long int cnt = 0;     for (long long int i = 0; i < n; i++) {       if (b[i] >= n - 2 * m && num_swaps(a, i) <= m) {         ans.push_back(i);       }     }     cout << ans.size() <<    ;     for (long long int val : ans) {       cout << val <<    ;     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t = 1;     cin >> t;     for (long long int i = 1; i <= t; i++) {       solve(i);     }   }
#include <bits/stdc++.h>   using namespace std;   struct st {     long long int num;     long long int big;   } w[200001];   bool cmp(st a, st b) { return a.big < b.big; }   int main() {     std::ios::sync_with_stdio(false);     long long int n, i;     char p;     while (cin >> n) {       queue<long long int> q;       stack<st> ss;       for (i = 0; i < n; i++) {         cin >> w[i].big;         w[i].num = i + 1;       }       long long int t = 0;       sort(w, w + n, cmp);       for (i = 0; i < 2 * n; i++) {         cin >> p;         if (p ==  0 ) {           q.push(w[t].num);           ss.push(w[t]);           t++;         }         if (p ==  1 ) {           st a;           a = ss.top();           q.push(a.num);           ss.pop();         }       }       while (!q.empty()) {         cout << q.front() <<    ;         q.pop();       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   const int INF = 1e9;   const long long mod = 1e9 + 7;   int n, a, b, v;   string s;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     cin >> t;     while (t--) {       cin >> n >> a >> b >> v;       cin >> s;       int zero = 0, one = 0;       for (auto isi : s) {         if (isi ==  1 )           one++;         else           zero++;       }       cout << min(zero * a + one * b, min(n * a + one * v, n * b + zero * v))            <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10, logn = 20, inf = 2e5 + 10;   int L[maxn * logn], R[maxn * logn], sum[maxn * logn];   int root[maxn];   int sz = 1, rt = 1;   int copy(int v, int &u) {     L[sz] = L[v];     R[sz] = R[v];     sum[sz] = sum[v];     return u = sz++;   }   void add(int x, int v = root[rt - 1], int l = 0, int r = +inf) {     if (r - l == 1) {       sum[v]++;       return;     }     int m = (l + r) / 2;     if (x < m)       add(x, copy(L[v], L[v]), l, m);     else       add(x, copy(R[v], R[v]), m, r);     sum[v] = sum[L[v]] + sum[R[v]];   }   void push(int x) {     copy(root[rt - 1], root[rt]);     rt++;     add(x);   }   int get(int a, int b, int v, int l = 0, int r = +inf) {     if (a <= l && r <= b) return sum[v];     if (r <= a || b <= l) return 0;     int m = (l + r) / 2;     return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);   }   int cnt(int l, int r, int x) {     return get(0, x, root[r - 1]) - get(0, x, root[l - 1]);   }   int x[maxn], y[maxn];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &x[i]), y[i] = x[i];     sort(y, y + n);     for (int i = 0; i < n; i++) {       x[i] = lower_bound(y, y + n, x[i]) - y;       push(x[i]);     }     int ans[n];     memset(ans, 0, sizeof(ans));     for (int k = 1; k < n; k++)       for (int i = 1; k * (i - 1) + 2 <= n; i++)         ans[k - 1] += cnt(k * (i - 1) + 2, min(n + 1, k * i + 2), x[i - 1]);     for (int i = 0; i < n - 1; i++) printf( %d  , ans[i]);   }
#include <bits/stdc++.h>   using namespace std;   long long num[100005], sorted[100005];   long long prenum[100005], presorted[100005];   int main() {     int n, q, l, r, op;     cin >> n;     for (__typeof(n) i = 0; i < n; i++) {       cin >> num[i];       sorted[i] = num[i];     }     sort(sorted, sorted + n);     prenum[0] = 0;     presorted[0] = 0;     for (__typeof(n + 1) i = 1; i < n + 1; i++) {       prenum[i] = prenum[i - 1] + num[i - 1];       presorted[i] = presorted[i - 1] + sorted[i - 1];     }     cin >> q;     for (__typeof(q) i = 0; i < q; i++) {       cin >> op >> l >> r;       switch (op) {         case 1:           cout << prenum[r] - prenum[l - 1] << endl;           break;         case 2:           cout << presorted[r] - presorted[l - 1] << endl;           break;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int root;   void addEdge(vector<int> adj[], int u, int v) {     if (u == -1) {       root = v;       return;     }     adj[u].push_back(v);   }   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     vector<int> res;     int n, ctr, c;     cin >> n;     vector<int> adj[n + 1];     vector<bool> vis(n + 1, false);     for (int i = 0; i < n; i++) {       cin >> ctr >> c;       addEdge(adj, ctr, i + 1);       if (c) vis[i + 1] = true;     }     for (int i = 1; i <= n; i++) {       if (!vis[i]) continue;       auto it = adj[i].begin();       for (; it != adj[i].end(); it++) {         if (!vis[*it]) break;       }       if (it == adj[i].end()) res.push_back(i);     }     if (res.size() == 0)       cout << -1;     else       for (int x : res) cout << x <<    ;   }
#include <bits/stdc++.h>   constexpr double EPS = 1e-9;   using namespace std;   int sz = 0;   int n, m;   vector<vector<pair<int, int>>> ss;   bool vis[51][51];   string grid[51];   void dfs(int r, int c) {     vis[r][c] = 1;     ss[sz].push_back({r, c});     for (int i = 0; i < n; i++) {       if (grid[i][c] ==  #  && !vis[i][c]) dfs(i, c);     }     for (int i = 0; i < m; i++) {       if (grid[r][i] ==  #  && !vis[r][i]) dfs(r, i);     }   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> grid[i];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (grid[i][j] ==  #  && !vis[i][j]) {           ss.push_back(vector<pair<int, int>>());           dfs(i, j);           sz++;         }       }     }     for (auto el : ss) {       int rr[51] = {0};       int cc[51] = {0};       for (auto qq : el) {         rr[qq.first] = 1;         cc[qq.second] = 1;       }       int a, b;       a = b = 0;       for (int i = 0; i < 50; i++) {         a += rr[i];         b += cc[i];       }       if (a * b != el.size()) {         cout <<  No ;         return 0;       }     }     cout <<  Yes ;   }
#include <bits/stdc++.h>   using namespace std;   int n;   set<int> S[100001];   int main() {     scanf( %d , &n);     set<int> s;     int x, k;     for (int i = 0; i < n; ++i) {       scanf( %d%d , &x, &k);       if (x && S[k].find(x - 1) == S[k].end()) {         puts( NO );         return 0;       }       S[k].insert(x);     }     puts( YES );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5, inf = 1e9 + 7;   long long n, k, i, j;   long long C[N], f[3][N];   pair<long long, long long> A[N];   inline long long util(long long a, long long b, long long c) {     return f[c][a] - f[c][b];   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long i, j, a, b, ans = 0, m, temp, c, d, e, ff, cur = 0;     cin >> n >> k;     for (i = 0; i < k; i++) {       cin >> A[i].first >> A[i].second;       C[A[i].first]++;       C[A[i].second + 1]--;     }     sort(A, A + k);     for (i = 1; i < n + 2; i++) C[i] += C[i - 1];     for (i = 1; i < n + 1; i++)       if (C[i]) cur++;     for (i = 1; i < n + 1; i++)       for (j = 1; j < 3; j++)         if (C[i] == j) f[j][i]++;     for (i = 1; i < n + 1; i++)       for (j = 1; j < 3; j++) f[j][i] += f[j][i - 1];     temp = 0;     for (i = 0; i < k; i++)       for (j = i + 1; j < k; j++) {         if (A[i].second < A[j].first) {           temp = cur - util(A[i].second, A[i].first - 1, 1) -                  util(A[j].second, A[j].first - 1, 1);         } else if (A[i].second <= A[j].second) {           a = A[i].first - 1;           b = A[j].first - 1;           c = A[j].first - 1;           d = A[i].second;           e = A[i].second;           ff = A[j].second;           temp = cur - util(b, a, 1) - util(d, c, 2) - util(ff, e, 1);         } else {           a = A[i].first - 1;           b = A[j].first - 1;           c = A[j].first - 1;           d = A[j].second;           e = A[j].second;           ff = A[i].second;           temp = cur - util(b, a, 1) - util(d, c, 2) - util(ff, e, 1);         }         ans = max(ans, temp);       }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)1e5 + 1;   int n, rt, tot, sz[maxn], bel[maxn], in[maxn], out[maxn], seg[maxn << 1 | 1];   vector<pair<int, int> > e[maxn];   set<int> sp[maxn];   long long ans;   void dfs(int u, long long dis, int ori) {     sz[u] = 1;     ans += dis;     bel[u] = ori;     in[u] = ++tot;     for (auto it : e[u]) {       int v = it.first;       int w = it.second;       if (!in[v]) {         dfs(v, dis + w, ori);         sz[u] += sz[v];       }     }     out[u] = tot;   }   inline int seg_idx(int L, int R) { return (L + R) | (L < R); }   void seg_upd(int L, int R, int x, int v) {     int rt = seg_idx(L, R);     if (L == R) {       seg[rt] = v;     } else {       int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R);       if (x <= M) {         seg_upd(L, M, x, v);       } else {         seg_upd(M + 1, R, x, v);       }       seg[rt] = min(seg[lch], seg[rch]);     }   }   int seg_que(int L, int R, int l, int r) {     if (l <= L && R <= r) return seg[seg_idx(L, R)];     int M = (L + R) >> 1, ret = n + 1;     if (l <= M) ret = min(ret, seg_que(L, M, l, r));     if (M < r) ret = min(ret, seg_que(M + 1, R, l, r));     return ret;   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; ++i) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       e[u].push_back(make_pair(v, w));       e[v].push_back(make_pair(u, w));     }     int *que = bel;     tot = 0;     memset(in + 1, 0, n * sizeof(int));     que[++tot] = 1;     in[1] = tot;     for (int i = 1; i <= n; ++i) {       int u = que[i];       for (auto it : e[u]) {         int v = it.first;         if (!in[v]) {           que[++tot] = v;           in[v] = tot;         }       }     }     for (int i = n; i >= 1; --i) {       int u = que[i], mx = 0;       sz[u] = 1;       for (auto it : e[u]) {         int v = it.first;         if (in[v] > in[u]) {           sz[u] += sz[v];           mx = max(mx, sz[v]);         }       }       if (max(mx, n - sz[u]) << 1 <= n) rt = u;     }     tot = ans = 0;     memset(in + 1, 0, n * sizeof(int));     in[rt] = ++tot;     bel[rt] = rt;     for (auto it : e[rt]) {       int v = it.first;       int w = it.second;       dfs(v, w, v);       sp[sz[v] <<= 1].insert(v);     }     out[rt] = tot;     printf( %lld n , ans <<= 1);     for (int i = 1; i <= n; ++i) seg_upd(1, n, in[i], i);     for (int i = 1; i <= n; ++i) {       int p = 0;       for (auto u : sp[n - i + 1])         if (u != bel[i]) p = u;       if (!p && i == rt) p = rt;       if (p) {         p = seg_que(1, n, in[p], out[p]);       } else {         p = n + 1;         if (in[bel[i]] > 1) p = min(p, seg_que(1, n, 1, in[bel[i]] - 1));         if (out[bel[i]] < n) p = min(p, seg_que(1, n, out[bel[i]] + 1, n));       }       printf( %d%c , p,    n [i == n]);       seg_upd(1, n, in[p], n + 1);       if (i != rt) {         sp[sz[bel[i]]].erase(bel[i]);         sp[--sz[bel[i]]].insert(bel[i]);       }       if (p != rt) {         sp[sz[bel[p]]].erase(bel[p]);         sp[--sz[bel[p]]].insert(bel[p]);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<long long, int> cnt;   int lp[100005];   long long p[100005][102];   void sieve() {     for (int i = 2; i <= 100000; i++) {       if (!lp[i]) {         lp[i] = i;         for (int j = i + i; j <= 100000; j += i)           if (!lp[j]) lp[j] = i;       }     }   }   int main() {     int n, k, a;     long long ans = 0;     sieve();     for (int i = 1; i <= 100000; i++) {       p[i][0] = 1;       for (int j = 1; j <= 100; j++) {         p[i][j] = p[i][j - 1] * i;         if (p[i][j] > 1e9) p[i][j] = 0;       }     }     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %d , &a);       long long cur = 1;       long long mate = 1;       while (a > 1) {         int cnt = 0;         int prime = lp[a];         while (a % prime == 0) {           a /= prime;           cnt++;         }         cnt %= k;         cur *= p[prime][cnt];         mate *= p[prime][(k - cnt) % k];         if (mate > 1e9) mate = 0;       }       if (mate != 0 && cnt.count(mate)) ans += cnt[mate];       cnt[cur]++;     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int n, k, num = 0;     cin >> n >> k;     vector<pair<int, int> > a(n);     for (int i = 0; i < n; i++) {       cin >> a[i].first >> a[i].second;       a[i].first = -a[i].first;     }     (sort(a.begin(), a.end()));     int fi = a[k - 1].first, sec = a[k - 1].second;     for (int i = 0; i < n; i++) {       if (fi == a[i].first && sec == a[i].second) {         num++;       }     }     cout << num;   }
#include <bits/stdc++.h>   long long gcd(long long A, long long B) {     return A > B ? gcd(B, A) : A ? gcd(B % A, A) : B;   }   long long lcm(long long A, long long B) { return A / gcd(A, B) * B; }   long long pow(long long A, long long B, long long P) {     if (!B) return 1 % P;     if (B == 1) return A % P;     long long h = pow(A, B / 2, P);     h *= h;     h %= P;     if (B % 2) h *= A;     return h % P;   }   using namespace std;   int n, m, q, i, j, I, J;   int a[41][41];   int sum[41][41];   int res[41][41][41][41];   int all[41][41][41][41];   char ch;   int get(int i, int j, int I, int J) {     return sum[I][J] - sum[i - 1][J] - sum[I][j - 1] + sum[i - 1][j - 1];   }   bool f(int k) {     if (k == 0)       return true;     else       return false;   }   int main() {     cin >> n >> m >> q;     for (i = 1; i <= n; i++)       for (j = 1; j <= m; j++) {         cin >> ch;         a[i][j] = ch -  0 ;         sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];       }     for (i = n; i >= 1; i--)       for (j = m; j >= 1; j--)         if (!a[i][j]) {           all[i][j][i][j] = 1;           for (I = i - 1; I >= 1; I--)             all[I][j][i][j] = all[I + 1][j][i][j] + f(get(I, j, i, j));           for (J = j - 1; J >= 1; J--)             all[i][J][i][j] = all[i][J + 1][i][j] + f(get(i, J, i, j));           for (I = i - 1; I >= 1; I--)             for (J = j - 1; J >= 1; J--)               all[I][J][i][j] = all[I][J + 1][i][j] + all[I + 1][J][i][j] -                                 all[I + 1][J + 1][i][j] + f(get(I, J, i, j));         }     for (i = 1; i <= n; i++)       for (j = 1; j <= m; j++) {         if (!a[i][j]) res[i][j][i][j] = 1;         for (I = i + 1; I <= n; I++)           res[i][j][I][j] = res[i][j][I - 1][j] + all[i][j][I][j];         for (J = j + 1; J <= m; J++)           res[i][j][i][J] = res[i][j][i][J - 1] + all[i][j][i][J];         for (I = i + 1; I <= n; I++)           for (J = j + 1; J <= m; J++)             res[i][j][I][J] = res[i][j][I][J - 1] + res[i][j][I - 1][J] -                               res[i][j][I - 1][J - 1] + all[i][j][I][J];       }     while (q--) {       cin >> i >> j >> I >> J;       cout << res[i][j][I][J] << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int a[1000001];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     int m;     cin >> m;     for (int i = 1; i <= m; i++) {       int x, y;       cin >> x >> y;       a[x + 1] += a[x] - y;       a[x - 1] += y - 1;       a[x] = 0;     }     for (int i = 1; i <= n; i++) {       cout << a[i] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5010;   int v[MAXN], n, k;   int vec[1000100][12];   bool vis[1000100];   void crivo() {     for (int i = 1; i <= 1000001; i++) {       set<int> s;       for (int j = 0; j <= 1000000; j += i) {         for (int t = 1; t <= 11; t++) {           int at = vec[j][t];           if (at == -1) break;           s.insert(at);           if (s.size() > 2 * k) {             vis[i] = true;             break;           }         }         if (vis[i] == true) break;       }       if (vis[i] == true) continue;       int q = 0;       unordered_map<int, int> mp;       for (auto j : s) mp[v[j] % i]++;       for (auto j : mp) q += j.second - 1;       if (q > k) vis[i] = true;       if (vis[i] == false) {         printf( %d n , i);         return;       }     }   }   vector<int> vet[1000100];   int cnt[1000100];   int main() {     memset(cnt, 0, sizeof cnt);     memset(vec, -1, sizeof vec);     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &v[i]);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (i == j) continue;         int h = abs(v[i] - v[j]);         if (cnt[h] > 10) continue;         vec[h][cnt[h] + 1] = i, cnt[h]++;       }     }     crivo();   }
#include <bits/stdc++.h>   using namespace std;   int a[1505];   int main() {     ios::sync_with_stdio(false), cin.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     bool isEven = true;     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         if (a[i] > a[j]) {           isEven ^= 1;         }       }     }     int q;     cin >> q;     for (int qq = 0; qq < q; qq++) {       int l, r;       cin >> l >> r;       int len = r - l + 1;       isEven ^= ((len * (len - 1)) / 2) % 2;       cout << (isEven ?  even  :  odd ) <<   n ;     }     return 0;   }