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;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int maxn = 2e5 + 20;
const int maxk = 2.5e4 + 20;
int a[maxn];
vector<int> nodes[maxk];
vector<int> times[maxk];
vector<int> ch[maxn];
int tin[maxn];
int tout[maxn];
map<int, int> memo[maxk];
int n, k, q, t;
void dfs(int u) {
tin[u] = ++t;
nodes[a[u]].push_back(u);
times[a[u]].push_back(tin[u]);
for (auto v: ch[u]) {
dfs(v);
}
tout[u] = t;
}
int query(int x, int y) {
auto it = memo[x].find(y);
if (it != memo[x].end()) {
return it->second;
}
int res = 0;
for (auto u: nodes[x]) {
res += upper_bound(times[y].begin(), times[y].end(), tout[u]) - lower_bound(times[y].begin(), times[y].end(), tin[u]);
}
return (memo[x][y] = res);
}
void just_do_it() {
cin >> n >> k >> q;
cin >> a[1];
for (int v = 2; v <= n; v++) {
int u;
cin >> u >> a[v];
ch[u].push_back(v);
}
dfs(1);
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
cout << query(x, y) << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |