#include <iostream>
#include <vector>
#include <tuple>
#define tiiii tuple<int, int, int, int>
using namespace std;
vector<int> A[202020];
vector<int> B[202020];
int cntA = 0;
int cntB = 0;
vector<int> ordA, ordB;
int inA[202020];
int outA[202020];
int inB[202020];
int outB[202020];
void dfsA(int x)
{
inA[x] = cntA++;
ordA.push_back(x);
for(auto y : A[x]) dfsA(y);
outA[x] = cntA;
}
int fen[202020];
void upd(int pos)
{
++pos;
while(pos < 202020)
{
fen[pos]++;
pos += pos & -pos;
}
}
int qry(int pos)
{
int ret = 0;
while(pos)
{
ret += fen[pos];
pos &= pos - 1;
}
return ret;
}
void dfsB(int x)
{
inB[x] = cntB++;
ordB.push_back(x);
for(auto y : B[x]) dfsB(y);
outB[x] = cntB;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("in.txt", "r", stdin);
int n; cin >> n;
for(int i = 1; i < n; ++i)
{
int x; cin >> x; --x;
A[x].push_back(i);
}
for(int i = 1; i < n; ++i)
{
int x; cin >> x; --x;
B[x].push_back(i);
}
dfsA(0);
dfsB(0);
int R[n]; for(int i = 0; i < n; ++i) R[ordA[i]] = i;
int Q[n]; for(int i = 0; i < n; ++i) Q[i] = R[ordB[i]];
vector<tiiii> ls[n + 1];
for(int i = 0; i < n; ++i)
{
ls[inB[i]].push_back({inA[i], outA[i], i, -1});
ls[outB[i]].push_back({inA[i], outA[i], i, 1});
}
int ans[n]{};
for(int i = 0; i <= n; ++i)
{
for(auto [l, r, q, c] : ls[i])
ans[q] += c * (qry(r) - qry(l));
if(i < n) upd(Q[i]);
}
for(int i = 0; i < n; ++i) cout << ans[i] - 1 << ' ';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9804 KB |
Output is correct |
2 |
Correct |
8 ms |
9804 KB |
Output is correct |
3 |
Correct |
6 ms |
9804 KB |
Output is correct |
4 |
Correct |
6 ms |
9804 KB |
Output is correct |
5 |
Correct |
7 ms |
9884 KB |
Output is correct |
6 |
Correct |
6 ms |
9804 KB |
Output is correct |
7 |
Correct |
7 ms |
9880 KB |
Output is correct |
8 |
Correct |
8 ms |
9932 KB |
Output is correct |
9 |
Correct |
7 ms |
9932 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 |
10008 KB |
Output is correct |
13 |
Correct |
7 ms |
9952 KB |
Output is correct |
14 |
Correct |
8 ms |
9932 KB |
Output is correct |
15 |
Correct |
8 ms |
10116 KB |
Output is correct |
16 |
Correct |
10 ms |
10144 KB |
Output is correct |
17 |
Correct |
9 ms |
10132 KB |
Output is correct |
18 |
Correct |
10 ms |
10128 KB |
Output is correct |
19 |
Correct |
8 ms |
10216 KB |
Output is correct |
20 |
Correct |
10 ms |
10188 KB |
Output is correct |
21 |
Correct |
8 ms |
10188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9804 KB |
Output is correct |
2 |
Correct |
8 ms |
9804 KB |
Output is correct |
3 |
Correct |
6 ms |
9804 KB |
Output is correct |
4 |
Correct |
6 ms |
9804 KB |
Output is correct |
5 |
Correct |
7 ms |
9884 KB |
Output is correct |
6 |
Correct |
6 ms |
9804 KB |
Output is correct |
7 |
Correct |
7 ms |
9880 KB |
Output is correct |
8 |
Correct |
8 ms |
9932 KB |
Output is correct |
9 |
Correct |
7 ms |
9932 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 |
10008 KB |
Output is correct |
13 |
Correct |
7 ms |
9952 KB |
Output is correct |
14 |
Correct |
8 ms |
9932 KB |
Output is correct |
15 |
Correct |
8 ms |
10116 KB |
Output is correct |
16 |
Correct |
10 ms |
10144 KB |
Output is correct |
17 |
Correct |
9 ms |
10132 KB |
Output is correct |
18 |
Correct |
10 ms |
10128 KB |
Output is correct |
19 |
Correct |
8 ms |
10216 KB |
Output is correct |
20 |
Correct |
10 ms |
10188 KB |
Output is correct |
21 |
Correct |
8 ms |
10188 KB |
Output is correct |
22 |
Correct |
120 ms |
25220 KB |
Output is correct |
23 |
Correct |
155 ms |
25916 KB |
Output is correct |
24 |
Correct |
172 ms |
27196 KB |
Output is correct |
25 |
Correct |
117 ms |
28312 KB |
Output is correct |
26 |
Correct |
137 ms |
28824 KB |
Output is correct |
27 |
Correct |
145 ms |
28848 KB |
Output is correct |
28 |
Correct |
130 ms |
28776 KB |
Output is correct |
29 |
Correct |
315 ms |
39044 KB |
Output is correct |
30 |
Correct |
369 ms |
40292 KB |
Output is correct |
31 |
Correct |
381 ms |
42868 KB |
Output is correct |
32 |
Correct |
340 ms |
43636 KB |
Output is correct |
33 |
Correct |
285 ms |
45980 KB |
Output is correct |
34 |
Correct |
358 ms |
45968 KB |
Output is correct |
35 |
Correct |
321 ms |
45852 KB |
Output is correct |
36 |
Correct |
373 ms |
40556 KB |
Output is correct |
37 |
Correct |
312 ms |
41904 KB |
Output is correct |
38 |
Correct |
375 ms |
44580 KB |
Output is correct |
39 |
Correct |
306 ms |
45368 KB |
Output is correct |
40 |
Correct |
365 ms |
47820 KB |
Output is correct |
41 |
Correct |
343 ms |
47836 KB |
Output is correct |
42 |
Correct |
351 ms |
47884 KB |
Output is correct |