結果

問題 No.1641 Tree Xor Query
ユーザー harurunharurun
提出日時 2021-06-20 23:01:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 883 ms / 5,000 ms
コード長 4,171 bytes
コンパイル時間 1,696 ms
コンパイル使用メモリ 116,664 KB
実行使用メモリ 112,268 KB
最終ジャッジ日時 2023-10-17 02:16:52
合計ジャッジ時間 6,597 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 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 883 ms
112,268 KB
testcase_14 AC 880 ms
112,268 KB
testcase_15 AC 13 ms
7,012 KB
testcase_16 AC 57 ms
15,020 KB
testcase_17 AC 37 ms
9,448 KB
testcase_18 AC 25 ms
10,800 KB
testcase_19 AC 21 ms
4,688 KB
testcase_20 AC 823 ms
110,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
3secにしたほうがいいかもしれない
*/
#include <atcoder/segtree>
#include <iostream>
#include <vector>
#include <deque>
#include <stdio.h>
using namespace std;
using namespace atcoder;
#define ll long long

const ll bit_length=10;

typedef struct count{
  vector<ll> val;
  count(){
    val.resize(bit_length);
  }
}Counter;

ll restore(const Counter& a){
  ll ret=0,c=1;
  for(int i=0;i<bit_length;i++){
    ret+=c*a.val.at(i);
    c*=2;
  }
  return ret;
}


ostream& operator<<(ostream& os,const vector<Counter>& a){
  for(Counter i:a){
    os<<restore(i)<<" ";
  }
  return os;
}

ostream& operator<<(ostream& os,const vector<ll>& a){
  for(ll i:a){
    os<<i<<" ";
  }
  return os;
}

ostream& operator<<(ostream& os,const Counter& a){
  for(int i=bit_length-1;i>=0;i--){
    os<<a.val.at(i);
  }
  return os;
}

Counter change(ll a){
  Counter ret;
  for(int i=0;i<bit_length;i++){
    if((a>>i)&1)ret.val.at(i)=1;
    else ret.val.at(i)=0;
  }
  return ret;
}

Counter op(const Counter a,const Counter b){
  Counter ret;
  for(int i=0;i<bit_length;i++){
    ret.val.at(i)=a.val.at(i)+b.val.at(i);
  }
  //cerr<<a<<" "<<b<<" "<<ret<<endl;
  return ret;
}

Counter e(){
  Counter ret;
  for(int i=0;i<bit_length;i++){
    ret.val.at(i)=0;
  }
  return ret;
}

ll calculate(const Counter& a){
  ll ret=0,c=1;
  for(int i=0;i<bit_length;i++){
    ret+=c*((a.val.at(i)/2)%2);
    c*=2;
  }
  return ret;
}

Counter operator^(const Counter& a,const Counter& b){
  Counter ret;
  for(int i=0;i<bit_length;i++){
    ret.val.at(i)=a.val.at(i)^b.val.at(i);
  }
  return ret;
}

vector<vector<ll>>d;
vector<ll>depth;

int main(){
  ll N,Q,a,b,T,x,y,q;
  cin>>N>>Q;
  vector<ll>C(N);
  d.resize(N);
  depth.resize(N);
  for(int i=0;i<N;i++){
    cin>>C.at(i);
    depth.at(i)=0;
  }
  for(int i=0;i<N-1;i++){
    cin>>a>>b;
    d.at(a-1).push_back(b-1);
    d.at(b-1).push_back(a-1);
  }
  //要素の深さを計算する
  deque<ll> dq;
  vector<bool>B(N,false);
  dq.push_back(0);
  while(!dq.empty()){
    q=dq.back();dq.pop_back();
    B.at(q)=true;
    for(ll i:d.at(q)){
      if(B.at(i))continue;  
      dq.push_back(i);
      depth.at(i)+=depth.at(q)+1;
    }
  }
  //子と親に分ける
  vector<vector<ll>> parents(N);
  vector<vector<ll>> children(N);
  for(int i=0;i<N;i++){
    for(ll j:d.at(i)){
      if(depth.at(i)<depth.at(j))children.at(i).push_back(j);
      else parents.at(i).push_back(j);
    }
  }
  //cerr<<C<<endl;
  ll next,now=0;
  vector<vector<ll>> Nodes(N);
  vector<Counter> A;
  vector<bool> check(N,false);//初めてきたか
  while(1){
    //cerr<<"now:"<<now+1<<" ";
    if(!check.at(now)){
      //初めて来たらpush
      //cerr<<"push "<<C.at(now)<<" "<<change(C.at(now))<<" ";
      Nodes.at(now).push_back(A.size());
      A.push_back(change(C.at(now)));
      check.at(now)=true;
    }
    if(!children.at(now).empty()){
      //進む
      //cerr<<"go\n";
      next=children.at(now).back();
      children.at(now).pop_back();
    }else if(!parents.at(now).empty()){
      //戻る
      //戻るときもpush
      //cerr<<"back "<<C.at(now)<<" "<<change(C.at(now))<<endl;
      Nodes.at(now).push_back(A.size());
      A.push_back(change(C.at(now)));
      next=parents.at(now).back();
    }else{
      //親がない->根
      //cerr<<"break "<<C.at(now)<<" "<<change(C.at(now))<<endl;
      Nodes.at(now).push_back(A.size());
      A.push_back(change(C.at(now)));
      break;
    }
    now=next;
  }
  //cerr<<A<<endl;
  //頂点が2つあることを忘れないようにする
  segtree<Counter,op,e> seg(A);
  ll r,l;
  Counter R,L,P;
  for(int i=0;i<Q;i++){
    cin>>T>>x>>y;
    if(T==1){
      x--;
      l=Nodes.at(x).at(0);
      r=Nodes.at(x).at(1);
      //cerr<<"l:"<<l<<",r:"<<r<<endl;
      L=seg.get(l);
      R=seg.get(r);
      seg.set(l,L^change(y));
      seg.set(r,R^change(y));
    }else{
      x--;
      l=Nodes.at(x).at(0);
      r=Nodes.at(x).at(1);
      //cerr<<"l:"<<l<<",r:"<<r<<endl;
      P=seg.prod(l,r+1);
      //cerr<<"P:"<<P<<endl;
      printf("%lld\n",calculate(P));
    }
  }
  // for(int i=0;i<2*N;i++){
  //   cerr<<restore(seg.get(i))<<" ";
  // }
  return 0;
}
0