結果

問題 No.1641 Tree Xor Query
ユーザー suzuken_wsuzuken_w
提出日時 2021-08-07 00:00:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 5,000 ms
コード長 4,977 bytes
コンパイル時間 2,535 ms
コンパイル使用メモリ 226,484 KB
実行使用メモリ 19,596 KB
最終ジャッジ日時 2023-10-17 05:43:26
合計ジャッジ時間 4,184 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 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 92 ms
19,596 KB
testcase_14 AC 94 ms
19,596 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 7 ms
4,348 KB
testcase_17 AC 5 ms
4,348 KB
testcase_18 AC 6 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 63 ms
14,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
template <typename T>
struct ST{//1index query l,r lが開区間
  using F = function<T(T,T)>;
  int n;
  F f;
  T ti;
  vector<T> dat;
  ST(){};
  ST(F f,T ti):f(f),ti(ti){}
  void init(int n_){    
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }
  void build(const vector<T> &v){
    int n_=v.size();
    init(n_);
    for(int i=0;i<n_;i++) dat[n+i]=v[i];
    for(int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }
  void set_val(int k,T x){//kは0-index
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);    
  }
  T query(int a,int b){
    T vl=ti,vr=ti;
    for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }
};
template<typename G>
struct HLD{
    G &g;
    vector<int> sz,in,out,head,rev,par,d;
    bool built;
    HLD(G &g,int s=0,bool Auto_build=true):g(g),sz(g.size()),in(g.size()),out(g.size()),head(g.size()),rev(g.size()),par(g.size()),d(g.size()),built(false){
         if(Auto_build)build(s);
    };
    HLD(){}

    void dfs_sz(int idx,int p){
        par[idx]=p;
        sz[idx]=1;
        if(g[idx].size()&&g[idx][0]==p)swap(g[idx][0],g[idx].back());
        for(auto &to:g[idx]){
            if(to==p)continue;
            d[to]+=d[idx]+1;
            dfs_sz(to,idx);
            sz[idx]+=sz[to];
            if(sz[g[idx][0]]<sz[to])swap(g[idx][0],to);
        }
    }

    void dfs_hld(int idx,int p,int &times){
        in[idx]=times++;
        rev[in[idx]]=idx;
        for(auto &to:g[idx]){
              if(to==p)continue;
              head[to]=(g[idx][0]==to?head[idx]:to);
              dfs_hld(to,idx,times);
        }
        out[idx]=times;
    }


    void build(int s){
        dfs_sz(s,-1);
        int t=0;
        dfs_hld(s,-1,t);
        built=true;
    }

    //頂点vから頂点0へk個だけ遡った頂点
    int la(int v,int k){
          while(1){
              int u=head[v];
              if(in[v]-k>=in[u])return rev[in[v]-k];
              k-=in[v]-in[u]+1;
              v=par[u];
          }
          assert(false);
          return 0;
    }
    
    int lca(int u,int v){
        for(;;v=par[head[v]]){
            if(in[u]>in[v])swap(u,v);
            if(head[u]==head[v])return u;
        }
        assert(false);
        return 0;
    }

    template<typename T,typename Q,typename F>
    T query(int u,int v,const T& ti,const Q &q,const F &f,bool e=false){
        T l=ti,r=ti;
        for(;;v=par[head[v]]){
              if(in[u]>in[v]){
                    swap(u,v);
                    swap(l,r);
              }
              if(head[u]==head[v])break;
              l=f(q(in[head[v]],in[v]+1),l);
        }
        return f(f(q(in[u]+e,in[v]+1),l),r);
    }

    template<typename Q>
    void add(int u,int v,const Q &q,bool e=false){
          for(;;v=par[head[v]]){
              if(in[u]>in[v])swap(u,v);
              if(head[u]==head[v])break;
              q(in[head[v]],in[v]+1);
          }
          q(in[u]+e,in[v]+1);
    }

    int dist(int u,int v){
          return d[u]+d[v]-2*d[lca(u,v)];
    }
    template<typename F>
    void subtree_query(int v, F&f) {
        f(in[v], out[v]);
    }  
};
int main(){
      auto f=[&](ll a,ll b){
            return a^b;
      };
      int n,q;
      cin>>n>>q;
      V<ll> c(n);
      for(int i=0;i<n;i++)cin>>c[i];
      V<V<int>> g(n);
      for(int i=0;i<n-1;i++){
            int a,b;
            cin>>a>>b;
            --a;--b;
            g[a].emplace_back(b);
            g[b].emplace_back(a);
      }
      HLD<V<V<int>>> hld(g);
      ST<ll> dp(f,0);
      dp.init(n+5);
      for(int i=0;i<n;i++){
            dp.set_val(hld.in[i],c[i]);
      }
      auto h=[&](int l,int r){
            cout<<dp.query(l,r)<<"\n";
      };
      for(int i=0;i<q;i++){
            ll t,x,y;
            cin>>t>>x>>y;
            x--;
            if(t==1){
                  dp.set_val(hld.in[x],dp.query(hld.in[x],hld.in[x]+1)^y);          
            }else{
                 hld.subtree_query(x,h);
            }
      }
}
0