結果

問題 No.1641 Tree Xor Query
ユーザー Example0911Example0911
提出日時 2021-08-06 21:57:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 258 ms / 5,000 ms
コード長 1,762 bytes
コンパイル時間 2,243 ms
コンパイル使用メモリ 207,140 KB
実行使用メモリ 26,872 KB
最終ジャッジ日時 2023-10-17 03:19:35
合計ジャッジ時間 3,964 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,380 KB
testcase_01 AC 3 ms
8,380 KB
testcase_02 AC 4 ms
8,380 KB
testcase_03 AC 4 ms
8,384 KB
testcase_04 AC 3 ms
8,396 KB
testcase_05 AC 4 ms
8,396 KB
testcase_06 AC 4 ms
8,392 KB
testcase_07 AC 4 ms
8,396 KB
testcase_08 AC 4 ms
8,380 KB
testcase_09 AC 4 ms
8,392 KB
testcase_10 AC 4 ms
8,388 KB
testcase_11 AC 3 ms
8,384 KB
testcase_12 AC 4 ms
8,388 KB
testcase_13 AC 258 ms
26,872 KB
testcase_14 AC 255 ms
26,872 KB
testcase_15 AC 6 ms
8,700 KB
testcase_16 AC 13 ms
9,392 KB
testcase_17 AC 11 ms
8,868 KB
testcase_18 AC 8 ms
9,196 KB
testcase_19 AC 9 ms
8,512 KB
testcase_20 AC 141 ms
21,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

#define int long long

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 1000000007;
template <typename T>
struct RMQ {
	int n;         // 葉の数
	vector<T> dat; // 完全二分木の配列
	RMQ(int n_) : n(), dat(n_ * 4, 0) { // 葉の数は 2^x の形
		int x = 1;
		while (n_ > x) {
			x *= 2;
		}
		n = x;
	}
	void update(int i, T x) {
		i += n - 1;
		dat[i] = (dat[i] ^ x);
		while (i > 0) {
			i = (i - 1) / 2;  // parent
			dat[i] = (dat[i * 2 + 1] ^ dat[i * 2 + 2]);
		}
	}
	// the minimum element of [a,b)
	T query(int a, int b) { return query_sub(a, b, 0, 0, n); }
	T query_sub(int a, int b, int k, int l, int r) {
		if (r <= a || b <= l) {
			return 0;
		}
		else if (a <= l && r <= b) {
			return dat[k];
		}
		else {
			T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
			T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
			return (vl ^ vr);
		}
	}
};
int N, Q;
vector<int>G[100010];
vector<int>C;
vector<int>euler_tour;
int B[200010], E[200010];
int k = 0;
void dfs(int v, int p) {
	B[v] = k; k++;
	euler_tour.push_back(v);
	for (auto nv : G[v]) {
		if (nv == p)continue;
		dfs(nv, v);
		euler_tour.push_back(v); k++;
	}
	E[v] = k;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> N >> Q;
	C.resize(N);
	for (int i = 0; i < N; i++)cin >> C[i];
	for (int i = 0; i < N - 1; i++) {
		int a, b; cin >> a >> b; a--; b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	RMQ<int>r(2 * N + 10);
	dfs(0, -1);
	for (int i = 0; i < N; i++) {
		r.update(B[i], C[i]);
	}
	for (int _ = 0; _ < Q; _++) {
		int T, X, Y; cin >> T >> X >> Y; X--;
		if (T == 1) {
			r.update(B[X], Y);
		}
		else {
			cout << r.query(B[X], E[X]) << endl;
		}
	}
	return 0;

}

0