malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>      #define fi first   #define se second   #define DB double   #define U unsigned   #define P std::pair   #define LL long long   #define LD long double   #define pb emplace_back   #define MP std::make_pair   #define SZ(x) ((int)x.size())   #define all(x) x.begin(),x.end()   #define CLR(i,a) memset(i,a,sizeof(i))   #define FOR(i,a,b) for(int i = a;i <= b;++i)   #define ROF(i,a,b) for(int i = a;i >= b;--i)   #define DEBUG(x) std::cerr << #x <<  =  << x << std::endl      const int MAXN = 400+5;   const int ha = 998244353;   int n,c[26];   int f[2][MAXN][MAXN][3][3];   int g[2][MAXN][2][2],pw[MAXN];   int F[MAXN][MAXN],G[MAXN];   // 0=a 1=b 2=oth      inline void add(int &x,int y){       x += y-ha;x += x>>31&ha;   }      int main(){       pw[0] = 1;FOR(i,1,MAXN-1) pw[i] = 25ll*pw[i-1]%ha;       scanf( %d ,&n);       FOR(i,0,25) scanf( %d ,c+i);       int now = 0;       f[now][0][0][2][2] = 24*24;       f[now][1][0][2][0] = f[now][1][0][0][2] = 24;       f[now][0][1][2][1] = f[now][0][1][1][2] = 24;       f[now][1][1][0][1] = f[now][1][1][1][0] = 1;       f[now][2][0][0][0] = f[now][0][2][1][1] = 1;       FOR(i,3,n){           CLR(f[now^1],0);           FOR(a,0,n){               FOR(b,0,n){                   FOR(c,0,2){                       FOR(d,0,2){                           // cd?                           int gx = f[now][a][b][c][d];                           if(!gx) continue;                           if(c != 0) add(f[now^1][a+1][b][d][0],gx);                           if(c != 1) add(f[now^1][a][b+1][d][1],gx);                           add(f[now^1][a][b][d][2],1ll*gx*(c==2?23:24)%ha);                       }                   }               }           }           now ^= 1;       }       FOR(i,0,n) FOR(j,0,n) FOR(x,0,2) FOR(y,0,2) add(F[i][j],f[now][i][j][x][y]);       ROF(i,n,0) ROF(j,n,1) add(F[i][j],F[i+1][j]),add(F[i][j],F[i][j+1]),add(F[i][j],ha-F[i+1][j+1]);       now = 0;       g[now][0][1][1] = 25*25;       g[now][1][0][1] = g[now][1][1][0] = 25;       g[now][2][0][0] = 1;       FOR(i,3,n){           CLR(g[now^1],0);           FOR(a,0,n){               FOR(c,0,1){                   FOR(d,0,1){                       int gx = g[now][a][c][d];                       if(!gx) continue;                       if(c != 0) add(g[now^1][a+1][d][0],gx);                       add(g[now^1][a][d][1],1ll*gx*(c==1?24:25)%ha);                   }               }           }           now ^= 1;       }   //    DEBUG(g[now][3][0][1]);       FOR(i,0,n) FOR(x,0,1) FOR(y,0,1) add(G[i],g[now][i][x][y]);       ROF(i,n,0) add(G[i],G[i+1]);       auto calc = [&](int n){           return 26ll*pw[n-1]%ha;       };       int ans = 1ll*calc(n/2)*calc((n+1)/2)%ha;       FOR(i,0,25) add(ans,ha-G[c[i]+1]);       FOR(i,0,25) FOR(j,i+1,25) add(ans,F[c[i]+1][c[j]+1]);       printf( %d n ,ans);       return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int a[510];   vector<pair<int, int> > edges;   vector<int> leaf, nonleaf;   int d = 0;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) {       if (a[i] == 1)         leaf.push_back(i);       else         nonleaf.push_back(i);     }     d = nonleaf.size() - 1 + min((int)leaf.size(), 2);     int reserved = -1;     if (!leaf.empty()) reserved = leaf.back(), leaf.pop_back();     for (int i = 0; i < (int)nonleaf.size() - 1; i++) {       int k = nonleaf[i];       while (!leaf.empty() && a[k] > 1) {         edges.push_back({leaf.back(), k});         leaf.pop_back();         a[k]--;       }       a[k]--;       a[nonleaf[i + 1]]--;       edges.push_back({k, nonleaf[i + 1]});     }     int tp = 501;     if (!nonleaf.empty()) tp = nonleaf.back();     if (reserved != -1) leaf.push_back(reserved);     while (!leaf.empty() && a[tp] > 0) {       edges.push_back({leaf.back(), tp});       leaf.pop_back();       a[tp]--;     }     if (leaf.empty()) {       printf( YES %d n , d);       printf( %d n , (int)edges.size());       for (auto k : edges) {         printf( %d %d n , k.first, k.second);       }     } else {       printf( NO n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1234567;   const long long MOD = 998244353;   void solve() {     long long n, m, i, j = 0, k, l = 0, ok = 0, o = 0, p, ans = 0, x, y, a[30];     string s;     cin >> n >> k >> s;     for (long long i = 0; i < k / 2; i++) {       map<char, long long> mg;       for (j = 0; j < n; j += k) {         mg[s[i + j]]++;         mg[s[j + k - i - 1]]++;       }       l = 0;       for (auto it : mg) l = max(l, it.second);       ans += l;     }     if (k % 2) {       map<char, long long> mg;       for (j = 0; j < n; j += k) {         mg[s[j + k / 2]]++;       }       l = 0;       for (auto it : mg) l = max(l, it.second);       ans += l;     }     cout << (n - ans) << endl;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     tuple<long long, long long, long long> p[200010];     int n;     cin >> n;     long long k, a, x, y, m;     long long b;     long long ans = 0;     long long cnt = 0;     long long sm = 0;     for (int i = 0; i < n; i++) {       cin >> k >> a >> x >> y >> m;       long long t = 0;       sm += k;       for (int j = 0; j < k; j++) {         if (cnt <= 200000) p[cnt++] = {t, a, i};         b = (a * x + y) % m;         if (b < a && j != k - 1) t++;         a = b;       }       ans = max(ans, t);     }     cout << ans << endl;     if (cnt <= 200000) {       sort(p, p + cnt,            [](tuple<long long, long long, long long> &a,               tuple<long long, long long, long long> &b) -> bool {              if (get<0>(a) == get<0>(b)) {                return get<1>(a) < get<1>(b) ||                       (get<1>(a) == get<1>(b) && get<2>(a) < get<2>(b));              }              return get<0>(a) < get<0>(b);            });       for (int i = 0; i < cnt; i++) {         cout << get<1>(p[i]) <<     << get<2>(p[i]) + 1 << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 210;   const double eps = 1e-9;   int n;   int lsh[maxn * 2], ln, K;   map<int, int> mt;   int To[maxn];   int li[maxn], ri[maxn];   double f[2][maxn][maxn];   double ans[maxn][maxn];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &li[i], &ri[i]);       lsh[++ln] = li[i];       lsh[++ln] = ri[i];     }     sort(lsh + 1, lsh + ln + 1);     lsh[0] = lsh[1] - 1;     K = 0;     for (int i = 1; i <= ln; i++) {       if (lsh[i] != lsh[i - 1]) {         To[mt[lsh[i]] = ++K] = lsh[i];       }     }     for (int i = 1; i <= n; i++) {       li[i] = mt[li[i]], ri[i] = mt[ri[i]];     }     for (int i = 1; i <= n; i++) {       for (int x = li[i]; x < ri[i]; x++) {         double inow = (double)(To[x + 1] - To[x]) / (To[ri[i]] - To[li[i]]);         memset(f, 0, sizeof(f));         int now = 1;         f[now][0][0] = 1.0;         for (int j = 1; j <= n; j++) {           if (i == j) {             continue;           }           int L = To[li[j]], R = To[ri[j]];           now = !now;           if (x < li[j]) {             for (int x1 = 0; x1 < j; x1++) {               for (int x2 = 0; x2 < j - x1; x2++) {                 if (f[!now][x1][x2] > eps) {                   double tmp = f[!now][x1][x2];                   f[!now][x1][x2] = 0;                   f[now][x1][x2] += tmp;                 }               }             }           } else if (ri[j] <= x) {             for (int x1 = 0; x1 < j; x1++) {               for (int x2 = 0; x2 < j - x1; x2++) {                 if (f[!now][x1][x2] > eps) {                   double tmp = f[!now][x1][x2];                   f[!now][x1][x2] = 0;                   f[now][x1 + 1][x2] += tmp;                 }               }             }           } else {             double p1 = (double)(To[x] - L) / (R - L),                    p2 = (double)(To[x + 1] - To[x]) / (R - L),                    p3 = (double)(R - To[x + 1]) / (R - L);             for (int x1 = 0; x1 < j; x1++) {               for (int x2 = 0; x2 < j - x1; x2++) {                 if (f[!now][x1][x2] > eps) {                   double tmp = f[!now][x1][x2];                   f[!now][x1][x2] = 0;                   f[now][x1 + 1][x2] += tmp * p1;                   f[now][x1][x2 + 1] += tmp * p2;                   f[now][x1][x2] += tmp * p3;                 }               }             }           }         }         for (int x1 = 0; x1 < n; x1++) {           for (int x2 = 0; x2 < n - x1; x2++) {             if (f[now][x1][x2] > eps) {               double tmp = f[now][x1][x2], temp = 1.0 / (x2 + 1.0);               for (int l = 0; l <= x2; l++) {                 ans[i][x1 + l + 1] += inow * tmp * temp;               }             }           }         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         printf( %.10lf  , ans[i][j]);       }       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     vector<string> ans;     string email =   ;     bool isPoss = true;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  @ ) {         if ((i + 1 < s.length()) && (s[i + 1] !=  @ )) {           if (email ==   ) {             isPoss = false;             break;           } else {             email += s[i];             email += s[i + 1];             ans.push_back(email);             i++;             email =   ;           }         } else {           isPoss = false;           break;         }       } else {         email += s[i];       }     }     if ((email !=   ) && (isPoss) && (ans.size() > 0)) {       ans[ans.size() - 1] = ans[ans.size() - 1] + email;     }     if ((ans.size() == 0) || (!isPoss)) {       cout <<  No solution ;     } else {       cout << ans[0];       for (int i = 1; i < ans.size(); i++) {         cout <<  ,  << ans[i];       }     }   }
#include <bits/stdc++.h>   using namespace std;   char s[1000005];   int main() {     int k, a[10], i, sum, num, maxr;     while (~scanf( %d%s , &k, s)) {       memset(a, 0, sizeof(a));       num = sum = 0;       maxr = -1;       for (i = 0; s[i]; i++) {         int t = s[i] -  0 ;         maxr = max(maxr, t);         a[t]++;         sum += t;       }       if (sum >= k)         puts( 0 );       else {         int flag = 0;         for (i = 0; i <= maxr; i++) {           while (a[i]) {             a[i]--;             sum -= i;             sum += 9;             num++;             if (sum >= k) {               flag = 1;               break;             }           }           if (flag) {             printf( %d n , num);             break;           }         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[100005];   int main() {     int n;     scanf( %d , &n);     memset(a, -1, sizeof(int) * 100005);     int cnt = 1;     for (int i = 2; i <= n; i++) {       if (a[i] == -1) {         for (int j = i; j <= n; j += i) {           if (a[j] == -1) a[j] = cnt;         }         cnt++;       }     }     for (int i = 2; i <= n; i++) {       printf( %d  , a[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, s, i, h, m;     cin >> n >> s;     int a[110];     a[0] = 0;     for (i = 1; i <= n; i++) {       cin >> h >> m;       a[i] = h * 60 + m;     }     if (a[1] - a[0] - 1 >= s) {       cout <<  0 0 ;       return 0;     }     for (i = 2; i <= n; i++) {       if (a[i] - a[i - 1] - 2 >= (2 * s)) {         cout << (a[i - 1] + s + 1) / 60 <<     << (a[i - 1] + s + 1) % 60;         return 0;       }     }     cout << (a[n] + s + 1) / 60 <<     << (a[n] + s + 1) % 60;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool isprime[2000005];   void setprime() {     for (long long i = 0; i < 2000005; i++) isprime[i] = true;     isprime[0] = false;     isprime[1] = false;     for (long long i = 2; i < 2000005; i++) {       for (long long j = 2; i * j < 2000005; j++) isprime[i * j] = false;     }   }   long long choose(long long n, long long k) {     if (k == 0) return 1;     return (n * choose(n - 1, k - 1)) / k;   }   long long gcd(long long a, long long b) {     if (!a) return b;     return gcd(b % a, a);   }   long long n, m, temp;   pair<long long, long long> p[100005];   bool start[100005];   vector<pair<pair<long long, long long>, long long> > ans;   long long min1 = LONG_LONG_MAX;   long long end1;   void dfs(long long x) {     if (p[x].first != 0) {       min1 = min(min1, p[x].second);       dfs(p[x].first);     } else {       end1 = x;       return;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (long long i = 0; i < m; i++) {       long long u, v, w;       cin >> u >> v >> w;       p[u] = make_pair(v, w);       start[v] = 1;     }     for (long long i = 1; i < n + 1; i++) {       if (!start[i]) {         min1 = LONG_LONG_MAX;         end1 = -1;         dfs(i);         if (min1 != LONG_LONG_MAX)           ans.push_back(make_pair(make_pair(i, end1), min1));       }     }     sort(ans.begin(), ans.end());     cout << ans.size() << endl;     for (long long i = 0; i < ans.size(); i++) {       cout << ans[i].first.first <<     << ans[i].first.second <<                << ans[i].second << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   long long MOD = 1000000007;   const int MAXN = 107;   int A[50007];   int F[107][107];   unsigned long long b[107][107];   struct matrix {     int n, m;     int arr[MAXN][MAXN];     matrix() {}     matrix(int x, int y, int V = 0) {       n = x;       m = y;       for (int j = 0; j < n; j++)         for (int i = 0; i < m; i++) arr[j][i] = V;     }     matrix(int x, int y, int A[MAXN][MAXN]) {       n = x;       m = y;       for (int j = 0; j < n; j++)         for (int i = 0; i < m; i++) arr[j][i] = A[j][i];     }     matrix operator*(matrix &mat) {       matrix res(n, mat.m, 0);       long long theta;       for (int j = 0; j < n; j++) {         for (int i = 0; i < mat.m; i++) {           theta = 0;           for (int k = 0; k < m; k++) {             theta += 1ll * arr[j][k] * mat.arr[k][i];             theta %= MOD;           }           res.arr[j][i] = theta;         }       }       return res;     }     matrix power(long long K) {       matrix res(n, m, 0), b(n, m, 0);       for (int j = 0; j < n; j++) res.arr[j][j] = 1;       b = (*this);       while (K > 0) {         if ((K & 1)) res = res * b;         K /= 2;         b = b * b;       }       return res;     }   };   int main() {     int n, k, x;     unsigned long long b;     cin >> n >> b >> k >> x;     for (int i = 0; i < n; i++) cin >> A[i];     for (int j = 0; j < x; j++)       for (int i = 0; i < n; i++) F[j][(j * 10 + A[i]) % x]++;     matrix M(x, x, F);     matrix B(x, x, 0);     B = M.power(b);     cout << B.arr[0][k] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   #pragma GCC target( avx2 )   #pragma GCC optimization( O3 )   #pragma GCC optimization( unroll-loops )   void f() {     int n;     string s, t;     cin >> n >> s >> t;     map<char, int> d1, d2;     for (int i = 0; i < n; i++) {       ++d1[s[i]];       ++d2[t[i]];     }     if (d1 != d2) {       cout << -1;       return;     }     int ans = 0;     int l = 1, r = n, mid;     while (l <= r) {       mid = (l + r) / 2;       bool cs = false;       for (int left = 0; left + mid - 1 < n; left++) {         int ptr = left;         for (int i = 0; i < n && ptr < left + mid; i++) {           if (s[i] == t[ptr]) {             ++ptr;           }         }         if (ptr == left + mid) {           cs = true;           break;         }       }       if (cs) {         ans = mid;         l = mid + 1;       } else {         r = mid - 1;       }     }     cout << n - ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int q;     cin >> q;     while (q--) {       f();       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int dp[1000000 + 10], cnt[1000000 + 10];   int n;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int a;       scanf( %d , &a);       int t = 0, temp = a;       while (temp <= 1e6) {         dp[temp] += t;         cnt[temp]++;         t++;         temp = (temp << 1);       }       t = 0;       while (a) {         if (a & 1 && a != 1) {           int nt = t + 2, temp = a - 1;           while (temp <= 1e6) {             dp[temp] += nt;             cnt[temp]++;             nt++;             temp = (temp << 1);           }         }         t++;         a = (a >> 1);         dp[a] += t;         cnt[a]++;       }     }     int ans = 0x3f3f3f3f;     for (int i = 1; i <= 1e6; i++) {       if (cnt[i] == n) ans = min(ans, dp[i]);     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool compare(int a, int b) { return a > b; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     for (long long int i = 0; i < n + 1; i++) {       for (long long int j = 0; j < 2 * (n - i); j++) {         cout <<    ;       }       for (long long int j = 0; j < i + 1; j++) {         if (i)           cout << j <<    ;         else           cout << j;       }       for (int j = i - 1; j >= 0; j--) {         if (j)           cout << j <<    ;         else           cout << j;       }       cout << endl;     }     for (int i = n - 1; i >= 1; i--) {       for (long long int j = 0; j < 2 * (n - i); j++) {         cout <<    ;       }       for (long long int j = 0; j < i + 1; j++) {         cout << j <<    ;       }       for (int j = i - 1; j >= 0; j--) {         if (j)           cout << j <<    ;         else           cout << j;       }       cout << endl;     }     for (long long int j = 0; j < 2 * (n); j++) {       cout <<    ;     }     cout <<  0 ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 9;   int n;   long long a[N], l[N], r[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n;     for (int i = 1; i <= n - 1; ++i) cin >> a[i];     long long tmp;     l[1] = 0;     for (int i = 2; i <= n; ++i) {       tmp = (a[i - 1] % 2 == 0) ? a[i - 1] : a[i - 1] - 1;       if (a[i - 1] >= 2) l[i] = l[i - 1] + tmp;     }     r[n] = 0;     for (int i = n - 1; i >= 1; --i) {       tmp = (a[i] % 2 == 0) ? a[i] : a[i] - 1;       if (a[i] >= 2) r[i] = r[i + 1] + tmp;     }     tmp = 0;     long long delta;     long long res = 0;     for (int i = n - 1; i >= 1; --i) {       delta = (a[i] % 2 == 0) ? a[i] - 1 : a[i];       tmp = max(tmp + delta, r[i]);       res = max(res, l[i] + tmp);     }     cout << res;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int arr[n];     int freq[101] = {0};     for (int i = 0; i < n; i++) {       cin >> arr[i];       freq[arr[i]]++;     }     sort(freq, freq + 101, greater<int>());     int max = freq[0];     int num = 0;     for (int i = 0; i < 101; i++) {       if (freq[i] > 0) {         num++;       }     }     int b;     int a = (float)k;     int c = (float)max;     if (k > max) {       b = 1;     } else if (c % a == 0) {       b = max / k;     } else if (k < max && a % c != 0) {       b = (max / k) + 1;     }     cout << (b * k * num) - n << endl;   }
#include <bits/stdc++.h>   using namespace std;   bool color[200005], vis[200005];   int height[200005], dp[200005];   vector<vector<int> > v(200005);   int ans;   void dfs(int a) {     vis[a] = true;     dp[a] = 0;     for (int i = 0; i < v[a].size(); i++) {       if (!vis[v[a][i]]) {         dfs(v[a][i]);         ans = max(ans, dp[a] + dp[v[a][i]] + (color[a] != color[v[a][i]]));         dp[a] = max(dp[a], dp[v[a][i]] + (color[a] != color[v[a][i]]));       }     }     ans = max(ans, dp[a]);   }   int main() {     ios_base::sync_with_stdio(0);     ;     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> color[i + 1];     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     dfs(1);     cout << (ans + 1) / 2;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     unordered_map<int, int> mp;     vector<int> v[n + 1];     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     vector<int> t;     int ans = 0;     while (1) {       int num = 0;       vector<int> t;       for (int i = 1; i <= n; i++) {         if (v[i].size() == 1) {           num++;           t.push_back(i);         }       }       if (num > 0) {         ans++;         for (int i = 0; i < num; i++) {           int neigh = v[t[i]][0];           v[t[i]].clear();           v[neigh].erase(remove(v[neigh].begin(), v[neigh].end(), t[i]),                          v[neigh].end());         }       } else {         break;       }     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 0x3f3f3f3f;   unordered_map<int, int> cnt;   int maxi = 0;   int x[100005];   int head[100005], ne[200005], to[200005];   int idx = 0;   int son[100005];   int sz[100005];   long long sum = 0;   long long s[100005];   void Insert(int a, int b) {     to[++idx] = b;     ne[idx] = head[a];     head[a] = idx;   }   void Add(int root, int fa) {     cnt[x[root]]++;     if (cnt[x[root]] == maxi) {       sum += x[root];     } else if (cnt[x[root]] > maxi) {       maxi = cnt[x[root]];       sum = x[root];     }     for (int i = head[root]; i != -1; i = ne[i]) {       int a = to[i];       if (a == fa) continue;       Add(a, root);     }   }   void dfs1(int root, int fa) {     sz[root] = 1;     for (int i = head[root]; i != -1; i = ne[i]) {       int a = to[i];       if (a == fa) continue;       dfs1(a, root);       sz[root] += sz[a];       if (sz[a] > sz[son[root]]) {         son[root] = a;       }     }   }   void dfs2(int root, int fa) {     for (int i = head[root]; i != -1; i = ne[i]) {       int a = to[i];       if (a == fa || a == son[root]) continue;       dfs2(a, root);       maxi = 0;       sum = 0;       cnt.clear();     }     if (son[root]) dfs2(son[root], root);     for (int i = head[root]; i != -1; i = ne[i]) {       int a = to[i];       if (a == fa || a == son[root]) continue;       Add(a, root);     }     cnt[x[root]]++;     if (cnt[x[root]] == maxi) {       sum += x[root];     } else if (cnt[x[root]] > maxi) {       maxi = cnt[x[root]];       sum = x[root];     }     s[root] = sum;   }   int main() {     int n;     memset(head, -1, sizeof(head));     cin >> n;     for (int i = 1; i <= n; i++) {       scanf( %d , &x[i]);     }     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d%d , &a, &b);       Insert(b, a);       Insert(a, b);     }     dfs1(1, 0);     dfs2(1, 0);     for (int i = 1; i <= n; i++) {       printf( %lld%c , s[i], i == n ?   n  :    );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 5;   int n, q;   vector<pair<int, int> > query[maxn];   vector<int> G[maxn];   long long ans[maxn];   long long add[maxn];   void dfs(int v, int p, int d, long long sum) {     for (auto p : query[v]) {       int l = d;       int r = d + p.first + 1;       add[l] += p.second;       if (r < maxn) add[r] -= p.second;     }     sum += add[d];     ans[v] = sum;     for (int u : G[v]) {       if (u == p) continue;       dfs(u, v, d + 1, sum);     }     for (auto p : query[v]) {       int l = d;       int r = d + p.first + 1;       add[l] -= p.second;       if (r < maxn) add[r] += p.second;     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       int x, y;       scanf( %d%d , &x, &y);       G[x].emplace_back(y);       G[y].emplace_back(x);     }     scanf( %d , &q);     while (q--) {       int v, d, x;       scanf( %d%d%d , &v, &d, &x);       query[v].push_back(make_pair(d, x));     }     dfs(1, 0, 1, 0);     for (int i = 1; i <= n; i++) {       printf( %I64d  , ans[i]);     }     return 0;   }