#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
vector<int> adj[300005], reg[25005], regst[25005];
int st[300005], en[300005], timer = 0;
void dfs(int i, int p) {
st[i] = timer;
timer++;
for (int j : adj[i]) {
if (j == p) {
continue;
}
dfs(j, i);
}
en[i] = timer;
}
void solve() {
int n, r, q;
cin >> n >> r >> q;
for (int i = 1; i <= n; i++) {
int s, h;
if (i == 1) {
cin >> h;
reg[h].push_back(i);
} else {
cin >> s >> h;
reg[h].push_back(i);
adj[s].push_back(i);
adj[i].push_back(s);
}
}
dfs(1, -1);
for (int i = 1; i <= r; i++) {
for (int j : reg[i]) {
regst[i].push_back(st[j]);
}
sort(regst[i].begin(), regst[i].end());
}
while(q--) {
int a, b, ans = 0;
cin >> a >> b;
for (int i : reg[a]) {
int l, r;
l = upper_bound(regst[b].begin(), regst[b].end(), st[i]) - regst[b].begin();
r = lower_bound(regst[b].begin(), regst[b].end(), en[i]) - regst[b].begin();
ans += r-l;
}
cout << ans << endl;
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
t = 1;
while(t--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
3 ms |
10840 KB |
Output is correct |
4 |
Correct |
5 ms |
10840 KB |
Output is correct |
5 |
Correct |
7 ms |
10840 KB |
Output is correct |
6 |
Correct |
11 ms |
10840 KB |
Output is correct |
7 |
Correct |
18 ms |
10840 KB |
Output is correct |
8 |
Correct |
22 ms |
10840 KB |
Output is correct |
9 |
Correct |
31 ms |
11096 KB |
Output is correct |
10 |
Correct |
49 ms |
11096 KB |
Output is correct |
11 |
Correct |
88 ms |
11352 KB |
Output is correct |
12 |
Correct |
107 ms |
11864 KB |
Output is correct |
13 |
Correct |
135 ms |
11864 KB |
Output is correct |
14 |
Correct |
211 ms |
12120 KB |
Output is correct |
15 |
Correct |
217 ms |
13912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
8029 ms |
14424 KB |
Time limit exceeded |
2 |
Execution timed out |
8048 ms |
14424 KB |
Time limit exceeded |
3 |
Execution timed out |
8042 ms |
15696 KB |
Time limit exceeded |
4 |
Correct |
190 ms |
12376 KB |
Output is correct |
5 |
Correct |
235 ms |
13444 KB |
Output is correct |
6 |
Correct |
1202 ms |
13388 KB |
Output is correct |
7 |
Correct |
1459 ms |
14424 KB |
Output is correct |
8 |
Correct |
1048 ms |
17756 KB |
Output is correct |
9 |
Correct |
2003 ms |
18768 KB |
Output is correct |
10 |
Correct |
3770 ms |
21996 KB |
Output is correct |
11 |
Correct |
3626 ms |
20560 KB |
Output is correct |
12 |
Correct |
5059 ms |
19280 KB |
Output is correct |
13 |
Correct |
5331 ms |
19796 KB |
Output is correct |
14 |
Correct |
7359 ms |
20308 KB |
Output is correct |
15 |
Correct |
7754 ms |
22664 KB |
Output is correct |
16 |
Correct |
5869 ms |
26192 KB |
Output is correct |
17 |
Execution timed out |
8045 ms |
25356 KB |
Time limit exceeded |