#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int LIM = 500;
int in[200005];
int out[200005];
int region[200005];
int parent[200005];
int cnt[200005];
vector <int> graf[200005];
int gore[25005][505];
int dole[25005][505];
int sada[25005];
int rev[25005];
int compress[25005];
int ccomp;
int r;
int tajm;
vector <pair <int, int>> svi[25005];
vector <int> ins[25005];
void dfs(int v){
in[v] = ++tajm;
sada[region[v]]++;
if(compress[region[v]]){
for(int i=1; i<=r; i++){
dole[i][compress[region[v]]] += sada[region[v]];
}
dole[region[v]][compress[region[v]]]--;
gore[region[v]][compress[region[v]]]--;
}
for(int i=1; i<=ccomp; i++){
gore[region[v]][i] += sada[rev[i]];
}
for(auto c : graf[v]){
dfs(c);
}
sada[region[v]]--;
out[v] = tajm;
svi[region[v]].push_back({in[v], 1});
svi[region[v]].push_back({out[v]+1, -1});
ins[region[v]].push_back(in[v]);
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int n, q;
cin >> n >> r >> q;
cin >> region[1];
cnt[region[1]]++;
for(int i=2; i<=n; i++){
cin >> parent[i] >> region[i];
cnt[region[i]]++;
graf[parent[i]].push_back(i);
}
for(int i=1; i<=r; i++){
if(cnt[i] <= LIM) continue;
compress[i] = ++ccomp;
rev[ccomp] = i;
}
dfs(1);
return 0;
for(int i=1; i<=r; i++) sort(svi[i].begin(), svi[i].end());
for(int i=1; i<=r; i++) sort(ins[i].begin(), ins[i].end());
while(q--){
int a, b;
cin >> a >> b;
if(cnt[a] > LIM){
cout << gore[b][compress[a]] << endl;
continue;
}
if(cnt[b] > LIM){
cout << dole[a][compress[b]] << endl;
continue;
}
int k = svi[a].size();
int j = -1;
int tren = 0;
ll res = 0;
for(auto c : ins[b]){
while(j < k-1 && c >= svi[a][j+1].first){
j++;
tren += svi[a][j].second;
}
res += tren;
}
cout << res << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
6272 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
4 ms |
6272 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
4 ms |
6272 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
4 ms |
6272 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
5 ms |
6272 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
5 ms |
6400 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
5 ms |
6400 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
5 ms |
6400 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
7 ms |
6912 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
8 ms |
7040 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
10 ms |
7424 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
13 ms |
8036 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
15 ms |
7936 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
16 ms |
8704 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
18 ms |
11392 KB |
Unexpected end of file - int32 expected |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
13116 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
50 ms |
12272 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
55 ms |
15476 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
18 ms |
8576 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
19 ms |
10240 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
29 ms |
10104 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
50 ms |
11672 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
44 ms |
16888 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
92 ms |
19308 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
81 ms |
23540 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
122 ms |
19832 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
3362 ms |
83512 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
3362 ms |
83676 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
7932 ms |
99164 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
5663 ms |
124064 KB |
Unexpected end of file - int32 expected |
16 |
Incorrect |
4760 ms |
129044 KB |
Unexpected end of file - int32 expected |
17 |
Incorrect |
7559 ms |
108068 KB |
Unexpected end of file - int32 expected |