結果

問題 No.1641 Tree Xor Query
ユーザー tnakao0123tnakao0123
提出日時 2021-08-08 03:19:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 5,000 ms
コード長 2,520 bytes
コンパイル時間 969 ms
コンパイル使用メモリ 100,384 KB
実行使用メモリ 12,284 KB
最終ジャッジ日時 2023-10-19 03:11:35
合計ジャッジ時間 3,017 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,936 KB
testcase_01 AC 3 ms
7,936 KB
testcase_02 AC 3 ms
7,936 KB
testcase_03 AC 3 ms
7,940 KB
testcase_04 AC 3 ms
7,940 KB
testcase_05 AC 3 ms
7,940 KB
testcase_06 AC 3 ms
7,940 KB
testcase_07 AC 3 ms
7,940 KB
testcase_08 AC 3 ms
7,940 KB
testcase_09 AC 3 ms
7,940 KB
testcase_10 AC 3 ms
7,940 KB
testcase_11 AC 3 ms
7,940 KB
testcase_12 AC 3 ms
7,940 KB
testcase_13 AC 104 ms
12,284 KB
testcase_14 AC 107 ms
12,284 KB
testcase_15 AC 5 ms
8,100 KB
testcase_16 AC 10 ms
8,352 KB
testcase_17 AC 7 ms
8,160 KB
testcase_18 AC 7 ms
8,276 KB
testcase_19 AC 5 ms
7,972 KB
testcase_20 AC 73 ms
12,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1641.cc:  No.1641 Tree Xor Query - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 18; // = 262144

/* typedef */

typedef vector<int> vi;

template <typename T, const int MAX_E2>
struct SegTreeXor {
  int e2;
  T nodes[MAX_E2];
  SegTreeXor() {}

  void init(int n) {
    for (e2 = 1; e2 < n; e2 <<= 1);
    //fill(nodes, nodes + MAX_E2, 0);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = nodes[j * 2 + 1] ^ nodes[j * 2 + 2];
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = nodes[j * 2 + 1] ^ nodes[j * 2 + 2];
    }
  }

  T xor_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return 0;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = xor_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = xor_range(r0, r1, k * 2 + 2, im, i1);
    return v0 ^ v1;
  }
  T xor_range(int r0, int r1) { return xor_range(r0, r1, 0, 0, e2); }
};

/* global variables */

int cs[MAX_N], ls[MAX_N], rs[MAX_N];
int ps[MAX_N], cis[MAX_N];
vi nbrs[MAX_N];
SegTreeXor<int,MAX_E2> st;

/* subroutines */

/* main */

int main() {
  int n, qn;
  scanf("%d%d", &n, &qn);

  for (int i = 0; i < n; i++) scanf("%d", cs + i);

  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    nbrs[a].push_back(b);
    nbrs[b].push_back(a);
  }

  ps[0] = -1;
  for (int u = 0, k = 0; u >= 0;) {
    vi &nbru = nbrs[u];
    int up = ps[u];

    if (cis[u] == 0) ls[u] = k++;
    if (cis[u] < nbru.size()) {
      int v = nbru[cis[u]++];
      if (v != up) {
	ps[v] = u;
	u = v;
      }
    }
    else {
      rs[u] = k;
      u = up;
    }
  }

  st.init(n);
  for (int i = 0; i < n; i++) st.seti(ls[i], cs[i]);
  st.setall();

  while (qn--) {
    int t, x, y;
    scanf("%d%d%d", &t, &x, &y);
    x--;

    if (t == 1)
      st.set(ls[x], st.geti(ls[x]) ^ y);
    else {
      int v = st.xor_range(ls[x], rs[x]);
      printf("%d\n", v);
    }
  }
  return 0;
}
0