結果
| 問題 |
No.1641 Tree Xor Query
|
| コンテスト | |
| ユーザー |
Example0911
|
| 提出日時 | 2021-08-06 21:57:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 287 ms / 5,000 ms |
| コード長 | 1,762 bytes |
| コンパイル時間 | 2,143 ms |
| コンパイル使用メモリ | 197,504 KB |
| 最終ジャッジ日時 | 2025-01-23 15:15:42 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 18 |
ソースコード
#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;
}
Example0911