結果

問題 No.2325 Skill Tree
ユーザー tentententen
提出日時 2023-08-09 13:30:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,056 ms / 3,000 ms
コード長 2,875 bytes
コンパイル時間 2,916 ms
コンパイル使用メモリ 96,284 KB
実行使用メモリ 85,592 KB
最終ジャッジ日時 2024-04-27 08:29:01
合計ジャッジ時間 33,437 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
50,568 KB
testcase_01 AC 47 ms
50,256 KB
testcase_02 AC 46 ms
50,596 KB
testcase_03 AC 47 ms
50,340 KB
testcase_04 AC 47 ms
50,280 KB
testcase_05 AC 47 ms
50,188 KB
testcase_06 AC 46 ms
50,424 KB
testcase_07 AC 337 ms
61,248 KB
testcase_08 AC 345 ms
66,272 KB
testcase_09 AC 372 ms
65,200 KB
testcase_10 AC 462 ms
75,884 KB
testcase_11 AC 451 ms
69,568 KB
testcase_12 AC 755 ms
85,592 KB
testcase_13 AC 876 ms
83,652 KB
testcase_14 AC 715 ms
74,216 KB
testcase_15 AC 801 ms
75,204 KB
testcase_16 AC 862 ms
75,376 KB
testcase_17 AC 761 ms
74,256 KB
testcase_18 AC 891 ms
83,412 KB
testcase_19 AC 748 ms
74,060 KB
testcase_20 AC 789 ms
74,352 KB
testcase_21 AC 829 ms
83,452 KB
testcase_22 AC 776 ms
73,840 KB
testcase_23 AC 739 ms
74,904 KB
testcase_24 AC 746 ms
73,984 KB
testcase_25 AC 821 ms
73,916 KB
testcase_26 AC 704 ms
73,636 KB
testcase_27 AC 958 ms
79,192 KB
testcase_28 AC 949 ms
78,808 KB
testcase_29 AC 979 ms
79,212 KB
testcase_30 AC 963 ms
79,192 KB
testcase_31 AC 933 ms
79,744 KB
testcase_32 AC 969 ms
78,736 KB
testcase_33 AC 857 ms
77,808 KB
testcase_34 AC 863 ms
76,388 KB
testcase_35 AC 1,056 ms
78,984 KB
testcase_36 AC 956 ms
73,344 KB
testcase_37 AC 950 ms
75,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static int[] levels;
    static int[] needs;
    static TreeMap<Integer, Integer> counts = new TreeMap<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        levels = new int[n];
        for (int i = 1; i < n; i++) {
            levels[i] = sc.nextInt();
            graph.get(sc.nextInt() - 1).add(i);
        }
        needs = new int[n];
        Arrays.fill(needs, -1);
        setNeeds(0, 0, 0);
        counts.put(0, 1);
        int current = 0;
        for (int x : counts.keySet()) {
            current += counts.get(x);
            counts.put(x, current);
        }
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (q-- > 0) {
            int type = sc.nextInt();
            int x = sc.nextInt();
            if (type == 1) {
                sb.append(counts.floorEntry(x).getValue()).append("\n");
            } else {
                sb.append(needs[x - 1]).append("\n");
            }
        }
        
        System.out.print(sb);
    }
    
    static void setNeeds(int idx, int p, int v) {
        needs[idx] = Math.max(v, levels[idx]);
        counts.put(needs[idx], counts.getOrDefault(needs[idx], 0) + 1);
        for (int x : graph.get(idx)) {
            if (p != x) {
                setNeeds(x, idx, needs[idx]);
            }
        }
    }
    
} 
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0