結果

問題 No.1641 Tree Xor Query
ユーザー tko919tko919
提出日時 2021-08-06 22:04:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 301 ms / 5,000 ms
コード長 2,438 bytes
コンパイル時間 2,320 ms
コンパイル使用メモリ 213,264 KB
実行使用メモリ 16,488 KB
最終ジャッジ日時 2023-10-17 03:26:09
合計ジャッジ時間 4,823 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 300 ms
16,488 KB
testcase_14 AC 301 ms
16,488 KB
testcase_15 AC 6 ms
4,348 KB
testcase_16 AC 18 ms
4,348 KB
testcase_17 AC 13 ms
4,348 KB
testcase_18 AC 11 ms
4,348 KB
testcase_19 AC 10 ms
4,348 KB
testcase_20 AC 208 ms
12,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

struct HLD{
   using P=pair<int,int>;
   vector<vector<int>> g; vector<int> sz,in,out,hs,par;
   void dfs(int v,int p){
      par[v]=p; sz[v]=1;
      if(!g[v].empty() and g[v][0]==p)swap(g[v][0],g[v].back());
      for(auto& to:g[v])if(to!=p){
         dfs(to,v); sz[v]+=sz[to];
         if(sz[g[v][0]]<sz[to])swap(g[v][0],to);
      }
   }
   void dfs2(int v,int p,int& k){
      in[v]=k++;
      for(auto& to:g[v])if(to!=p){
         hs[to]=(g[v][0]==to?hs[v]:to);
         dfs2(to,v,k);
      }
      out[v]=k;
   }
   HLD(int _n):g(_n),sz(_n),in(_n),out(_n),hs(_n),par(_n){}
   void add_edge(int u,int v){
      g[u].emplace_back(v); g[v].emplace_back(u);
   }
   void run(){dfs(0,-1); int k=0; dfs2(0,-1,k);}
   int lca(int u,int v){
      for(;;v=par[hs[v]]){
         if(in[u]>in[v])swap(u,v);
         if(hs[u]==hs[v])return u;
      }
   }
   vector<P> get(int u,int v,bool es=0){
      vector<P> res;
      for(;;v=par[hs[v]]){
         if(in[u]>in[v])swap(u,v);
         if(hs[u]==hs[v])break;
         res.emplace_back(in[hs[v]],in[v]+1);
      }
      res.emplace_back(in[u]+es,in[v]+1);
      return res;
   }
};

template<typename T>struct BIT{
   int n; T all=0; vector<T> val;
   BIT(int _n):n(_n),val(_n+10){}
   void clear(){val.assign(n+10,0); all=T();}
   void add(int i,T x){
      for(i++;i<=n;i+=(i&-i))val[i]^=x;
      all^=x;
   }
   T sum(int i){
      T res=0;
      for(i++;i;i-=(i&-i))res^=val[i];
      return res;
   }
};

int main(){
   int n,q;
   cin>>n>>q;

   vector<int> a(n);
   HLD hld(n);
   rep(i,0,n)cin>>a[i];
   rep(_,0,n-1){
      int x,y;
      cin>>x>>y;
      x--; y--;
      hld.add_edge(x,y);
   }
   
   hld.run();
   BIT<int> bit(n);
   rep(i,0,n)bit.add(hld.in[i],a[i]);

   while(q--){
      int t,x,y;
      cin>>t>>x>>y;
      if(t==1){
         x--;
         bit.add(hld.in[x],y);
      }
      else{
         x--;
         int res=bit.sum(hld.out[x]-1)^bit.sum(hld.in[x]-1);
         cout<<res<<'\n'; 
      }
   }
   return 0;
}
0