diff --git a/khj20006/202502/10 BOJ P2 Promotion Counting.md b/khj20006/202502/10 BOJ P2 Promotion Counting.md new file mode 100644 index 00000000..b84c5073 --- /dev/null +++ b/khj20006/202502/10 BOJ P2 Promotion Counting.md @@ -0,0 +1,117 @@ +```java + +import java.util.*; +import java.io.*; + + +class Main { + + // IO field + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + static long nextLong() {return Long.parseLong(st.nextToken());} + static void bwEnd() throws Exception {bw.flush();bw.close();} + + // Additional field + static int[] P, in, out, ord; + static List[] V; + static int N, num = 0; + static List[] seg; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + } + + static void ready() throws Exception{ + + N = Integer.parseInt(br.readLine()); + P = new int[N+1]; + V = new List[N+1]; + in = new int[N+1]; + out = new int[N+1]; + ord = new int[N+1]; + seg = new List[4*N+1]; + for(int i=1;i<=N;i++) V[i] = new ArrayList<>(); + + for(int i=1;i<=N;i++) P[i] = Integer.parseInt(br.readLine()); + for(int i=2;i<=N;i++) V[Integer.parseInt(br.readLine())].add(i); + + } + + static void solve() throws Exception{ + + dfs(1); + init(1,N,1); + for(int i=1;i<=N;i++) { + int res = find(1,N,in[i]+1,out[i],P[i],1); + bw.write(res+"\n"); + } + + } + + static void dfs(int n) { + in[n] = ++num; + ord[num] = n; + for(int i:V[n]) dfs(i); + out[n] = num; + } + + static void init(int s, int e, int n) { + if(s==e) { + seg[n] = new ArrayList<>(); + seg[n].add(P[ord[s]]); + return; + } + int m=(s+e)>>1; + init(s,m,n*2); init(m+1,e,n*2+1); + seg[n] = merge(seg[n*2], seg[n*2+1]); + } + + static List merge(List A, List B){ + List result = new ArrayList<>(); + int l = 0, r = 0; + while(lr || l>e || r>1; + return find(s,m,l,r,v,n*2) + find(m+1,e,l,r,v,n*2+1); + } + + static int upperBound(List A, int k) { + int s = 0, e = A.size() - 1, m = (s+e)>>1; + if(k >= A.get(A.size()-1)) return A.size(); + if(k < A.get(0)) return 0; + while(s < e) { + if(A.get(m) <= k) s = m+1; + else e = m; + m = (s+e)>>1; + } + return m; + } + +} + +```