結果
問題 | No.1641 Tree Xor Query |
ユーザー |
![]() |
提出日時 | 2024-08-06 17:34:34 |
言語 | Java (openjdk 23) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 18 |
ソースコード
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();}}}