結果
| 問題 |
No.1641 Tree Xor Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-06 22:50:31 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 52 ms / 5,000 ms |
| コード長 | 16,654 bytes |
| コンパイル時間 | 2,458 ms |
| コンパイル使用メモリ | 187,192 KB |
| 実行使用メモリ | 22,144 KB |
| 最終ジャッジ日時 | 2024-09-17 03:03:38 |
| 合計ジャッジ時間 | 3,643 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 18 |
コンパイルメッセージ
In function 'void wt_L(long long int)',
inlined from 'int main()' at main.cpp:510:11:
main.cpp:149:6: warning: 'res' may be used uninitialized [-Wmaybe-uninitialized]
149 | x=-x;
| ~^~~
main.cpp: In function 'int main()':
main.cpp:658:8: note: 'res' was declared here
658 | SVAL res;
| ^~~
In function 'long long int segtree_rh_merge(long long int, long long int)',
inlined from 'SVAL segtree_rh<SVAL, SFUN>::get(int, int) [with SVAL = long long int; SFUN = long long int]' at main.cpp:697:28,
inlined from 'int main()' at main.cpp:510:11:
main.cpp:457:14: warning: 'tmp' may be used uninitialized [-Wmaybe-uninitialized]
457 | return a ^ b;
| ^
main.cpp: In function 'int main()':
main.cpp:659:8: note: 'tmp' was declared here
659 | SVAL tmp;
| ^~~
ソースコード
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
(*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
(*arr)=(T*)(*mem);
(*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
walloc1d(arr, x2-x1, mem);
(*arr) -= x1;
}
inline int my_getchar(){
static char buf[1048576];
static int s = 1048576;
static int e = 1048576;
if(s == e && e == 1048576){
e = fread(buf, 1, 1048576, stdin);
s = 0;
}
if(s == e){
return EOF;
}
return buf[s++];
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline void rd(long long &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
struct MY_WRITER{
char buf[1048576];
int s;
int e;
MY_WRITER(){
s = 0;
e = 1048576;
}
~MY_WRITER(){
if(s){
fwrite(buf, 1, s, stdout);
}
}
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar(int a){
if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
MY_WRITER_VAR.s = 0;
}
MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
my_putchar(a);
}
inline void wt_L(int x){
int s=0;
int m=0;
char f[10];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
my_putchar('-');
}
while(s--){
my_putchar(f[s]+'0');
}
}
inline void wt_L(unsigned x){
int s=0;
char f[10];
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
while(s--){
my_putchar(f[s]+'0');
}
}
inline void wt_L(long long x){
int s=0;
int m=0;
char f[20];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
my_putchar('-');
}
while(s--){
my_putchar(f[s]+'0');
}
}
inline void wt_L(unsigned long long x){
int s=0;
char f[21];
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
while(s--){
my_putchar(f[s]+'0');
}
}
int WRITER_DOUBLE_DIGIT = 15;
inline int writerDigit_double(){
return WRITER_DOUBLE_DIGIT;
}
inline void writerDigit_double(int d){
WRITER_DOUBLE_DIGIT = d;
}
inline void wt_L(double x){
const int d = WRITER_DOUBLE_DIGIT;
int k;
int r;
double v;
if(x!=x || (x==x+1 && x==2*x)){
my_putchar('E');
my_putchar('r');
my_putchar('r');
return;
}
if(x < 0){
my_putchar('-');
x = -x;
}
x += 0.5 * pow(0.1, d);
r = 0;
v = 1;
while(x >= 10*v){
v *= 10;
r++;
}
while(r >= 0){
r--;
k = floor(x / v);
if(k >= 10){
k = 9;
}
if(k <= -1){
k = 0;
}
x -= k * v;
v *= 0.1;
my_putchar(k + '0');
}
if(d > 0){
my_putchar('.');
v = 1;
for(r=(0);r<(d);r++){
v *= 0.1;
k = floor(x / v);
if(k >= 10){
k = 9;
}
if(k <= -1){
k = 0;
}
x -= k * v;
my_putchar(k + '0');
}
}
}
inline void wt_L(const char c[]){
int i=0;
for(i=0;c[i]!='\0';i++){
my_putchar(c[i]);
}
}
inline void wt_L(string &x){
int i=0;
for(i=0;x[i]!='\0';i++){
my_putchar(x[i]);
}
}
template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
a[k] = aval;
}
template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
a[k] = aval;
b[k] = bval;
}
template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
for(i=sz-1;i>k;i--){
c[i] = c[i-1];
}
a[k] = aval;
b[k] = bval;
c[k] = cval;
}
template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
for(i=sz-1;i>k;i--){
c[i] = c[i-1];
}
for(i=sz-1;i>k;i--){
d[i] = d[i-1];
}
a[k] = aval;
b[k] = bval;
c[k] = cval;
d[k] = dval;
}
template<class SVAL, class SFUN> struct segtree_rh{
int N;
int logN;
int trueN;
SVAL*val;
SFUN*fun;
char*dofun;
void malloc(int maxN, int once = 0);
void walloc(int maxN, int once = 0, void **mem = &wmem);
void free(void);
SVAL& operator[](int i);
void setN(int n, int zerofill = 1, int dobuild = 1);
void build(void);
inline void push_one(int a);
inline void push(int a);
inline void build(int a);
inline void change(int a, int b, SFUN f);
inline SVAL get(int a, int b);
template<bool (*f)(SVAL)> int max_right(int a, int mx);
template<bool (*f)(SVAL)> int max_right(int a);
template<bool (*f)(SVAL)> int min_left(int b, int mn);
template<bool (*f)(SVAL)> int min_left(int b);
}
;
struct graph{
int N;
int*es;
int**edge;
void setEdgeRootedTree(int N__, int M, int A[], int B[], int root=0, int reorder=0, int cnv[] = NULL, void **mem = &wmem){
int i;
int j;
int k;
int*dist;
int*q;
int qs;
int qe;
int*ind;
void*tmem;
N = N__;
tmem = ((char*)(*mem)) + (sizeof(int) * N + 15) + (sizeof(int*) * N + 15) + (sizeof(int) * M + 15 * N);
walloc1d(&es, N, mem);
walloc1d(&edge, N, mem);
for(i=(0);i<(N);i++){
es[i] = 0;
}
for(i=(0);i<(M);i++){
es[A[i]]++;
es[B[i]]++;
}
for(i=(0);i<(N);i++){
walloc1d(&edge[i], es[i], &tmem);
}
for(i=(0);i<(N);i++){
es[i] = 0;
}
for(i=(0);i<(M);i++){
edge[A[i]][es[A[i]]++] = B[i];
edge[B[i]][es[B[i]]++] = A[i];
}
walloc1d(&dist, N, &tmem);
walloc1d(&q, N, &tmem);
walloc1d(&ind, N, &tmem);
if(cnv==NULL){
walloc1d(&cnv, N, &tmem);
}
for(i=(0);i<(N);i++){
dist[i] = -1;
}
dist[root] = 0;
qs = qe = 0;
q[qe++] = root;
while(qs < qe){
i = q[qs++];
for(j=(0);j<(es[i]);j++){
k = edge[i][j];
if(dist[k]==-1){
dist[k] = dist[i] + 1;
q[qe++] = k;
}
}
}
if(reorder == 0){
for(i=(0);i<(N);i++){
cnv[i] = i;
}
for(i=(0);i<(N);i++){
ind[i] = i;
}
}
else{
for(i=(0);i<(N);i++){
cnv[i] = q[i];
}
for(i=(0);i<(N);i++){
ind[cnv[i]] = i;
}
}
for(i=(0);i<(N);i++){
es[i] = 0;
}
for(i=(0);i<(M);i++){
j = A[i];
k = B[i];
if(dist[j] > dist[k]){
swap(j, k);
}
es[ind[j]]++;
}
for(i=(0);i<(N);i++){
walloc1d(&edge[i], es[i], mem);
}
for(i=(0);i<(N);i++){
es[i] = 0;
}
for(i=(0);i<(M);i++){
j = A[i];
k = B[i];
if(dist[j] > dist[k]){
swap(j, k);
}
j = ind[j];
k = ind[k];
edge[j][es[j]++] = k;
}
}
}
;
graph g;
int A[100000+10];
int B[100000+10];
int esz;
long long C[100000+10];
int inorder[100000+10];
int exorder[100000+10];
int tsz[100000+10];
void add_edge(int x, int y){
arrInsert(esz, esz, A, x, B, y);
}
int N;
int Q;
int ts;
void dfs(int x){
int PiIOrLma;
inorder[x] = ts++;
for(PiIOrLma=(0);PiIOrLma<(g.es[x]);PiIOrLma++){
auto&i = g.edge[x][PiIOrLma];
dfs(i);
}
exorder[x] = ts;
}
long long segtree_rh_merge(long long a, long long b){
return a ^ b;
}
long long segtree_rh_apply(long long f, long long a){
return f ^ a;
}
long long segtree_rh_compose(long long f, long long g){
return f ^ g;
}
segtree_rh<long long, long long> t;
int main(){
int i, x;
wmem = memarr;
rd(N);
rd(Q);
{
int GIHf_YD2;
for(GIHf_YD2=(0);GIHf_YD2<(N);GIHf_YD2++){
rd(C[GIHf_YD2]);
}
}
{
int rbGYC8dh;
for(rbGYC8dh=(0);rbGYC8dh<(N-1);rbGYC8dh++){
rd(A[rbGYC8dh]);A[rbGYC8dh] += (-1);
rd(B[rbGYC8dh]);B[rbGYC8dh] += (-1);
}
}
t.walloc(300000);
t.setN(300000, 1, 0);
t.build();
g.setEdgeRootedTree(N, N-1, A, B);
ts = 1;
dfs(0);
for(x=(0);x<(N);x++){
t.change(inorder[x], inorder[x]+1, C[x]);
}
for(i=(0);i<(Q);i++){
int type;
rd(type);
if(type == 1){
int x;
long long y;
rd(x);
rd(y);
--x;
t.change(inorder[x], inorder[x]+1, y);
}
if(type==2){
int x;
int y;
rd(x);
rd(y);
--x;
wt_L(t.get(inorder[x], exorder[x] ));
wt_L('\n');
}
}
return 0;
}
template<class SVAL, class SFUN> void segtree_rh<SVAL, SFUN>::malloc(int maxN, int once /*= 0*/){
int i;
for(i=1;i<maxN;i*=2){
;
}
val = new SVAL[2*i];
fun = new SFUN[i];
dofun = new char[i];
if(once){
setN(maxN);
}
}
template<class SVAL, class SFUN> void segtree_rh<SVAL, SFUN>::walloc(int maxN, int once /*= 0*/, void **mem /*= &wmem*/){
int i;
for(i=1;i<maxN;i*=2){
;
}
walloc1d(&val, 2*i, mem);
walloc1d(&fun, i, mem);
walloc1d(&dofun, i, mem);
if(once){
setN(maxN);
}
}
template<class SVAL, class SFUN> void segtree_rh<SVAL, SFUN>::free(void){
delete [] val;
delete [] fun;
}
template<class SVAL, class SFUN> SVAL& segtree_rh<SVAL, SFUN>::operator[](int i){
return val[N+i];
}
template<class SVAL, class SFUN> void segtree_rh<SVAL, SFUN>::setN(int n, int zerofill /*= 1*/, int dobuild /*= 1*/){
int i;
for(i=1,logN=0;i<n;i*=2,logN++){
;
}
trueN = n;
N = i;
if(dobuild){
build();
}
}
template<class SVAL, class SFUN> void segtree_rh<SVAL, SFUN>::build(void){
int i;
for(i=N-1;i;i--){
val[i] = segtree_rh_merge(val[2*i], val[2*i+1]);
}
int ur5KXcGs = N;
for(i=(1);i<(ur5KXcGs);i++){
dofun[i] = 0;
}
}
template<class SVAL, class SFUN> inline void segtree_rh<SVAL, SFUN>::push_one(int a){
if(dofun[a]){
if(2*a < N){
if(dofun[2*a]){
fun[2*a] = segtree_rh_compose(fun[a], fun[2*a]);
}
else{
fun[2*a] = fun[a];
dofun[2*a] = 1;
}
}
val[2*a] = segtree_rh_apply(fun[a], val[2*a]);
if(2*a+1 < N){
if(dofun[2*a+1]){
fun[2*a+1] = segtree_rh_compose(fun[a], fun[2*a+1]);
}
else{
fun[2*a+1] = fun[a];
dofun[2*a+1] = 1;
}
}
val[2*a+1] = segtree_rh_apply(fun[a], val[2*a+1]);
dofun[a] = 0;
}
}
template<class SVAL, class SFUN> inline void segtree_rh<SVAL, SFUN>::push(int a){
int i;
for(i=logN;i;i--){
push_one(a>>i);
}
}
template<class SVAL, class SFUN> inline void segtree_rh<SVAL, SFUN>::build(int a){
while(a > 1){
a /= 2;
val[a] = segtree_rh_merge(val[2*a], val[2*a+1]);
if(dofun[a]){
val[a] = segtree_rh_apply(fun[a], val[a]);
}
}
}
template<class SVAL, class SFUN> inline void segtree_rh<SVAL, SFUN>::change(int a, int b, SFUN f){
int aa;
int bb;
if(a >= b){
return;
}
aa = (a += N);
bb = (b += N);
push(a);
push(b-1);
if(a%2){
val[a] = segtree_rh_apply(f, val[a]);
a++;
}
if(b%2){
b--;
val[b] = segtree_rh_apply(f, val[b]);
}
a /= 2;
b /= 2;
while(a < b){
if(a%2){
val[a] = segtree_rh_apply(f, val[a]);
if(dofun[a]){
fun[a] = segtree_rh_compose(f, fun[a]);
}
else{
fun[a] = f;
dofun[a] = 1;
}
a++;
}
if(b%2){
b--;
val[b] = segtree_rh_apply(f, val[b]);
if(dofun[b]){
fun[b] = segtree_rh_compose(f, fun[b]);
}
else{
fun[b] = f;
dofun[b] = 1;
}
}
a /= 2;
b /= 2;
}
build(aa);
build(bb-1);
}
template<class SVAL, class SFUN> inline SVAL segtree_rh<SVAL, SFUN>::get(int a, int b){
SVAL res;
SVAL tmp;
int fga = 0;
int fgb = 0;
a += N;
b += N;
push(a);
push(b-1);
while(a < b){
if(a%2){
if(fga){
res = segtree_rh_merge(res, val[a]);
}
else{
res = val[a];
fga = 1;
}
a++;
}
if(b%2){
b--;
if(fgb){
tmp = segtree_rh_merge(val[b], tmp);
}
else{
tmp = val[b];
fgb = 1;
}
}
a /= 2;
b /= 2;
}
if(fga==1 && fgb==0){
return res;
}
if(fga==0 && fgb==1){
return tmp;
}
if(fga==1 && fgb==1){
return segtree_rh_merge(res, tmp);
}
return res;
}
template<class SVAL, class SFUN> template<bool (*f)(SVAL)> int segtree_rh<SVAL, SFUN>::max_right(int a, int mx){
int fg = 0;
int ta = a;
int sz = 1;
SVAL cur;
SVAL tmp;
if(a>=mx){
return mx;
}
a += N;
push(a);
for(;;){
while(a%2==0){
a /= 2;
sz *= 2;
}
if(ta + sz <= mx){
if(fg==0){
tmp = val[a];
}
else{
tmp = segtree_rh_merge(cur, val[a]);
}
}
if(ta + sz > mx || !f(tmp)){
while(a < N){
push_one(a);
a *= 2;
sz /= 2;
if(ta + sz <= mx){
if(fg==0){
tmp = val[a];
}
else{
tmp = segtree_rh_merge(cur, val[a]);
}
}
if(ta + sz <= mx && f(tmp)){
fg = 1;
cur = tmp;
a++;
ta += sz;
}
}
return a - N;
}
fg = 1;
cur = tmp;
if((a & (a+1)) == 0){
break;
}
a++;
ta += sz;
}
return mx;
}
template<class SVAL, class SFUN> template<bool (*f)(SVAL)> int segtree_rh<SVAL, SFUN>::max_right(int a){
return max_right<f>(a, trueN);
}
template<class SVAL, class SFUN> template<bool (*f)(SVAL)> int segtree_rh<SVAL, SFUN>::min_left(int b, int mn){
int fg = 0;
int tb = b;
int sz = 1;
SVAL cur;
SVAL tmp;
if(b <= mn){
return mn;
}
b += N;
push(b-1);
for(;;){
while(b%2==0){
b /= 2;
sz *= 2;
}
if(tb - sz >= mn){
if(fg==0){
tmp = val[b-1];
}
else{
tmp = segtree_rh_merge(val[b-1], cur);
}
}
if(tb - sz < mn || !f(tmp)){
while(b-1 < N){
push_one(b-1);
b *= 2;
sz /= 2;
if(tb - sz >= mn){
if(fg==0){
tmp = val[b-1];
}
else{
tmp = segtree_rh_merge(val[b-1], cur);
}
}
if(tb - sz >= mn && f(tmp)){
fg = 1;
cur = tmp;
b--;
tb -= sz;
}
}
return b - N;
}
fg = 1;
cur = tmp;
b--;
tb -= sz;
if(tb <= mn){
break;
}
}
return mn;
}
template<class SVAL, class SFUN> template<bool (*f)(SVAL)> int segtree_rh<SVAL, SFUN>::min_left(int b){
return min_left<f>(b, 0);
}
// cLay version 20210405-1
// --- original code ---
// //no-unlocked
// graph g;
// int A[1d5+10], B[], esz;
// ll C[1d5+10];
// int inorder[], exorder[], tsz[];
//
// void add_edge(int x, int y){
// arrInsert(esz, esz, A, x, B, y);
// }
// int N, Q, ts;
//
// void dfs(int x){
// inorder[x] = ts++;
// rep[g.edge[x]](i, g.es[x]) dfs(i);
// exorder[x] = ts;
// }
//
// ll segtree_rh_merge(ll a, ll b){
// return a ^ b;
// }
// ll segtree_rh_apply(ll f, ll a){
// return f ^ a;
// }
// ll segtree_rh_compose(ll f, ll g){
// return f ^ g;
// }
// segtree_rh<ll, ll> t;
//
// {
// rd(N, Q, C(N), (A--,B--)(N-1));
// t.walloc(3d5);
// t.setN(3d5, 1, 0);
// t.build();
// g.setEdgeRootedTree(N, N-1, A, B);
// ts = 1;
// dfs(0);
// rep(x, N){
// t.change(inorder[x], inorder[x]+1, C[x]);
// }
// rep(i, Q){
// int type;
// rd(type);
// if(type == 1){
// int x;
// ll y;
// rd(x, y);
// --x;
//
// t.change(inorder[x], inorder[x]+1, y);
// }
// if(type==2){
// int x, y;
// rd(x, y);
// --x;
// wt(t.get(inorder[x], exorder[x] ));
// }
// }
// }