#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()
#define ii pair<int, int>
#define pb push_back
#define vi vector<int>
#define vvi vector<int>
#define vii vector<ii>
const int N = 200000, R1 = 25000, R2 = 500;
int n, r, q, curr;
int reg[N], sz[N], pre[N], idx[N];
ll ans[R2][R2];
vi adj[N], from[R1], pos[R1];
void dfs(int u) {
sz[u] = 1;
pre[curr] = reg[u];
pos[reg[u]].pb(curr);
idx[u] = curr;
curr++;
for (int &v : adj[u]) {
dfs(v);
sz[u] += sz[v];
}
}
int query(int a, int b, int c) {
int l = lower_bound(all(pos[c]), a) - pos[c].begin();
int r = upper_bound(all(pos[c]), b) - pos[c].begin();
return r-l;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> r >> q;
int mx = 0;
for (int i = 0; i < n; i++) {
if (i) {
int x;
cin >> x;
x--;
adj[x].pb(i);
}
cin >> reg[i];
reg[i]--;
from[reg[i]].pb(i);
mx = max(mx, (int)from[reg[i]].size());
}
curr = 0;
dfs(0);
if (r <= R2) {
// precalc all queries
for (int i = 0; i < r; i++) {
for (int j = 0; j < r; j++) {
if (i == j) continue;
ans[i][j] = 0;
for (int &u : from[i]) {
ans[i][j] += query(idx[u], idx[u] + sz[u] - 1, j);
}
}
}
}
else {
assert(mx <= R2);
}
while (q--) {
int x, y;
cin >> x >> y;
x--; y--;
if (r <= R2) {
cout << ans[x][y] << endl;
}
else {
ll cnt = 0;
for (int &u : from[x]) {
cnt += query(idx[u], idx[u] + sz[u] - 1, y);
}
cout << cnt << endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6224 KB |
Output is correct |
2 |
Correct |
4 ms |
6224 KB |
Output is correct |
3 |
Correct |
6 ms |
6224 KB |
Output is correct |
4 |
Correct |
6 ms |
6352 KB |
Output is correct |
5 |
Correct |
15 ms |
6352 KB |
Output is correct |
6 |
Correct |
14 ms |
7248 KB |
Output is correct |
7 |
Correct |
36 ms |
6896 KB |
Output is correct |
8 |
Correct |
44 ms |
7120 KB |
Output is correct |
9 |
Correct |
78 ms |
7884 KB |
Output is correct |
10 |
Correct |
210 ms |
8380 KB |
Output is correct |
11 |
Correct |
279 ms |
8144 KB |
Output is correct |
12 |
Correct |
499 ms |
9348 KB |
Output is correct |
13 |
Correct |
556 ms |
8680 KB |
Output is correct |
14 |
Correct |
556 ms |
8604 KB |
Output is correct |
15 |
Correct |
460 ms |
11452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1458 ms |
11916 KB |
Output is correct |
2 |
Correct |
1717 ms |
10852 KB |
Output is correct |
3 |
Correct |
2186 ms |
14160 KB |
Output is correct |
4 |
Correct |
255 ms |
7992 KB |
Output is correct |
5 |
Correct |
338 ms |
9664 KB |
Output is correct |
6 |
Correct |
1576 ms |
9252 KB |
Output is correct |
7 |
Correct |
1768 ms |
10332 KB |
Output is correct |
8 |
Correct |
1318 ms |
15560 KB |
Output is correct |
9 |
Correct |
2279 ms |
16204 KB |
Output is correct |
10 |
Correct |
4208 ms |
21168 KB |
Output is correct |
11 |
Correct |
4697 ms |
16280 KB |
Output is correct |
12 |
Runtime error |
98 ms |
35536 KB |
Execution killed with signal 6 |
13 |
Runtime error |
83 ms |
35936 KB |
Execution killed with signal 6 |
14 |
Runtime error |
100 ms |
35676 KB |
Execution killed with signal 6 |
15 |
Runtime error |
89 ms |
44496 KB |
Execution killed with signal 6 |
16 |
Runtime error |
92 ms |
55264 KB |
Execution killed with signal 6 |
17 |
Runtime error |
95 ms |
52924 KB |
Execution killed with signal 6 |