#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Fenwick{
int n;
int tree[200002];
void add(int x, int y){
while(x<=n){
tree[x] += y;
x += x&-x;
}
}
int sum(int x){
int ret = 0;
while(x){
ret += tree[x];
x -= x&-x;
}
return ret;
}
int sum(int l, int r){
return sum(r) - sum(l-1);
}
} tree;
struct Query{
int x1, x2, y, idx, w;
Query(){}
Query(int x1, int x2, int y, int idx, int w): x1(x1), x2(x2), y(y), idx(idx), w(w){}
bool operator<(const Query &r)const{
return y<r.y;
}
};
int n;
int par1[200002], par2[200002];
vector<int> link1[200002], link2[200002];
int in1[200002], in2[200002], inCnt;
int out1[200002], out2[200002];
int ans[200002];
int arr[200002];
void dfs1(int x){
in1[x] = ++inCnt;
out1[x] = in1[x];
for(auto y: link1[x]){
dfs1(y);
out1[x] = out1[y];
}
}
void dfs2(int x){
in2[x] = ++inCnt;
out2[x] = in2[x];
for(auto y: link2[x]){
dfs2(y);
out2[x] = out2[y];
}
}
int main(){
scanf("%d", &n);
for(int i=2; i<=n; i++){
scanf("%d", &par1[i]);
link1[par1[i]].push_back(i);
}
for(int i=2; i<=n; i++){
scanf("%d", &par2[i]);
link2[par2[i]].push_back(i);
}
dfs1(1);
inCnt = 0;
dfs2(1);
vector<Query> vec;
for(int i=1; i<=n; i++){
arr[in1[i]] = in2[i];
vec.push_back(Query(in2[i]+1, out2[i], in1[i], i, -1));
vec.push_back(Query(in2[i]+1, out2[i], out1[i], i, 1));
}
sort(vec.begin(), vec.end());
int pnt = 0;
tree.n = n;
for(int i=1; i<=n; i++){
tree.add(arr[i], 1);
while(pnt < (int)vec.size() && vec[pnt].y <= i){
ans[vec[pnt].idx] += vec[pnt].w * tree.sum(vec[pnt].x1, vec[pnt].x2);
pnt++;
}
}
for(int i=1; i<=n; i++){
printf("%d ", ans[i]);
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Main.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%d", &par1[i]);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%d", &par2[i]);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9680 KB |
Output is correct |
4 |
Correct |
8 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
8 ms |
9804 KB |
Output is correct |
9 |
Correct |
7 ms |
9804 KB |
Output is correct |
10 |
Correct |
7 ms |
9804 KB |
Output is correct |
11 |
Correct |
7 ms |
9804 KB |
Output is correct |
12 |
Correct |
8 ms |
9932 KB |
Output is correct |
13 |
Correct |
7 ms |
9932 KB |
Output is correct |
14 |
Correct |
7 ms |
9932 KB |
Output is correct |
15 |
Correct |
9 ms |
10060 KB |
Output is correct |
16 |
Correct |
9 ms |
10076 KB |
Output is correct |
17 |
Correct |
9 ms |
10060 KB |
Output is correct |
18 |
Correct |
8 ms |
10060 KB |
Output is correct |
19 |
Correct |
8 ms |
10124 KB |
Output is correct |
20 |
Correct |
8 ms |
10156 KB |
Output is correct |
21 |
Correct |
8 ms |
10160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9680 KB |
Output is correct |
4 |
Correct |
8 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
8 ms |
9804 KB |
Output is correct |
9 |
Correct |
7 ms |
9804 KB |
Output is correct |
10 |
Correct |
7 ms |
9804 KB |
Output is correct |
11 |
Correct |
7 ms |
9804 KB |
Output is correct |
12 |
Correct |
8 ms |
9932 KB |
Output is correct |
13 |
Correct |
7 ms |
9932 KB |
Output is correct |
14 |
Correct |
7 ms |
9932 KB |
Output is correct |
15 |
Correct |
9 ms |
10060 KB |
Output is correct |
16 |
Correct |
9 ms |
10076 KB |
Output is correct |
17 |
Correct |
9 ms |
10060 KB |
Output is correct |
18 |
Correct |
8 ms |
10060 KB |
Output is correct |
19 |
Correct |
8 ms |
10124 KB |
Output is correct |
20 |
Correct |
8 ms |
10156 KB |
Output is correct |
21 |
Correct |
8 ms |
10160 KB |
Output is correct |
22 |
Correct |
124 ms |
23244 KB |
Output is correct |
23 |
Correct |
138 ms |
24756 KB |
Output is correct |
24 |
Correct |
145 ms |
26592 KB |
Output is correct |
25 |
Correct |
140 ms |
24020 KB |
Output is correct |
26 |
Correct |
145 ms |
28716 KB |
Output is correct |
27 |
Correct |
182 ms |
28764 KB |
Output is correct |
28 |
Correct |
138 ms |
28708 KB |
Output is correct |
29 |
Correct |
297 ms |
35836 KB |
Output is correct |
30 |
Correct |
323 ms |
38656 KB |
Output is correct |
31 |
Correct |
336 ms |
42084 KB |
Output is correct |
32 |
Correct |
333 ms |
37184 KB |
Output is correct |
33 |
Correct |
323 ms |
46128 KB |
Output is correct |
34 |
Correct |
347 ms |
46020 KB |
Output is correct |
35 |
Correct |
333 ms |
46176 KB |
Output is correct |
36 |
Correct |
316 ms |
36528 KB |
Output is correct |
37 |
Correct |
372 ms |
39652 KB |
Output is correct |
38 |
Correct |
322 ms |
43368 KB |
Output is correct |
39 |
Correct |
329 ms |
38064 KB |
Output is correct |
40 |
Correct |
331 ms |
47832 KB |
Output is correct |
41 |
Correct |
359 ms |
47768 KB |
Output is correct |
42 |
Correct |
373 ms |
47744 KB |
Output is correct |