結果
問題 | No.1641 Tree Xor Query |
ユーザー | tenten |
提出日時 | 2024-08-06 17:34:34 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 686 ms / 5,000 ms |
コード長 | 3,682 bytes |
コンパイル時間 | 4,136 ms |
コンパイル使用メモリ | 81,428 KB |
実行使用メモリ | 78,296 KB |
最終ジャッジ日時 | 2024-08-06 17:34:44 |
合計ジャッジ時間 | 9,778 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 48 ms
37,188 KB |
testcase_01 | AC | 47 ms
37,220 KB |
testcase_02 | AC | 48 ms
37,020 KB |
testcase_03 | AC | 51 ms
37,360 KB |
testcase_04 | AC | 51 ms
37,324 KB |
testcase_05 | AC | 54 ms
36,948 KB |
testcase_06 | AC | 52 ms
37,280 KB |
testcase_07 | AC | 50 ms
37,236 KB |
testcase_08 | AC | 48 ms
37,052 KB |
testcase_09 | AC | 49 ms
37,032 KB |
testcase_10 | AC | 50 ms
36,816 KB |
testcase_11 | AC | 51 ms
37,192 KB |
testcase_12 | AC | 50 ms
36,952 KB |
testcase_13 | AC | 665 ms
78,296 KB |
testcase_14 | AC | 686 ms
77,368 KB |
testcase_15 | AC | 154 ms
41,088 KB |
testcase_16 | AC | 176 ms
45,348 KB |
testcase_17 | AC | 154 ms
42,560 KB |
testcase_18 | AC | 155 ms
43,156 KB |
testcase_19 | AC | 138 ms
42,332 KB |
testcase_20 | AC | 584 ms
66,084 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { static List<List<Integer>> graph = new ArrayList<>(); static int[] inIdxes; static int[] outIdxes; static int idx = 0; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int q = sc.nextInt(); int[] values = new int[n]; for (int i = 0; i < n; i++) { values[i] = sc.nextInt(); graph.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; graph.get(a).add(b); graph.get(b).add(a); } inIdxes = new int[n]; outIdxes = new int[n]; setIdxes(0, 0); SegmentTree st = new SegmentTree(idx); for (int i = 0; i < n; i++) { st.add(inIdxes[i], values[i]); } StringBuilder sb = new StringBuilder(); while (q-- > 0) { int type = sc.nextInt(); int x = sc.nextInt() - 1; int y = sc.nextInt(); if (type == 1) { st.add(inIdxes[x], y); } else { sb.append(st.getSum(inIdxes[x], outIdxes[x])).append("\n"); } } System.out.print(sb); } static void setIdxes(int x, int p) { inIdxes[x] = idx++; for (int y : graph.get(x)) { if (y != p) { setIdxes(y, x); } } outIdxes[x] = idx++; } static class SegmentTree { int size; int[] values; public SegmentTree(int x) { size = 2; while (size < x) { size <<= 1; } values = new int[size * 2 - 1]; } public void add(int x, int v) { int current = size + x - 1; values[current] ^= v; calc((current - 1) / 2); } private void calc(int x) { values[x] = values[x * 2 + 1] ^ values[x * 2 + 2]; if (x > 0) { calc((x - 1) / 2); } } public int getSum(int left, int right) { return getSum(0, 0, size, left, right); } private int getSum(int x, int min, int max, int left, int right) { if (left <= min && max <= right) { return values[x]; } else if (right <= min || max <= left) { return 0; } else { return getSum(x * 2 + 1, min, (min + max) / 2, left, right) ^ getSum(x * 2 + 2, (min + max) / 2, max, left, right); } } } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }