Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace __gnu_pbds;
- #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
- const long long maxn = 1e5+10;
- int n;
- int val[maxn];
- ordered_set num[maxn];
- vector<int>g[maxn];
- int ans[maxn];
- void swap_ordered_set(ordered_set &a, ordered_set &b) {
- a.swap(b);
- }
- void solve(int node,int par)
- {
- for(auto ax:g[node])
- {
- if(ax==par)continue;
- solve(ax,node);
- if(num[node].size()<num[ax].size())
- {
- swap_ordered_set(num[node],num[ax]);
- }
- for(auto i:num[ax])
- {
- num[node].insert(i);
- }
- }
- ///pomali od val[node]+1;
- int L=num[node].order_of_key(val[node]+1);
- ans[node]=num[node].size()-L;
- num[node].insert(val[node]);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- freopen("promote.in", "r", stdin);
- freopen("promote.out", "w", stdout);
- ///ordered_set o_set;
- cin>>n;
- for(int i=0;i<n;i++)
- {
- cin>>val[i];
- }
- for(int i=1;i<n;i++)
- {
- int x;
- cin>>x;
- x--;
- g[x].push_back(i);
- }
- solve(0,-1);
- for(int i=0;i<n;i++)
- {
- cout<<ans[i]<<'\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement