malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int a[100001], b[100001], part1[100001], part2[100001];   int main() {     int n, m, c;     cin >> n >> m >> c;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= m; i++) cin >> b[i], part1[i] = part1[i - 1] + b[i];     for (int i = 1; i <= m; i++) part2[i] = part2[i - 1] + b[m - i + 1];     for (int i = 1; i <= n; i++) {       if (i <= m) {         a[i] = (a[i] +                 (i < n - m + 1 ? part1[i] : part1[i] - part1[i - (n - m + 1)])) %                c;       } else if (n - i + 1 <= m) {         a[i] = (a[i] + part2[n - i + 1]) % c;       } else         a[i] = (a[i] + part1[m]) % c;     }     for (int i = 1; i <= n; i++) cout << a[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     unsigned long long dp[1000000];     unsigned long long ans = 0;     for (long long i = 1; i <= 500000; i += 2) {       long long val = (i * i - (i - 2) * (i - 2)) * (i / 2);       ans = ans + val;       dp[i] = ans;     }     while (t--) {       long n;       cin >> n;       cout << dp[n] <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> w[200000 + 20];   int a[200000 + 20][3];   vector<pair<int, pair<int, int> > > to_compress;   int n;   void maximize(int &a, int b) {     if (a < b) a = b;   }   struct Fenwick_tree {     int bit[1000000 + 50];     void update(int x, int v) {       for (; x <= 1000000; x += x & (-x)) maximize(bit[x], v);     }     int get(int x) {       int res = 0;       for (; x > 0; x -= x & (-x)) maximize(res, bit[x]);       return res;     }   };   Fenwick_tree LIS;   int ans = 0;   void compress() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d %d , &w[i].first, &w[i].second);     sort(w + 1, w + 1 + n);     for (int i = 1; i <= n; i++) {       to_compress.push_back(pair<int, pair<int, int> >(w[i].first + w[i].second,                                                        pair<int, int>(i, 1)));       to_compress.push_back(pair<int, pair<int, int> >(w[i].first - w[i].second,                                                        pair<int, int>(i, 2)));     }     sort(to_compress.begin(), to_compress.end());     int curr = 0;     for (int i = 0; i < to_compress.size(); i++) {       if (i == 0 || to_compress[i].first > to_compress[i - 1].first) curr++;       a[to_compress[i].second.first][to_compress[i].second.second] = curr;     }   }   void solve() {     for (int i = 1; i <= n; i++) {       int x = LIS.get(a[i][2]) + 1;       maximize(ans, x);       LIS.update(a[i][1], x);     }     printf( %d , ans);   }   int main() {     compress();     solve();   }
#include <bits/stdc++.h>   using namespace std;   int n, k, a, b, q, d, ai, p, t, zb[200001], suma[800005], sumb[800005],       za[200001], res;   void build1(int a[], int v, int tl, int tr) {     if (tl == tr)       suma[v] = a[tl];     else {       int tm = (tl + tr) / 2;       build1(a, v * 2, tl, tm);       build1(a, v * 2 + 1, tm + 1, tr);       suma[v] = suma[v * 2] + suma[v * 2 + 1];     }   }   void build2(int a[], int v, int tl, int tr) {     if (tl == tr)       sumb[v] = a[tl];     else {       int tm = (tl + tr) / 2;       build2(a, v * 2, tl, tm);       build2(a, v * 2 + 1, tm + 1, tr);       sumb[v] = sumb[v * 2] + sumb[v * 2 + 1];     }   }   int sum1(int v, int tl, int tr, int l, int r) {     if (l > r) return 0;     if (l == tl && r == tr) return suma[v];     int tm = (tl + tr) / 2;     return sum1(v * 2, tl, tm, l, min(r, tm)) +            sum1(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);   }   int sum2(int v, int tl, int tr, int l, int r) {     if (l > r) return 0;     if (l == tl && r == tr) return sumb[v];     int tm = (tl + tr) / 2;     return sum2(v * 2, tl, tm, l, min(r, tm)) +            sum2(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);   }   void update1(int v, int tl, int tr, int pos, long long new_val) {     if (tl == tr)       suma[v] = new_val;     else {       int tm = (tl + tr) / 2;       if (pos <= tm)         update1(v * 2, tl, tm, pos, new_val);       else         update1(v * 2 + 1, tm + 1, tr, pos, new_val);       suma[v] = suma[v * 2] + suma[v * 2 + 1];     }   }   void update2(int v, int tl, int tr, int pos, long long new_val) {     if (tl == tr)       sumb[v] = new_val;     else {       int tm = (tl + tr) / 2;       if (pos <= tm)         update2(v * 2, tl, tm, pos, new_val);       else         update2(v * 2 + 1, tm + 1, tr, pos, new_val);       sumb[v] = sumb[v * 2] + sumb[v * 2 + 1];     }   }   int main() {     scanf( %d %d %d %d %d , &n, &k, &b, &a, &q);     for (int i = 0; i < n; i++) zb[i] = b;     for (int i = 0; i < n; i++) za[i] = a;     build1(za, 1, 0, n - 1);     build2(zb, 1, 0, n - 1);     for (int i = 0; i < q; i++) {       scanf( %d , &t);       if (t == 1) {         scanf( %d %d , &d, &ai);         d--;         za[d] -= ai;         zb[d] -= ai;         if (za[d] < 0) za[d] = 0;         if (zb[d] < 0) zb[d] = 0;         update1(1, 0, n - 1, d, za[d]);         update2(1, 0, n - 1, d, zb[d]);       } else {         scanf( %d , &p);         p--;         int size1 = p;         int size2 = n - k - p;         res = 0;         int resa = 0, resb = 0;         resa -= sum1(1, 0, n - 1, 0, size1 - 1);         resa += size1 * a;         resb -= sum2(1, 0, n - 1, n - size2, n - 1);         resb += size2 * b;         printf( %d n , resa + resb);       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long t;   long long n;   int x;   int main() {     ios::sync_with_stdio(0);     cin.tie(), cout.tie();     cin >> t;     while (t--) {       cin >> n;       set<int> s;       int count = 0;       for (int i = 0; i < n; i++) {         cin >> x;         if (s.find(x) == s.end()) {           count++;           s.insert(x);         }       }       cout << count << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 100005;   long long n, k, fd, gcd, sum = 0;   int main() {     cin >> n >> k;     if (k > sqrt(2 * n)) {       cout <<  -1 n ;     } else {       if ((k * (k + 1)) / 2 > n) {         cout << -1;       } else {         fd = -1;         for (long long i = (k * (k + 1)) / 2; i <= N; i++) {           if (n % i == 0) {             fd = i;             break;           }         }         if (fd == -1) {           for (long long i = N; i >= 1; i--) {             if (n % i == 0 && ((n / i) >= (k * (k + 1)) / 2)) {               gcd = i;               break;             }           }         } else           gcd = n / fd;         for (long long i = 1; i < k; i++) {           sum += gcd * i;           cout << gcd * i <<    ;         }         cout << (n - sum);       }     }   }
#include <bits/stdc++.h>   using namespace std;   string rotate(string s) {     for (int i = 1; i <= 3; i++) {       swap(s[i - 1], s[i]);     }     return s;   }   string rotate2(string s) {     swap(s[0], s[4]);     swap(s[2], s[4]);     swap(s[2], s[5]);     return s;   }   string solve(string s) {     string ans = s;     for (int i = 0; i < 2; i++) {       for (int j = 0; j < 4; j++) {         for (int k = 0; k < 4; k++) {           s = rotate(s);           ans = min(ans, s);         }         s = rotate2(s);       }       s = rotate(s);     }     return ans;   }   int main() {     string s;     cin >> s;     set<string> st;     sort(s.begin(), s.end());     do {       st.insert(solve(s));     } while (next_permutation(s.begin(), s.end()));     cout << st.size() << endl;   }
#include <bits/stdc++.h>   using namespace std;   long long n, k;   void In() { cin >> n >> k; }   void Solve() {     k--;     while (k--) {       int largest = INT_MIN, smallest = INT_MAX;       for (long long copy = n; copy; copy /= 10) {         int r = copy % 10;         largest = max(r, largest);         smallest = min(r, smallest);       }       if (largest == 0 || smallest == 0) return;       n += largest * smallest;     }   }   void Out() { cout << n <<   n ; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t = 1;     cin >> t;     while (t--) {       In();       Solve();       Out();     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m1 = 0, m2 = 0, mn1 = INT_MAX, mn2 = INT_MAX, s1 = 0, s2 = 0;     cin >> n;     vector<pair<int, int> > vt;     long long tmp1, tmp2;     for (int i = 0; i < n; i++) {       cin >> tmp1 >> tmp2;       vt.push_back(make_pair(tmp1, tmp2));     }     string ans =   ;     tmp1 = 0, tmp2 = 0;     for (int i = 0; i < n; i++) {       if (tmp2 + vt[i].second <= tmp1 + 500) {         tmp2 += vt[i].second;         ans +=  G ;       } else {         tmp1 += vt[i].first;         ans +=  A ;       }     }     if (!(abs(tmp1 - tmp2) <= 500)) {       cout << -1;       return 0;     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long func(long long x, long long y, long long z) {     return (x - y) * (x - y) + (y - z) * (y - z) + (z - x) * (z - x);   }   void solve() {     long long na, nb, nc;     cin >> na >> nb >> nc;     vector<long long> a(na), b(nb), c(nc);     long long i, j, k;     for (i = 0; i < na; i++) cin >> a[i];     for (j = 0; j < nb; j++) cin >> b[j];     for (k = 0; k < nc; k++) cin >> c[k];     sort(a.begin(), a.end());     sort(b.begin(), b.end());     sort(c.begin(), c.end());     i = j = k = 0;     long long ans, d1, d2, d3;     ans = LLONG_MAX;     while (i < na - 1 || j < nb - 1 || k < nc - 1) {       ans = min(ans, func(a[i], b[j], c[k]));       d1 = d2 = d3 = LLONG_MAX;       if (i < na - 1) d1 = func(a[i + 1], b[j], c[k]);       if (j < nb - 1) d2 = func(a[i], b[j + 1], c[k]);       if (k < nc - 1) d3 = func(a[i], b[j], c[k + 1]);       if (d1 <= d2 && d1 <= d3)         i++;       else if (d2 <= d1 && d2 <= d3)         j++;       else         k++;     }     ans = min(ans, func(a[i], b[j], c[k]));     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long t;     cin >> t;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   void _R(T &x) {     cin >> x;   }   void _R(int &x) { scanf( %d , &x); }   void _R(long long &x) { scanf( %lld , &x); }   void _R(double &x) { scanf( %lf , &x); }   void _R(char &x) { scanf(  %c , &x); }   void _R(char *x) { scanf( %s , x); }   void R() {}   template <class T, class... U>   void R(T &head, U &...tail) {     _R(head);     R(tail...);   }   template <class T>   void _W(const T &x) {     cout << x;   }   void _W(const int &x) { printf( %d , x); }   void _W(const long long &x) { printf( %lld , x); }   void _W(const double &x) { printf( %.16f , x); }   void _W(const char &x) { putchar(x); }   void _W(const char *x) { printf( %s , x); }   template <class T, class U>   void _W(const pair<T, U> &x) {     _W(x.first);     putchar(   );     _W(x.second);   }   template <class T>   void _W(const vector<T> &x) {     for (auto i = x.begin(); i != x.end(); _W(*i++))       if (i != x.cbegin()) putchar(   );   }   void W() {}   template <class T, class... U>   void W(const T &head, const U &...tail) {     _W(head);     putchar(sizeof...(tail) ?     :   n );     W(tail...);   }   int MOD = 1e9 + 7;   void ADD(long long &x, long long v) {     x = (x + v) % MOD;     if (x < 0) x += MOD;   }   const int SIZE = 1e6 + 10;   const int AA = 1000;   int dp[2001];   int main() {     long long target_W;     R(target_W);     long long sum = 0;     long long cnt[9], cnt2[9] = {};     for (int i = (1); i <= (8); ++i) {       R(cnt[i]);       sum += cnt[i] * i;     }     if (sum <= target_W)       W(sum);     else {       long long now = 0;       for (int i = (1); i <= (8); ++i) {         if (now + i * cnt[i] <= target_W) {           now += i * cnt[i];           cnt2[i] = cnt[i];           cnt[i] = 0;         } else {           long long v = (target_W - now) / i;           now += i * v;           cnt[i] -= v;           cnt2[i] += v;         }       }       dp[(0) + AA] = 1;       for (int v = (1); v <= (8); ++v) {         for (int j = 0; j < (min(100LL, cnt[v])); ++j) {           for (int k = 900; k >= -900; k--) {             if (dp[(k) + AA]) dp[(k + v) + AA] = 1;           }         }         for (int j = 0; j < (min(100LL, cnt2[v])); ++j) {           for (int k = -900; k <= 900; k++) {             if (dp[(k) + AA]) dp[(k - v) + AA] = 1;           }         }       }       long long an = 0;       for (int i = (0); i <= (target_W - now); ++i) {         if (dp[(i) + AA]) an = now + i;       }       W(an);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int const mod = 1e9 + 7;   long long int power(long long int x, long long int y, long long int p) {     long long int res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long int MIN(long long int a, long long int b) {     if (a < b) return a;     return b;   }   long long int MAX(long long int a, long long int b) {     if (a > b) return a;     return b;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     bool p[1000001];     memset(p, true, sizeof p);     p[0] = p[1] = false;     p[2] = true;     for (int i = 2; i * i <= 1000001; i++) {       if (p[i]) {         for (int j = i * i; j <= 1000001; j += i) {           p[j] = false;         }       }     }     vector<int> pp;     for (int i = 2; i <= 1000001; i++) {       if (p[i]) pp.push_back(i);     }     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       long long int a[n + 1];       for (int i = 0; i < n; i++) cin >> a[i + 1];       int dp[n + 1], ans = 1;       dp[1] = 1;       for (int i = 2; i <= n; i++) {         dp[i] = 1;         for (int j = 1; j * j <= i; j++) {           if (i % j == 0) {             if (a[i] > a[j]) dp[i] = MAX(dp[i], dp[j] + 1);             if (a[i] > a[i / j]) dp[i] = MAX(dp[i], dp[i / j] + 1);           }         }         ans = MAX(dp[i], ans);       }       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   int check_matrix(int** arr, int n, int m) {     int itN = 0, itJ = n - 1;     if (n % 2) return n;     while (itN < itJ) {       for (int i = 0; i < m; ++i)         if (arr[itN][i] != arr[itJ][i]) return n;       itN++;       itJ--;     }     return check_matrix(arr, n / 2, m);   }   int main() {     int n, m, **arr;     std::cin >> n >> m;     arr = new int*[n];     for (int i = 0; i < n; ++i) *(arr + i) = new int[m];     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j) std::cin >> arr[i][j];     std::cout << check_matrix(arr, n, m);     for (int i = 0; i < n; ++i) delete[] * (arr + i);     delete[] arr;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 201000;   struct str {     long long nr;     char c;   };   int n, m;   str a[2 * N], b[N], l[N];   vector<int> poz;   bool cmp(str a, str b) { return (a.nr == b.nr && a.c == b.c); }   int z[2 * N];   void calcpot() {     int i, j;     for (i = 1; i <= n; ++i) l[i] = a[i];     int nr = 0;     for (i = 2; i < n; ++i) {       a[++nr] = l[i];     }     for (i = 1; i <= m; ++i) a[++nr] = b[i];     int el, pmax = 0;     for (i = 2; i <= nr; ++i) {       if (i >= pmax) {         for (j = i; cmp(a[j], a[j - i + 1]) && j <= nr; ++j)           ;         --j;         z[i] = j - i + 1;         pmax = j;         el = i;       } else {         z[i] = min(z[i - el + 1], pmax - i + 1);         for (j = i + z[i] - 1; cmp(a[j], a[j - i + 1]) && j <= nr; ++j)           ;         --j;         z[i] = j - i + 1;         if (j > pmax) {           pmax = j;           el = i;         }       }       if (i > n - 2 && z[i] >= n - 2) {         poz.push_back(i - (n - 2));       }     }   }   void calc() {     int i, j;     calcpot();     int r = 0;     for (i = 0; i < poz.size(); ++i) {       int e = poz[i];       if (e > 1 && e + n - 2 <= m) {         if (l[1].c == b[e - 1].c && l[1].nr <= b[e - 1].nr)           if (l[n].c == b[e + n - 2].c && l[n].nr <= b[e + n - 2].nr) ++r;       }     }     cout << r;   }   int main() {     int i, j;     cin >> m >> n;     for (i = 1; i <= m; ++i) {       scanf( %I64d-%c  , &b[i].nr, &b[i].c);       if (i > 1 && b[i].c == b[i - 1].c) {         b[i - 1].nr += b[i].nr;         --m;         --i;       }     }     for (i = 1; i <= n; ++i) {       scanf( %I64d-%c  , &a[i].nr, &a[i].c);       if (i > 1 && a[i].c == a[i - 1].c) {         a[i - 1].nr += a[i].nr;         --n;         --i;       }     }     if (n == 1) {       long long r = 0;       for (i = 1; i <= m; ++i) {         if (a[1].c == b[i].c) r += max(0LL, b[i].nr - a[1].nr + 1);       }       cout << r;       return 0;     }     if (n == 2) {       if (m == 1) {         cout << 0;         return 0;       }       long long r = 0;       for (i = 1; i < m; ++i) {         if (a[1].c == b[i].c && a[2].c == b[i + 1].c && a[1].nr <= b[i].nr &&             a[2].nr <= b[i + 1].nr)           ++r;       }       cout << r;       return 0;     }     if (m < n) {       cout << 0;       return 0;     }     calc();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> v;   void dfs(int depth, bool state) {     if (depth == 0) return;     dfs(depth - 1, !state);     v.push_back(state);     dfs(depth - 1, state);   }   int main() {     int N;     cin >> N;     int D = 1;     while (1 << D <= N) D++;     dfs(D - 1, 1);     int n = v.size();     for (int i = 0; i < (int)v.size() - 1; i++) n += (v[i] == v[i + 1]);     cout << (n == N or n + 1 == N) << endl;   }
#include <bits/stdc++.h>   using namespace std;   const long double pi = acos(-1);   const long long inf = 1LL << 30;   const double eps = 1e-9;   const long long mod = 1e9 + 7;   const int N = 100100;   pair<int, pair<int, int>> a[N];   int seg[N << 2], lazy[N << 2];   ;   int ans[N];   int dp[N];   void push(int nd, int L, int R) {     if (lazy[nd] == 0) return;     seg[nd] = 0;     if (L != R) {       lazy[nd << 1] = 1;       lazy[nd << 1 | 1] = 1;     }     lazy[nd] = 0;   }   void update(int nd, int L, int R, int idx, int val) {     if (L == R) {       seg[nd] = val;       return;     }     int mid = (L + R) >> 1;     if (idx <= mid)       update(nd << 1, L, mid, idx, val);     else       update(nd << 1 | 1, mid + 1, R, idx, val);     seg[nd] = seg[nd << 1] + seg[nd << 1 | 1];   }   long long query(int nd, int L, int R, int from, int to) {     push(nd, L, R);     if (from <= L && R <= to) return seg[nd];     if (from > R || to < L) return 0;     int mid = (L + R) >> 1;     long long leftAns = query(nd << 1, L, mid, from, to);     long long rightAns = query(nd << 1 | 1, mid + 1, R, from, to);     return leftAns + rightAns;   }   void updateRange(int nd, int L, int R, int from, int to) {     push(nd, L, R);     if (R < from || L > to) return;     if (from <= L && R <= to) {       lazy[nd] = 1;       push(nd, L, R);       return;     }     int mid = (L + R) >> 1;     updateRange(nd << 1, L, mid, from, to);     updateRange(nd << 1 | 1, mid + 1, R, from, to);     seg[nd] = seg[nd << 1] + seg[nd << 1 | 1];   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d%d , &a[i].first, &a[i].second.first);       a[i].second.second = i;     }     sort(a, a + n);     for (int i = n - 1; i >= 0; i--) {       int j = lower_bound(                   a, a + n,                   make_pair(a[i].first + a[i].second.first, make_pair(0, 0))) -               a - 1;       dp[i] = 1 + query(1, 1, n, i, j);       updateRange(1, 1, n, i + 1, j);       update(1, 1, n, i, dp[i]);       ans[a[i].second.second] = dp[i];     }     for (int i = 0; i < n; i++) printf( %d  , ans[i]);   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 2005;   int n, h, l, r, a[maxN];   int dp[maxN][maxN];   inline int getint() {     int num = 0, bj = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c == EOF) return EOF;       bj = (c ==  -  || bj == -1) ? -1 : 1, c = getchar();     }     while (c >=  0  && c <=  9 ) num = num * 10 + c -  0 , c = getchar();     return num * bj;   }   void init() {     cin >> n >> h >> l >> r;     for (int i = 1; i <= n; ++i) {       a[i] = getint();     }   }   int cor(int x) { return (x % h + h) % h; }   void solve() {     memset(dp, -1, sizeof(dp));     int ans = -1;     (a[1] >= l && a[1] <= r)                     ? dp[1][a[1]] = 1,                                ans = max(ans, 1) : dp[1][a[1]] = 0,                                ans = max(ans, 0);     (a[1] - 1 >= l && a[1] - 1 <= r)         ? dp[1][a[1] - 1] = 1,                        ans = max(ans, 1) : dp[1][a[1] - 1] = 0, ans = max(ans, 0);     for (int i = 2; i <= n; ++i) {       for (int j = 0; j < h; ++j) {         if (dp[i - 1][cor(j - a[i])] == -1 && dp[i - 1][cor(j - a[i] + 1)] == -1)           continue;         dp[i][j] = max(dp[i - 1][cor(j - a[i])], dp[i - 1][cor(j - a[i] + 1)]);         if (j >= l && j <= r) dp[i][j]++;         ans = max(ans, dp[i][j]);       }     }     cout << ans << endl;   }   int main() {     init();     solve();   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> cnt(string s) {     int bal = 0, minBal = 0;     for (char c : s) {       if (c ==  ( )         bal++;       else         bal--;       minBal = min(minBal, bal);     }     return {bal, minBal};   }   int k[400400];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<int> bal(n), minBal(n);     for (int i = 0; i < n; i++) {       string s;       cin >> s;       auto p = cnt(s);       bal[i] = p.first;       minBal[i] = p.second;       if (bal[i] >= 0 && minBal[i] >= 0) k[bal[i]]++;     }     long long ans = 0;     for (int i = 0; i < n; i++) {       if (bal[i] > 0) continue;       if (minBal[i] - bal[i] >= 0) ans += (long long)k[-bal[i]];     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   const int INF = 0x3f3f3f3f;   const int mo = 1e9 + 7;   inline int ksm(int a, int b) {     int ret = 1;     for (; b; b >>= 1, a = 1ll * a * a % mo)       if (b & 1) ret = 1ll * ret * a % mo;     return ret;   }   inline int read() {     char ch = getchar();     int x = 0, f = 1;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 3) + (x << 1) + ch -  0 ;       ch = getchar();     }     return x * f;   }   int n = 4, m, f[N][21], dep[N];   struct node {     int u, v, mx;     bool operator<(const node &A) const { return mx < A.mx; }   } a;   int LCA(int x, int y) {     if (dep[x] < dep[y]) swap(x, y);     for (int i = 20; i >= 0; i--)       if (dep[f[x][i]] >= dep[y]) x = f[x][i];     if (x == y) return x;     for (int i = 20; i >= 0; i--)       if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];     return f[x][0];   }   int dist(int x, int y) {     int lca = LCA(x, y);     return dep[x] + dep[y] - 2 * dep[lca];   }   void add(int u) {     int x = n + 1, y = n + 2;     n += 2;     dep[x] = dep[y] = dep[u] + 1;     f[x][0] = f[y][0] = u;     for (int i = 1; i <= 20; i++) f[x][i] = f[y][i] = f[f[x][i - 1]][i - 1];     int xx = a.u, yy = a.v;     node b = (node){xx, x, dist(xx, x)}, c = (node){xx, y, dist(xx, y)};     node d = (node){yy, x, dist(yy, x)}, e = (node){yy, y, dist(yy, y)};     node f = (node){xx, yy, dist(xx, yy)};     if (a < b) a = b;     if (a < c) a = c;     if (a < d) a = d;     if (a < e) a = e;     if (a < f) a = f;   }   signed main() {     dep[1] = 1;     a.u = 2, a.v = 4, a.mx = 2;     dep[2] = dep[3] = dep[4] = 2;     f[1][0] = f[2][0] = f[3][0] = f[4][0] = 1;     m = read();     for (; m; m--) {       int u = read();       add(u);       printf( %d n , a.mx);     }     return 0;   }