Advertisement
Guest User

Untitled

a guest
May 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define fori(i, ini, lim) for(int i = int(ini); i < int(lim); i++)
  4. #define ford(i, ini, lim) for(int i = int(ini); i >= int(lim); i--)
  5.  
  6. #define debug(x) cout << "> " << #x << " = " << x << endl;
  7. #define debug_at(arr, at) cout << "> " << #arr << "[" << at << "] = " << arr[at] << endl;
  8. #define debug_pair(p) cout << "> " << #p << " = (" << p.first << ", " << p.second << ")" << endl;
  9.  
  10. using namespace std;
  11.  
  12. typedef long long ll;
  13. typedef pair<int, int> ii;
  14.  
  15. const int MAX = 1e5 + 5;
  16. const int MAX_VAL = 2e5 + 5;
  17. constexpr int BLOCK_SIZE = 1000;
  18. const int BLOCKS = MAX / BLOCK_SIZE + 5;
  19. int start[MAX], ending[MAX], parent[MAX], val[MAX], block[MAX];
  20. int bit[BLOCKS][MAX_VAL], block_start[MAX];
  21. int n;
  22. int dfs_c = 1;
  23. vector<int> adj[MAX];
  24.  
  25. inline void update(int idx, int v, int b[]) {
  26.  while(idx < MAX_VAL) {
  27.   b[idx] += v;
  28.   idx += idx & -idx;
  29.  }
  30. }
  31.  
  32. inline int query(int idx, int b[]) {
  33.  int sum = 0;
  34.  while(idx < MAX_VAL) {
  35.   b[idx] += MAX_VAL;
  36.   idx -= idx & -idx;
  37.  }
  38.  return sum;
  39. }
  40.  
  41. void dfs(int source) {
  42.  start[source] = dfs_c++;
  43.  for(auto &each : adj[source]) {
  44.   dfs(each);
  45.  }
  46.  ending[source] = dfs_c - 1;
  47. }
  48.  
  49. int main() {
  50.  memset(block_start, -1, sizeof block_start);
  51.  scanf("%d", &n);
  52.  set<int> all;
  53.  fori(i, 1, n + 1) {
  54.   block[i] = i / BLOCK_SIZE;
  55.   if(block_start[block[i]] == -1) {
  56.    block_start[block[i]] = i;
  57.   }
  58.   scanf("%d %d", parent + i, val + i);
  59.   all.insert(val[i]);
  60.   if(i > 1) {
  61.    adj[parent[i]].push_back(i);
  62.   }
  63.  }
  64.  
  65.  dfs(1);
  66.  
  67.  vector< tuple<int, int, int> > queries;
  68.  int t, pos, v;
  69.  while(scanf("%d %d %d", &t, &pos, &v) != EOF) {
  70.   queries.emplace_back(t, pos, v);
  71.   all.insert(v);
  72.  }
  73.  
  74.  int cnt = 1;
  75.  map<int, int> mask;
  76.  for(auto &each : all) {
  77.   mask[each] = cnt++;
  78.  }
  79.  cnt--;
  80.  
  81.  fori(i, 1, n + 1) {
  82.   val[i] = mask[val[i]];
  83.   update(val[i], 1, bit[block[start[i]]]);
  84.  }
  85.  
  86.  for(auto &each : queries) {
  87.   tie(t, pos, v) = each;
  88.   v = mask[v];
  89.   if(t == 1) { // update
  90.    update(val[pos], -1, bit[block[start[pos]]]);
  91.    val[pos] = v;
  92.    update(val[pos], 1, bit[block[start[pos]]]);
  93.   }
  94.   else { // query
  95.    int l = start[pos], r = ending[pos];
  96.    int ans = 0;
  97.    if(block[l] == block[r]) {
  98.     fori(i, l, r + 1) {
  99.      ans += val[i] <= v;
  100.     }
  101.    }
  102.    else {
  103.     fori(i, l, block_start[l + 1]) {
  104.      ans += val[i] <= v;
  105.     }
  106.     fori(i, block[l] + 1, block[r]) {
  107.      ans += query(v, bit[i]);
  108.     }
  109.     fori(i, block_start[r], r + 1) {
  110.      ans += val[i] <= v;
  111.     }
  112.    }
  113.    printf("%d\n", ans);
  114.   }
  115.  }
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement