#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define MOD 1000000007
#define INF (1ll<<62)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
int N, num;
int X[202020], Y[202020];
int RX[202020], RY[202020];
int T[202020], ans[202020];
vector<int> adjX[202020], adjY[202020];
void dfsX(int u){
X[u] = ++num;
for (int v : adjX[u]) dfsX(v);
RX[u] = num;
}
void dfsY(int u){
Y[u] = ++num;
for (int v : adjY[u]) dfsY(v);
RY[u] = num;
}
struct Event{int x, y1, y2, t;};
vector<Event> E;
void upd(int k){while(k<=N)T[k]++,k+=k&-k;}
int f(int k){int r=0;while(k)r+=T[k],k^=k&-k;return r;}
int main(){
int p;
scanf("%d", &N);
for (int i=2; i<=N; i++){
scanf("%d", &p);
adjX[p].pb(i);
}
for (int i=2; i<=N; i++){
scanf("%d", &p);
adjY[p].pb(i);
}
dfsX(1), num=0, dfsY(1);
for (int i=1; i<=N; i++){
E.pb((Event){X[i], Y[i], 0, 0});
E.pb((Event){X[i]-1, Y[i], RY[i], -i});
E.pb((Event){RX[i], Y[i], RY[i], i});
}
sort(E.begin(), E.end(), [&](Event a, Event b){
if (a.x != b.x) return a.x < b.x;
return !!a.t < !!b.t;
});
for (Event e : E){
if (!e.t) upd(e.y1);
else if (e.t < 0) ans[-e.t] += f(e.y1-1) - f(e.y2);
else ans[e.t] += f(e.y2) - f(e.y1-1);
}
for (int i=1; i<=N; i++) printf("%d ", ans[i]-1);
puts("");
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
Main.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d", &p);
| ~~~~~^~~~~~~~~~
Main.cpp:44:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d", &p);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9804 KB |
Output is correct |
2 |
Correct |
6 ms |
9804 KB |
Output is correct |
3 |
Correct |
6 ms |
9804 KB |
Output is correct |
4 |
Correct |
7 ms |
9804 KB |
Output is correct |
5 |
Correct |
6 ms |
9804 KB |
Output is correct |
6 |
Correct |
8 ms |
9804 KB |
Output is correct |
7 |
Correct |
7 ms |
9836 KB |
Output is correct |
8 |
Correct |
7 ms |
9932 KB |
Output is correct |
9 |
Correct |
9 ms |
9984 KB |
Output is correct |
10 |
Correct |
7 ms |
9932 KB |
Output is correct |
11 |
Correct |
7 ms |
9932 KB |
Output is correct |
12 |
Correct |
7 ms |
9932 KB |
Output is correct |
13 |
Correct |
9 ms |
9932 KB |
Output is correct |
14 |
Correct |
9 ms |
9932 KB |
Output is correct |
15 |
Correct |
9 ms |
10088 KB |
Output is correct |
16 |
Correct |
8 ms |
10188 KB |
Output is correct |
17 |
Correct |
8 ms |
10188 KB |
Output is correct |
18 |
Correct |
8 ms |
10200 KB |
Output is correct |
19 |
Correct |
8 ms |
10188 KB |
Output is correct |
20 |
Correct |
12 ms |
10188 KB |
Output is correct |
21 |
Correct |
10 ms |
10188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9804 KB |
Output is correct |
2 |
Correct |
6 ms |
9804 KB |
Output is correct |
3 |
Correct |
6 ms |
9804 KB |
Output is correct |
4 |
Correct |
7 ms |
9804 KB |
Output is correct |
5 |
Correct |
6 ms |
9804 KB |
Output is correct |
6 |
Correct |
8 ms |
9804 KB |
Output is correct |
7 |
Correct |
7 ms |
9836 KB |
Output is correct |
8 |
Correct |
7 ms |
9932 KB |
Output is correct |
9 |
Correct |
9 ms |
9984 KB |
Output is correct |
10 |
Correct |
7 ms |
9932 KB |
Output is correct |
11 |
Correct |
7 ms |
9932 KB |
Output is correct |
12 |
Correct |
7 ms |
9932 KB |
Output is correct |
13 |
Correct |
9 ms |
9932 KB |
Output is correct |
14 |
Correct |
9 ms |
9932 KB |
Output is correct |
15 |
Correct |
9 ms |
10088 KB |
Output is correct |
16 |
Correct |
8 ms |
10188 KB |
Output is correct |
17 |
Correct |
8 ms |
10188 KB |
Output is correct |
18 |
Correct |
8 ms |
10200 KB |
Output is correct |
19 |
Correct |
8 ms |
10188 KB |
Output is correct |
20 |
Correct |
12 ms |
10188 KB |
Output is correct |
21 |
Correct |
10 ms |
10188 KB |
Output is correct |
22 |
Correct |
149 ms |
24344 KB |
Output is correct |
23 |
Correct |
200 ms |
25424 KB |
Output is correct |
24 |
Correct |
161 ms |
26928 KB |
Output is correct |
25 |
Correct |
141 ms |
25908 KB |
Output is correct |
26 |
Correct |
143 ms |
28464 KB |
Output is correct |
27 |
Correct |
156 ms |
28516 KB |
Output is correct |
28 |
Correct |
187 ms |
28500 KB |
Output is correct |
29 |
Correct |
345 ms |
38224 KB |
Output is correct |
30 |
Correct |
361 ms |
40356 KB |
Output is correct |
31 |
Correct |
356 ms |
42960 KB |
Output is correct |
32 |
Correct |
400 ms |
41244 KB |
Output is correct |
33 |
Correct |
353 ms |
46124 KB |
Output is correct |
34 |
Correct |
402 ms |
46116 KB |
Output is correct |
35 |
Correct |
354 ms |
46124 KB |
Output is correct |
36 |
Correct |
391 ms |
38764 KB |
Output is correct |
37 |
Correct |
361 ms |
41048 KB |
Output is correct |
38 |
Correct |
389 ms |
43916 KB |
Output is correct |
39 |
Correct |
360 ms |
42004 KB |
Output is correct |
40 |
Correct |
366 ms |
47180 KB |
Output is correct |
41 |
Correct |
366 ms |
47160 KB |
Output is correct |
42 |
Correct |
406 ms |
47164 KB |
Output is correct |