|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | + |
| 7 | +class Main { |
| 8 | + |
| 9 | + // IO field |
| 10 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + static StringTokenizer st; |
| 13 | + |
| 14 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 15 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 16 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 17 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 18 | + |
| 19 | + // Additional field |
| 20 | + static int[] P, in, out, ord; |
| 21 | + static List<Integer>[] V; |
| 22 | + static int N, num = 0; |
| 23 | + static List<Integer>[] seg; |
| 24 | + |
| 25 | + public static void main(String[] args) throws Exception { |
| 26 | + |
| 27 | + ready(); |
| 28 | + solve(); |
| 29 | + |
| 30 | + bwEnd(); |
| 31 | + } |
| 32 | + |
| 33 | + static void ready() throws Exception{ |
| 34 | + |
| 35 | + N = Integer.parseInt(br.readLine()); |
| 36 | + P = new int[N+1]; |
| 37 | + V = new List[N+1]; |
| 38 | + in = new int[N+1]; |
| 39 | + out = new int[N+1]; |
| 40 | + ord = new int[N+1]; |
| 41 | + seg = new List[4*N+1]; |
| 42 | + for(int i=1;i<=N;i++) V[i] = new ArrayList<>(); |
| 43 | + |
| 44 | + for(int i=1;i<=N;i++) P[i] = Integer.parseInt(br.readLine()); |
| 45 | + for(int i=2;i<=N;i++) V[Integer.parseInt(br.readLine())].add(i); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + static void solve() throws Exception{ |
| 50 | + |
| 51 | + dfs(1); |
| 52 | + init(1,N,1); |
| 53 | + for(int i=1;i<=N;i++) { |
| 54 | + int res = find(1,N,in[i]+1,out[i],P[i],1); |
| 55 | + bw.write(res+"\n"); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + static void dfs(int n) { |
| 61 | + in[n] = ++num; |
| 62 | + ord[num] = n; |
| 63 | + for(int i:V[n]) dfs(i); |
| 64 | + out[n] = num; |
| 65 | + } |
| 66 | + |
| 67 | + static void init(int s, int e, int n) { |
| 68 | + if(s==e) { |
| 69 | + seg[n] = new ArrayList<>(); |
| 70 | + seg[n].add(P[ord[s]]); |
| 71 | + return; |
| 72 | + } |
| 73 | + int m=(s+e)>>1; |
| 74 | + init(s,m,n*2); init(m+1,e,n*2+1); |
| 75 | + seg[n] = merge(seg[n*2], seg[n*2+1]); |
| 76 | + } |
| 77 | + |
| 78 | + static List<Integer> merge(List<Integer> A, List<Integer> B){ |
| 79 | + List<Integer> result = new ArrayList<>(); |
| 80 | + int l = 0, r = 0; |
| 81 | + while(l<A.size() && r<B.size()) { |
| 82 | + if(A.get(l) < B.get(r)) { |
| 83 | + result.add(A.get(l)); |
| 84 | + l++; |
| 85 | + } |
| 86 | + else { |
| 87 | + result.add(B.get(r)); |
| 88 | + r++; |
| 89 | + } |
| 90 | + } |
| 91 | + while(l<A.size()) result.add(A.get(l++)); |
| 92 | + while(r<B.size()) result.add(B.get(r++)); |
| 93 | + return result; |
| 94 | + } |
| 95 | + |
| 96 | + static int find(int s, int e, int l, int r, int v, int n) { |
| 97 | + if(l>r || l>e || r<s) return 0; |
| 98 | + if(l<=s && e<=r) return seg[n].size() - upperBound(seg[n],v); |
| 99 | + int m=(s+e)>>1; |
| 100 | + return find(s,m,l,r,v,n*2) + find(m+1,e,l,r,v,n*2+1); |
| 101 | + } |
| 102 | + |
| 103 | + static int upperBound(List<Integer> A, int k) { |
| 104 | + int s = 0, e = A.size() - 1, m = (s+e)>>1; |
| 105 | + if(k >= A.get(A.size()-1)) return A.size(); |
| 106 | + if(k < A.get(0)) return 0; |
| 107 | + while(s < e) { |
| 108 | + if(A.get(m) <= k) s = m+1; |
| 109 | + else e = m; |
| 110 | + m = (s+e)>>1; |
| 111 | + } |
| 112 | + return m; |
| 113 | + } |
| 114 | + |
| 115 | +} |
| 116 | + |
| 117 | +``` |
0 commit comments