#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_pbds;
#pragma GCC optimize(2)
#define l int
int read(){
char x = getchar();
int val = 0;
while(x<'0'||x>'9')x=getchar();
while('0'<=x&&x<='9')
val = val*10+x-'0', x=getchar();
return val;
}
void print(int num){
if(num<0){
putchar('-');
num*=-1;
}
if(num>9) {
print(num/10);
}
putchar((num%10)^48);
}
using namespace std;
vector<l> adj[200001];
vector<pair<l,l>> arr[25001];
l region[200001], t, in[200001], out[200001], li[200001], sz[25001], lg[401];
unordered_map<int,int> ans1[401];
unordered_map<int,array<int,401>> ans2;
void merge(unordered_map<l, l> &a, unordered_map<l, l> b){
if(a.size()<b.size())
swap(a, b);
for(auto i: b)
a[i.first]+=i.second;
}
unordered_map<l,l> dfs(l n){
unordered_map<l,l> cur;
cur[region[n]]=1;
in[n]=t++;
arr[region[n]].push_back({in[n],1});
for(auto i: adj[n]){
merge(cur, dfs(i));
}
out[n]=t++;
arr[region[n]].push_back({out[n],0});
if(li[region[n]])
for(auto i: cur)
ans1[li[region[n]]][i.first]+=i.second;
else
for(auto i: lg)
ans2[region[n]][li[i]]+=cur[i];
return cur;
}
l query_s(int r1, int r2){
l sum=0, o=0, a=0, b=0;
vector<int> v;
while(a<arr[r1].size()||b<arr[r2].size()){
if(a<arr[r1].size()&&b<arr[r2].size()){
if(arr[r1][a]<arr[r2][b])
v.push_back(arr[r1][a].second*2-1), a++;
else
v.push_back(0), b++;
} else if(a<arr[r1].size())
v.push_back(arr[r1][a].second*2-1), a++;
else
v.push_back(0), b++;
}
for(auto i: v)
if(i)
o+=i;
else
sum+=o;
return sum/2;
}
int main(){
l n=read(), r=read(), q=read(), lr=0;
region[1]=read();
for(l i = 2; i <= n; i++){
adj[read()].push_back(i);
region[i] = read();
sz[region[i]]++;
}
for(int i = 1; i <= r; i++)
if(sz[i]>500)
lg[lr]=i, li[i]=++lr;
dfs(1);
while(q--){
int r1=read(), r2=read();
if(li[r1])
print(ans1[li[r1]][r2]),fflush(stdout);
else if(li[r2])
print(ans2[r1][li[r2]]),fflush(stdout);
else
print(query_s(r1, r2)),fflush(stdout);
}
return 0;
}
Compilation message
regions.cpp: In function 'int query_s(int, int)':
regions.cpp:57:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | while(a<arr[r1].size()||b<arr[r2].size()){
| ~^~~~~~~~~~~~~~~
regions.cpp:57:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | while(a<arr[r1].size()||b<arr[r2].size()){
| ~^~~~~~~~~~~~~~~
regions.cpp:58:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | if(a<arr[r1].size()&&b<arr[r2].size()){
| ~^~~~~~~~~~~~~~~
regions.cpp:58:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | if(a<arr[r1].size()&&b<arr[r2].size()){
| ~^~~~~~~~~~~~~~~
regions.cpp:63:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | } else if(a<arr[r1].size())
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2 ms |
8792 KB |
Time limit exceeded (wall clock) |
2 |
Execution timed out |
1 ms |
8792 KB |
Time limit exceeded (wall clock) |
3 |
Execution timed out |
2 ms |
8792 KB |
Time limit exceeded (wall clock) |
4 |
Execution timed out |
2 ms |
8792 KB |
Time limit exceeded (wall clock) |
5 |
Execution timed out |
4 ms |
8792 KB |
Time limit exceeded (wall clock) |
6 |
Execution timed out |
5 ms |
9304 KB |
Time limit exceeded (wall clock) |
7 |
Execution timed out |
8 ms |
9048 KB |
Time limit exceeded (wall clock) |
8 |
Execution timed out |
11 ms |
9304 KB |
Time limit exceeded (wall clock) |
9 |
Execution timed out |
22 ms |
10840 KB |
Time limit exceeded (wall clock) |
10 |
Execution timed out |
50 ms |
10548 KB |
Time limit exceeded (wall clock) |
11 |
Execution timed out |
72 ms |
10064 KB |
Time limit exceeded (wall clock) |
12 |
Execution timed out |
85 ms |
11652 KB |
Time limit exceeded (wall clock) |
13 |
Execution timed out |
106 ms |
10648 KB |
Time limit exceeded (wall clock) |
14 |
Execution timed out |
132 ms |
10816 KB |
Time limit exceeded (wall clock) |
15 |
Execution timed out |
158 ms |
22360 KB |
Time limit exceeded (wall clock) |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
220 ms |
17688 KB |
Time limit exceeded (wall clock) |
2 |
Execution timed out |
209 ms |
13068 KB |
Time limit exceeded (wall clock) |
3 |
Execution timed out |
293 ms |
22932 KB |
Time limit exceeded (wall clock) |
4 |
Execution timed out |
145 ms |
16820 KB |
Time limit exceeded (wall clock) |
5 |
Execution timed out |
176 ms |
20552 KB |
Time limit exceeded (wall clock) |
6 |
Execution timed out |
242 ms |
22864 KB |
Time limit exceeded (wall clock) |
7 |
Execution timed out |
338 ms |
30028 KB |
Time limit exceeded (wall clock) |
8 |
Execution timed out |
453 ms |
46396 KB |
Time limit exceeded (wall clock) |
9 |
Execution timed out |
790 ms |
44524 KB |
Time limit exceeded (wall clock) |
10 |
Execution timed out |
890 ms |
79652 KB |
Time limit exceeded (wall clock) |
11 |
Execution timed out |
1006 ms |
54612 KB |
Time limit exceeded (wall clock) |
12 |
Execution timed out |
1036 ms |
44720 KB |
Time limit exceeded (wall clock) |
13 |
Execution timed out |
1621 ms |
47408 KB |
Time limit exceeded (wall clock) |
14 |
Execution timed out |
2146 ms |
64196 KB |
Time limit exceeded (wall clock) |
15 |
Execution timed out |
7349 ms |
80524 KB |
Time limit exceeded (wall clock) |
16 |
Execution timed out |
8087 ms |
109232 KB |
Time limit exceeded |
17 |
Execution timed out |
8090 ms |
95164 KB |
Time limit exceeded |