#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
const int mxN = 2e5 + 10;
const int mxR = 501;
vector<int> adj[mxN], rnode[mxN];
pbds region[mxR];
int r[mxN], st[mxN], en[mxN], tin = -1;
void dfs(int u = 1){
st[u] = ++tin;
for(auto it : adj[u]){
dfs(it);
}
en[u] = tin;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(nullptr); cout.tie(nullptr);
int n, k, q;
cin >> n >> k >> q >> r[1];
for(int i = 2, p; i <= n; ++i){
cin >> p >> r[i];
adj[p].push_back(i);
}
dfs();
for(int i = 1; i <= n; ++i){
region[r[i]].insert(st[i]);
rnode[r[i]].push_back(i);
}
while(q--){
int r1, r2;
cin >> r1 >> r2;
int ans = 0;
for(auto it : rnode[r1]){
ans += region[r2].order_of_key(en[it] + 1) - region[r2].order_of_key(st[it]);
}
cout << ans << endl;
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |