Advertisement
Josif_tepe

Untitled

Dec 9th, 2023
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #include <ext/pb_ds/assoc_container.hpp>
  5. #include <ext/pb_ds/tree_policy.hpp>
  6. using namespace __gnu_pbds;
  7. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  8.  
  9. const long long maxn = 1e5+10;
  10. int n;
  11. int val[maxn];
  12. ordered_set num[maxn];
  13. vector<int>g[maxn];
  14. int ans[maxn];
  15. void swap_ordered_set(ordered_set &a, ordered_set &b) {
  16.     a.swap(b);
  17. }
  18. void solve(int node,int par)
  19. {
  20.     for(auto ax:g[node])
  21.     {
  22.         if(ax==par)continue;
  23.         solve(ax,node);
  24.         if(num[node].size()<num[ax].size())
  25.         {
  26.             swap_ordered_set(num[node],num[ax]);
  27.         }
  28.         for(auto i:num[ax])
  29.         {
  30.             num[node].insert(i);
  31.         }
  32.     }
  33.     ///pomali od val[node]+1;
  34.     int L=num[node].order_of_key(val[node]+1);
  35.     ans[node]=num[node].size()-L;
  36.     num[node].insert(val[node]);
  37. }
  38.  
  39. int main() {
  40.     ios_base::sync_with_stdio(false);
  41.     freopen("promote.in", "r", stdin);
  42.     freopen("promote.out", "w", stdout);
  43.     ///ordered_set o_set;
  44.     cin>>n;
  45.     for(int i=0;i<n;i++)
  46.     {
  47.         cin>>val[i];
  48.        
  49.     }
  50.     for(int i=1;i<n;i++)
  51.     {
  52.         int x;
  53.         cin>>x;
  54.         x--;
  55.         g[x].push_back(i);
  56.     }
  57.     solve(0,-1);
  58.     for(int i=0;i<n;i++)
  59.     {
  60.         cout<<ans[i]<<'\n';
  61.     }
  62.     return 0;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement