#include <bits/stdc++.h>
#define int long long
using namespace std;
const int sz=60;
int n,R,q,r1,r2,cnt[200001][sz+1],c[25001][sz+1],c2[25001][sz+1],r[200001],p[200001],tin[200001],tout[200001],t,C[25001],pos[25001];
vector <int> ke[200001],ve[25001],V;
void dfs(int u){
tin[u]=++t;
ve[r[u]].push_back(t);
if (C[r[u]]>sz)
cnt[u][pos[r[u]]]++;
for (int v:ke[u]){
for (int i=1;i<=sz;i++)
cnt[v][i]+=cnt[u][i];
dfs(v);
}
for (int i=1;i<=sz;i++)
c[r[u]][i]+=cnt[u][i];
tout[u]=++t;
ve[r[u]].push_back(-t);
}
void dfs2(int u){
if (C[r[u]]>sz)
cnt[u][pos[r[u]]]++;
for (int v:ke[u]){
dfs2(v);
for (int i=1;i<=sz;i++)
cnt[u][i]+=cnt[v][i];
}
for (int i=1;i<=sz;i++)
c2[r[u]][i]+=cnt[u][i];
}
int32_t main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
freopen("regions.in","r",stdin);
freopen("regions.out","w",stdout);
cin >> n >> R >> q;
for (int i=1;i<=n;i++){
if (i>1){
cin >> p[i];
ke[p[i]].push_back(i);
}
cin >> r[i];
C[r[i]]++;
}
for (int i=1;i<=R;i++)
V.push_back(i);
sort(V.begin(),V.end(),[](int i, int j){return C[i]>C[j];});
for (int i=0;i<V.size();i++)
pos[V[i]]=i+1;
dfs(1);
memset(cnt,0,sizeof(cnt));
dfs2(1);
while (q--){
cin >> r1 >> r2;
if (C[r1]>sz){
cout << c[r2][pos[r1]] << endl;
continue;
}
if (C[r2]>sz){
cout << c2[r1][pos[r2]] << endl;
continue;
}
int j=0,cnt=0,res=0;
for (int i=0;i<ve[r2].size();i++){
while (j<ve[r1].size()){
if (abs(ve[r1][j])>abs(ve[r2][i]))
break;
cnt+=(ve[r1][j]>0)-(ve[r1][j]<0);
j++;
}
if (ve[r2][i]>0)
res+=cnt;
}
cout << res << endl;
}
}
Compilation message
regions.cpp: In function 'int32_t main()':
regions.cpp:49:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i=0;i<V.size();i++)
| ~^~~~~~~~~
regions.cpp:65:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i=0;i<ve[r2].size();i++){
| ~^~~~~~~~~~~~~~
regions.cpp:66:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | while (j<ve[r1].size()){
| ~^~~~~~~~~~~~~~
regions.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen("regions.in","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen("regions.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
108376 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
15 ms |
108372 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
14 ms |
108376 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
15 ms |
108320 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
15 ms |
108376 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
14 ms |
108376 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
14 ms |
108492 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
15 ms |
108376 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
15 ms |
108420 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
15 ms |
108376 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
15 ms |
108376 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
14 ms |
108376 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
14 ms |
108376 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
15 ms |
108376 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
14 ms |
108376 KB |
Unexpected end of file - int32 expected |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
108376 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
14 ms |
108376 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
15 ms |
108376 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
14 ms |
108376 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
15 ms |
108376 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
16 ms |
108376 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
15 ms |
108376 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
15 ms |
108368 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
15 ms |
108632 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
15 ms |
108316 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
15 ms |
108376 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
14 ms |
108376 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
16 ms |
108376 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
14 ms |
108376 KB |
Unexpected end of file - int32 expected |
15 |
Incorrect |
15 ms |
108392 KB |
Unexpected end of file - int32 expected |
16 |
Incorrect |
14 ms |
108376 KB |
Unexpected end of file - int32 expected |
17 |
Incorrect |
15 ms |
108376 KB |
Unexpected end of file - int32 expected |