Guest User

f.cpp

a guest
Oct 29th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. vector <int> vec;
  5. int timer = 0;
  6. void dfs(int x){
  7.     tin[x] = timer++;
  8.     vec.pb(x);
  9.     a.pb(color[x]);
  10.     was[x] = 1;
  11.     for (int i = 0; i < g[x].size(); i++){
  12.         int to = g[x][i];
  13.         if (!was[to]){
  14.             dfs(to);
  15.         }
  16.     }
  17.  
  18.     tout[x] = timer++;
  19.     a.pb(0);
  20.     vec.pb(x);
  21. }
  22.  
  23. void add(int x){
  24.     if (a[x] != 0){
  25.         cnt[a[x]]++;
  26.         cnt2[cnt[a[x]]]++;
  27.     }
  28. }
  29. void del(int x){
  30.     if (a[x] != 0){
  31.         cnt[a[x]]--;
  32.         cnt2[cnt[a[x]]]--;
  33.     }
  34. }
  35.  
  36. int main()
  37. {
  38.     ...
  39.  
  40.  
  41.     dfs(1);
  42.     len = sqrt(2 * n);
  43.     pair <pair <int, int>, pair <int, pair <int, int> > > b[q];
  44.     for (int i = 0; i < q; i++){
  45.         cin >> v >> k;
  46.         l = tin[v];
  47.         r = tout[v];
  48.         b[i].first.first = l / len;
  49.         b[i].first.second = r;
  50.         b[i].second.first = l;
  51.         b[i].second.second.first = k;
  52.         b[i].second.second.second = i;
  53.     }
  54.     sort(b, b + q);
  55.     left = 0;
  56.     right = -1;
  57.     for (int i = 0; i < q; i++){
  58.         int l = b[i].second.first;
  59.         int r = b[i].first.second;
  60.         int k = b[i].second.second.first;
  61.         int ind = b[i].second.second.second;
  62.  
  63.         while (right < r){
  64.             right++;
  65.             add(right);
  66.         }
  67.         while (left > l){
  68.             left--;
  69.             add(left);
  70.         }
  71.  
  72.         while (right > r){
  73.             del(right);
  74.             right--;
  75.         }
  76.         while (left < l){
  77.             del(left);
  78.             left++;
  79.         }
  80.         ans[ind] = cnt2[k];
  81.     }
  82.  
  83.     for (int i = 0; i < q; i++){
  84.         cout << ans[i] << ' ';
  85.     }
  86.     return 0;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment