#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 << ' ';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 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 |
6 ms |
9804 KB |
Output is correct |
5 |
Correct |
6 ms |
9804 KB |
Output is correct |
6 |
Correct |
6 ms |
9804 KB |
Output is correct |
7 |
Correct |
6 ms |
9804 KB |
Output is correct |
8 |
Correct |
7 ms |
9932 KB |
Output is correct |
9 |
Correct |
7 ms |
9972 KB |
Output is correct |
10 |
Correct |
8 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 |
8 ms |
9932 KB |
Output is correct |
14 |
Correct |
8 ms |
9976 KB |
Output is correct |
15 |
Correct |
8 ms |
10060 KB |
Output is correct |
16 |
Correct |
10 ms |
10060 KB |
Output is correct |
17 |
Correct |
8 ms |
10200 KB |
Output is correct |
18 |
Correct |
8 ms |
10188 KB |
Output is correct |
19 |
Correct |
7 ms |
10188 KB |
Output is correct |
20 |
Correct |
7 ms |
10188 KB |
Output is correct |
21 |
Correct |
7 ms |
10188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 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 |
6 ms |
9804 KB |
Output is correct |
5 |
Correct |
6 ms |
9804 KB |
Output is correct |
6 |
Correct |
6 ms |
9804 KB |
Output is correct |
7 |
Correct |
6 ms |
9804 KB |
Output is correct |
8 |
Correct |
7 ms |
9932 KB |
Output is correct |
9 |
Correct |
7 ms |
9972 KB |
Output is correct |
10 |
Correct |
8 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 |
8 ms |
9932 KB |
Output is correct |
14 |
Correct |
8 ms |
9976 KB |
Output is correct |
15 |
Correct |
8 ms |
10060 KB |
Output is correct |
16 |
Correct |
10 ms |
10060 KB |
Output is correct |
17 |
Correct |
8 ms |
10200 KB |
Output is correct |
18 |
Correct |
8 ms |
10188 KB |
Output is correct |
19 |
Correct |
7 ms |
10188 KB |
Output is correct |
20 |
Correct |
7 ms |
10188 KB |
Output is correct |
21 |
Correct |
7 ms |
10188 KB |
Output is correct |
22 |
Correct |
121 ms |
25164 KB |
Output is correct |
23 |
Correct |
147 ms |
25844 KB |
Output is correct |
24 |
Correct |
139 ms |
27084 KB |
Output is correct |
25 |
Correct |
145 ms |
28356 KB |
Output is correct |
26 |
Correct |
126 ms |
28808 KB |
Output is correct |
27 |
Correct |
129 ms |
28848 KB |
Output is correct |
28 |
Correct |
129 ms |
28760 KB |
Output is correct |
29 |
Correct |
334 ms |
39232 KB |
Output is correct |
30 |
Correct |
313 ms |
40324 KB |
Output is correct |
31 |
Correct |
322 ms |
42904 KB |
Output is correct |
32 |
Correct |
307 ms |
43700 KB |
Output is correct |
33 |
Correct |
337 ms |
45964 KB |
Output is correct |
34 |
Correct |
346 ms |
46000 KB |
Output is correct |
35 |
Correct |
308 ms |
45964 KB |
Output is correct |
36 |
Correct |
360 ms |
40492 KB |
Output is correct |
37 |
Correct |
360 ms |
42000 KB |
Output is correct |
38 |
Correct |
402 ms |
44544 KB |
Output is correct |
39 |
Correct |
311 ms |
45360 KB |
Output is correct |
40 |
Correct |
358 ms |
47852 KB |
Output is correct |
41 |
Correct |
332 ms |
47848 KB |
Output is correct |
42 |
Correct |
296 ms |
47772 KB |
Output is correct |