#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> son1[200000];
vector<int> son2[200000];
int ind1[262144];
int sz1[200000];
int ind2[262144];
int rev2[200000];
int sz2[200000];
int f1=0;
int f2=0;
void dfs1(int v,int prev) {
ind1[v]=f1++;
sz1[v]=1;
for(int nt:son1[v]) {
dfs1(nt,v);
sz1[v]+=sz1[nt];
}
}
void dfs2(int v,int prev) {
ind2[v]=f2++;
rev2[ind2[v]]=v;
sz2[v]=1;
for(int nt:son2[v]) {
dfs2(nt,v);
sz2[v]+=sz2[nt];
}
}
const int sz=262144;
vector<int> v[sz*2];
void init(int node=1,int nodel=0,int noder=sz-1) {
for(int i=nodel;i<=noder;i++) {
if (i<n)
v[node].push_back(ind1[rev2[i]]);
}
if (nodel==noder) {
return;
}
sort(v[node].begin(),v[node].end());
int mid=(nodel+noder)/2;
init(node*2,nodel,mid);
init(node*2+1,mid+1,noder);
}
int query(int l,int r,int d,int u,int node=1,int nodel=0,int noder=sz-1) {
if (r<nodel||l>noder) {
return 0;
}
if (l<=nodel&&noder<=r) {
return upper_bound(v[node].begin(),v[node].end(),u)-lower_bound(v[node].begin(),v[node].end(),d);
}
int mid=(nodel+noder)/2;
return query(l,r,d,u,node*2,nodel,mid)+query(l,r,d,u,node*2+1,mid+1,noder);
}
int main(void) {
scanf("%d",&n);
for(int i=1;i<n;i++) {
int p;
scanf("%d",&p);
p--;
son1[p].push_back(i);
}
for(int i=1;i<n;i++) {
int p;
scanf("%d",&p);
p--;
son2[p].push_back(i);
}
dfs1(0,-1);
dfs2(0,-1);
init();
for(int i=0;i<n;i++) {
printf("%d ",query(ind2[i],ind2[i]+sz2[i]-1,ind1[i],ind1[i]+sz1[i]-1)-1);
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Main.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d",&p);
| ~~~~~^~~~~~~~~
Main.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d",&p);
| ~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
21964 KB |
Output is correct |
2 |
Correct |
19 ms |
21948 KB |
Output is correct |
3 |
Correct |
19 ms |
22060 KB |
Output is correct |
4 |
Correct |
20 ms |
22156 KB |
Output is correct |
5 |
Correct |
20 ms |
21964 KB |
Output is correct |
6 |
Correct |
22 ms |
22052 KB |
Output is correct |
7 |
Correct |
27 ms |
21964 KB |
Output is correct |
8 |
Correct |
22 ms |
22220 KB |
Output is correct |
9 |
Correct |
27 ms |
22220 KB |
Output is correct |
10 |
Correct |
23 ms |
22240 KB |
Output is correct |
11 |
Correct |
22 ms |
22224 KB |
Output is correct |
12 |
Correct |
21 ms |
22264 KB |
Output is correct |
13 |
Correct |
21 ms |
22168 KB |
Output is correct |
14 |
Correct |
22 ms |
22212 KB |
Output is correct |
15 |
Correct |
25 ms |
22348 KB |
Output is correct |
16 |
Correct |
25 ms |
22464 KB |
Output is correct |
17 |
Correct |
28 ms |
22452 KB |
Output is correct |
18 |
Correct |
23 ms |
22432 KB |
Output is correct |
19 |
Correct |
22 ms |
22476 KB |
Output is correct |
20 |
Correct |
30 ms |
22476 KB |
Output is correct |
21 |
Correct |
24 ms |
22476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
21964 KB |
Output is correct |
2 |
Correct |
19 ms |
21948 KB |
Output is correct |
3 |
Correct |
19 ms |
22060 KB |
Output is correct |
4 |
Correct |
20 ms |
22156 KB |
Output is correct |
5 |
Correct |
20 ms |
21964 KB |
Output is correct |
6 |
Correct |
22 ms |
22052 KB |
Output is correct |
7 |
Correct |
27 ms |
21964 KB |
Output is correct |
8 |
Correct |
22 ms |
22220 KB |
Output is correct |
9 |
Correct |
27 ms |
22220 KB |
Output is correct |
10 |
Correct |
23 ms |
22240 KB |
Output is correct |
11 |
Correct |
22 ms |
22224 KB |
Output is correct |
12 |
Correct |
21 ms |
22264 KB |
Output is correct |
13 |
Correct |
21 ms |
22168 KB |
Output is correct |
14 |
Correct |
22 ms |
22212 KB |
Output is correct |
15 |
Correct |
25 ms |
22348 KB |
Output is correct |
16 |
Correct |
25 ms |
22464 KB |
Output is correct |
17 |
Correct |
28 ms |
22452 KB |
Output is correct |
18 |
Correct |
23 ms |
22432 KB |
Output is correct |
19 |
Correct |
22 ms |
22476 KB |
Output is correct |
20 |
Correct |
30 ms |
22476 KB |
Output is correct |
21 |
Correct |
24 ms |
22476 KB |
Output is correct |
22 |
Correct |
403 ms |
42544 KB |
Output is correct |
23 |
Correct |
442 ms |
44416 KB |
Output is correct |
24 |
Correct |
494 ms |
46676 KB |
Output is correct |
25 |
Correct |
322 ms |
42644 KB |
Output is correct |
26 |
Correct |
306 ms |
49244 KB |
Output is correct |
27 |
Correct |
331 ms |
49120 KB |
Output is correct |
28 |
Correct |
326 ms |
49184 KB |
Output is correct |
29 |
Correct |
893 ms |
60988 KB |
Output is correct |
30 |
Execution timed out |
1095 ms |
64588 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |