結果
| 問題 |
No.2325 Skill Tree
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-08-09 13:30:56 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,264 ms / 3,000 ms |
| コード長 | 2,875 bytes |
| コンパイル時間 | 2,927 ms |
| コンパイル使用メモリ | 92,536 KB |
| 実行使用メモリ | 81,128 KB |
| 最終ジャッジ日時 | 2024-11-15 09:12:37 |
| 合計ジャッジ時間 | 39,506 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 36 |
ソースコード
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();
}
}
tenten