orazma

f.cpp

Apr 16th, 2026
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define pb push_back
  5. #define mp make_pair
  6. #define ll long long
  7.  
  8. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  9.  
  10. using namespace std;
  11. const int INF = 1e9 + 1;
  12. const int MAXN = 2e5 + 10;
  13. int tin[MAXN], a[2 * MAXN], tout[MAXN], ans[MAXN], col[MAXN], cnt[MAXN], cntCnt[MAXN];
  14. int n, m;
  15. pair <pair <int, int>, pair <int, pair <int, int> > > b[MAXN];
  16. int timer = 0;
  17. bool was[MAXN];
  18. vector <int> g[MAXN];
  19.  
  20. void dfs(int v){
  21.     was[v] = 1;
  22.     tin[v] = ++timer;
  23.     a[tin[v]] = col[v];
  24.     for (int to : g[v]){
  25.         if (!was[to]){
  26.             dfs(to);
  27.         }
  28.     }
  29.  
  30.     tout[v] = ++timer;
  31.     a[tout[v]] = -1;
  32. }
  33.  
  34. void add(int x){
  35.     if (x == -1){
  36.         return;
  37.     }
  38.     cnt[x]++;
  39.     cntCnt[cnt[x]]++;
  40. }
  41.  
  42. void del(int x){
  43.     if (x == -1){
  44.         return;
  45.     }
  46.     cntCnt[cnt[x]]--;
  47.     cnt[x]--;
  48. }
  49.  
  50. void solve(){
  51.  
  52.     cin >> n >> m;
  53.     for (int i = 1; i <= n; i++){
  54.         cin >> col[i];
  55.     }
  56.     for (int i = 1; i < n; i++){
  57.         int x, y;
  58.         cin >> x >> y;
  59.         g[x].pb(y);
  60.         g[y].pb(x);
  61.     }
  62.  
  63.     dfs(1);
  64.     int len = sqrt(n);
  65.     for (int i = 1; i <= m; i++){
  66.         int v, k;
  67.         cin >> v >> k;
  68.         b[i].f.f = tin[v] / len;
  69.         b[i].f.s = tout[v];
  70.         b[i].s.f = tin[v];
  71.         b[i].s.s.f = i;
  72.         b[i].s.s.s = k;
  73.     }
  74.  
  75.     sort(b + 1, b + m + 1);
  76.     int l = 1;
  77.     int r = 0;
  78.     for (int i = 1; i <= m; i++){
  79.         while (r < b[i].f.s){
  80.             r++;
  81.             add(a[r]);
  82.         }
  83.         while (l > b[i].s.f){
  84.             l--;
  85.             add(a[l]);
  86.         }
  87.         while (r > b[i].f.s){
  88.             del(a[r]);
  89.             r--;
  90.         }
  91.         while(l < b[i].s.f){
  92.             del(a[l]);
  93.             l++;
  94.         }
  95.  
  96.         ans[b[i].s.s.f] = cntCnt[b[i].s.s.s];
  97.     }
  98.  
  99.     for (int i = 1; i <= m; i++){
  100.         cout << ans[i] << '\n';
  101.     }
  102.  
  103. }
  104. int main() {
  105.     IOS;
  106.     //freopen("rmq.in", "r", stdin);
  107.     //freopen("rmq.out", "w", stdout);
  108.     int t = 1;
  109.    // cin >> t;
  110.     while(t--){
  111.         solve();
  112.     }
  113.     return 0;
  114. }
  115.  
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment