fork download
  1. /* AUTHOR: TUAN ANH - BUI */
  2. // ~~ icebear ~~
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7. typedef pair<int, int> ii;
  8. typedef pair<int, ii> iii;
  9.  
  10. template<class X, class Y>
  11. bool minimize(X &x, const Y &y) {
  12. if (x > y) return x = y, true;
  13. return false;
  14. }
  15.  
  16. template<class X, class Y>
  17. bool maximize(X &x, const Y &y) {
  18. if (x < y) return x = y, true;
  19. return false;
  20. }
  21.  
  22. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  23. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  24. #define REP(i, n) for(int i=0; i<(n); ++i)
  25. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  26. #define MASK(i) (1LL << (i))
  27. #define BIT(S, i) (((S) >> (i)) & 1)
  28. #define mp make_pair
  29. #define pb push_back
  30. #define fi first
  31. #define se second
  32. #define all(x) x.begin(), x.end()
  33. #define task "icebear"
  34. /*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN TST26 */
  35.  
  36. const int MOD = 1e9 + 7;
  37. const int inf = (int)1e9 + 27092008;
  38. const ll INF = (ll)1e18 + 27092008;
  39. const int N = 5e5 + 5;
  40. int n, q, p[N];
  41. vector<int> G[N];
  42. int par[N][20], cnt[N], sz[N], tin[N], ans[N], tout[N], tour[N], timer = 0, h[N];
  43. vector<ii> Q[N];
  44.  
  45. void dfs(int u) {
  46. tin[u] = ++timer;
  47. tour[timer] = u;
  48. sz[u] = 1;
  49. for(int v: G[u]) {
  50. h[v] = h[u] + 1;
  51. dfs(v);
  52. sz[u] += sz[v];
  53. }
  54. tout[u] = timer;
  55. }
  56.  
  57. void sack(int u, bool keep = false) {
  58. int hvy = 0;
  59. for(int v : G[u]) if (sz[v] > sz[hvy])
  60. hvy = v;
  61.  
  62. for(int v : G[u]) if (v != hvy)
  63. sack(v, false);
  64.  
  65. if (hvy > 0) sack(hvy, true);
  66.  
  67. for(int v : G[u]) if (v != hvy) {
  68. FOR(i, tin[v], tout[v]) {
  69. int x = tour[i];
  70. cnt[h[x]]++;
  71. }
  72. }
  73.  
  74. cnt[h[u]]++;
  75. for(ii x : Q[u]) {
  76. int k, i; tie(k, i) = x;
  77. if (k + h[u] <= n) ans[i] = cnt[k + h[u]];
  78. }
  79.  
  80. if (keep == false) {
  81. FOR(i, tin[u], tout[u]) {
  82. int x = tour[i];
  83. cnt[h[x]]--;
  84. }
  85. }
  86. }
  87.  
  88. int acs(int k, int v) {
  89. RED(j, 20) if (BIT(k, j))
  90. v = par[v][j];
  91. return v;
  92. }
  93.  
  94. void init(void) {
  95. cin >> n;
  96. FOR(i, 1, n) {
  97. cin >> p[i];
  98. if (!p[i]) continue;
  99. G[p[i]].pb(i);
  100. par[i][0] = p[i];
  101. }
  102. cin >> q;
  103. FOR(j, 1, 19) FOR(i, 1, n) par[i][j] = par[par[i][j - 1]][j - 1];
  104. FOR(i, 1, q) {
  105. int v, k;
  106. cin >> v >> k;
  107. int u = acs(k, v);
  108. Q[u].pb(mp(k, i));
  109. }
  110. }
  111.  
  112. void process(void) {
  113. FOR(i, 1, n) if (p[i] == 0) {
  114. dfs(i);
  115. sack(i);
  116. }
  117. FOR(i, 1, q) cout << max(0, ans[i]-1) << ' ';
  118. }
  119.  
  120.  
  121. int main() {
  122. ios_base::sync_with_stdio(0);
  123. cin.tie(0); cout.tie(0);
  124. if (fopen(task".inp", "r")) {
  125. freopen(task".inp", "r", stdin);
  126. freopen(task".out", "w", stdout);
  127. }
  128. int tc = 1;
  129. // cin >> tc;
  130. while(tc--) {
  131. init();
  132. process();
  133. }
  134. return 0;
  135. }
  136.  
  137.  
Success #stdin #stdout 0.01s 30440KB
stdin
Standard input is empty
stdout
Standard output is empty