This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5, MAXK = 2.5e4, SQRT = 250;
int k[MAXN], cnt[MAXK], t[MAXN], s[MAXN], dfsTimer = 0, cur, c;
vector<int> g[MAXN], a[MAXN], res[MAXK], ord[MAXK];
void dfs0(int u){
t[u] = dfsTimer++, s[u] = 1;
ord[k[u]].push_back(t[u]);
for(int v : g[u]) dfs0(v), s[u] += s[v];
}
void dfs1(int u){
if(k[u] == cur) ++c;
res[cur][k[u]] += c;
for(int v : g[u]) dfs1(v);
if(k[u] == cur) --c;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n, r, q, x, y; cin >> n >> r >> q;
for(int i=0, j; i<n; ++i){
if(i) cin >> j, g[j-1].push_back(i);
cin >> k[i], a[--k[i]].push_back(i);
}
dfs0(0);
for(int i=0; i<r; ++i){
if(a[i].size() < SQRT) continue;
cur = i, c = 0;
res[i].resize(r);
dfs1(0);
}
while(q--){
cin >> x >> y; --x, --y;
if(a[x].size() < SQRT){
int ans = 0;
for(int u : a[x]){
ans += upper_bound(ord[y].begin(), ord[y].end(), t[u]+s[u]-1)
- lower_bound(ord[y].begin(), ord[y].end(), t[u]);
}
cout << ans << endl;
}else{
cout << res[x][y] << endl;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |