#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200200;
const int R = 25025;
const int B = 1080;
int T;
int tin[N];
int tout[N];
vector<int> g[N];
int rg[N];
int m;
vector<int> heavy;
vector<int> ver[R];
vector<int> seg[R];
void precalc(int s) {
tin[s] = ++T;
seg[rg[s]].push_back(tin[s]);
for (int to : g[s])
precalc(to);
tout[s] = ++T;
}
vector<vector<ll>> heavy_ans;
vector<ll> light_ans[N];
int lst[R];
int cnt[N];
bool isheavy(int r) {
return ((int)seg[r].size() > B);
}
void dfs(int s) {
for (int i = 0; i < m; i++) {
int c = heavy[i];
heavy_ans[i][c] += cnt[lst[c]];
}
int x = lst[rg[s]];
cnt[s] = cnt[lst[rg[s]]] + 1;
lst[rg[s]] = s;
light_ans[s].resize(m);
if (isheavy(rg[s])) {
int c = lower_bound(heavy.begin(), heavy.end(), rg[s]) - heavy.begin();
light_ans[s][c]++;
}
for (int to : g[s]) {
dfs(to);
for (int i = 0; i < m; i++)
light_ans[s][i] += light_ans[to][i];
}
lst[rg[s]] = x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, regions, q;
cin >> n >> regions >> q;
for (int i = 1; i <= n; i++) {
int p;
if (i > 1) {
cin >> p;
g[p].push_back(i);
}
cin >> rg[i];
ver[rg[i]].push_back(i);
}
precalc(1);
for (int i = 1; i <= regions; i++) {
if (isheavy(i)) {
heavy.push_back(i);
heavy_ans.push_back(vector<ll>(regions + 1));
m++;
}
sort(seg[i].begin(), seg[i].end());
}
dfs(1);
while (q--) {
int r1, r2;
cin >> r1 >> r2;
int ans = 0;
if (isheavy(r1)) {
int c = lower_bound(heavy.begin(), heavy.end(), r1) - heavy.begin();
ans = heavy_ans[c][r2];
} else if (isheavy(r2)) {
int c = lower_bound(heavy.begin(), heavy.end(), r2) - heavy.begin();
for (int v : ver[r1]) {
ans += light_ans[v][c];
}
} else {
for (int v : ver[r1]) {
int l, r;
l = upper_bound(seg[r2].begin(), seg[r2].end(), tin[v]) - seg[r2].begin();
r = upper_bound(seg[r2].begin(), seg[r2].end(), tout[v]) - seg[r2].begin();
ans += max(r - l, 0);
}
}
cout << ans << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12632 KB |
Output is correct |
3 |
Correct |
3 ms |
12632 KB |
Output is correct |
4 |
Correct |
5 ms |
12632 KB |
Output is correct |
5 |
Correct |
6 ms |
12632 KB |
Output is correct |
6 |
Correct |
13 ms |
12888 KB |
Output is correct |
7 |
Correct |
20 ms |
12628 KB |
Output is correct |
8 |
Correct |
22 ms |
12888 KB |
Output is correct |
9 |
Correct |
32 ms |
13304 KB |
Output is correct |
10 |
Correct |
57 ms |
13144 KB |
Output is correct |
11 |
Correct |
99 ms |
13400 KB |
Output is correct |
12 |
Correct |
104 ms |
14168 KB |
Output is correct |
13 |
Correct |
139 ms |
13656 KB |
Output is correct |
14 |
Correct |
201 ms |
14168 KB |
Output is correct |
15 |
Correct |
205 ms |
18008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1199 ms |
19800 KB |
Output isn't correct |
2 |
Incorrect |
1100 ms |
22156 KB |
Output isn't correct |
3 |
Incorrect |
2029 ms |
28276 KB |
Output isn't correct |
4 |
Correct |
214 ms |
14168 KB |
Output is correct |
5 |
Correct |
275 ms |
16468 KB |
Output is correct |
6 |
Correct |
1140 ms |
15436 KB |
Output is correct |
7 |
Correct |
1483 ms |
16472 KB |
Output is correct |
8 |
Correct |
1076 ms |
23384 KB |
Output is correct |
9 |
Correct |
1956 ms |
22136 KB |
Output is correct |
10 |
Correct |
3911 ms |
29152 KB |
Output is correct |
11 |
Correct |
3642 ms |
21296 KB |
Output is correct |
12 |
Incorrect |
1206 ms |
28880 KB |
Output isn't correct |
13 |
Incorrect |
1539 ms |
29576 KB |
Output isn't correct |
14 |
Incorrect |
2001 ms |
46632 KB |
Output isn't correct |
15 |
Incorrect |
2717 ms |
35696 KB |
Output isn't correct |
16 |
Incorrect |
2593 ms |
44880 KB |
Output isn't correct |
17 |
Incorrect |
2485 ms |
63556 KB |
Output isn't correct |