#include <bits/stdc++.h>
#define tiiii tuple<int, int, int, int>
using namespace std;
vector<int> A[202020];
vector<int> B[202020];
int cntA = 0;
int cntB = 0;
int inA[202020];
int outA[202020];
int inB[202020];
int outB[202020];
void dfsA(int x)
{
inA[x] = cntA++;
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++;
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[inA[i]] = i;
int Q[n]; for(int i = 0; i < n; ++i) Q[i] = R[inB[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 |
Incorrect |
6 ms |
9804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
9804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |