結果

問題 No.1641 Tree Xor Query
ユーザー harurunharurun
提出日時 2021-07-10 11:34:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 903 ms / 5,000 ms
コード長 3,386 bytes
コンパイル時間 1,758 ms
コンパイル使用メモリ 114,220 KB
実行使用メモリ 122,468 KB
最終ジャッジ日時 2023-10-17 02:26:22
合計ジャッジ時間 6,006 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 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 903 ms
122,468 KB
testcase_14 AC 888 ms
122,468 KB
testcase_15 AC 16 ms
7,060 KB
testcase_16 AC 52 ms
11,688 KB
testcase_17 AC 35 ms
7,708 KB
testcase_18 AC 29 ms
11,040 KB
testcase_19 AC 22 ms
4,420 KB
testcase_20 AC 891 ms
119,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Writer解 その1
#include <atcoder/segtree>
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
using namespace atcoder;

const int bit_length=10;

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

void calculate_depth(const vector<vector<int>>& G,vector<int>& depth){
  int n=G.size();
  deque<int> dq;
  vector<bool> check(n,false);
  dq.push_back(0);
  while(!dq.empty()){
    int q=dq.back();
    dq.pop_back();
    check.at(q)=true;
    for(int i:G.at(q)){
      if(check.at(i))continue;
      dq.push_back(i);
      depth.at(i)=depth.at(q)+1;
    }
  }
  return;
}

void separate(const vector<vector<int>>& G,const vector<int>& depth,vector<vector<int>>& parents,vector<vector<int>>& children){
  int n=G.size();
  for(int i=0;i<n;i++){
    for(int j:G.at(i)){
      if(depth.at(i)<depth.at(j))children.at(i).push_back(j);
      else parents.at(i).push_back(j);
    }
  }
  return;
}

Counter change(int 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;
}

void convert(const vector<vector<int>>& parents,vector<vector<int>>& children,vector<int>& C,vector<vector<int>>& Nodes,vector<Counter>& A){
  int n=parents.size(),next,now=0;
  vector<bool> check(n,false);
  while(true){
    if(!check.at(now)){
      Nodes.at(now).push_back((int)A.size());
      A.push_back(change(C.at(now)));
      check.at(now)=true;
    }
    if(!children.at(now).empty()){
      next=children.at(now).back();
      children.at(now).pop_back();
    }else if(!parents.at(now).empty()){
      Nodes.at(now).push_back((int)A.size());
      A.push_back(change(C.at(now)));
      next=parents.at(now).back();
    }else{
      Nodes.at(now).push_back((int)A.size());
      A.push_back(change(C.at(now)));
      break;
    }
    now=next;
  }
  return;
}

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);
  }
  return ret;
}

Counter e(){
  Counter ret;
  for(int i=0;i<bit_length;i++){
    ret.val.at(i)=0;
  }
  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;
}

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

int main(){
  int N,Q;
  cin>>N>>Q;
  vector<int>C(N);
  vector<vector<int>>G(N);
  for(int i=0;i<N;i++)cin>>C.at(i);
  for(int i=0;i<N-1;i++){
    int a,b;
    cin>>a>>b;
    G.at(a-1).push_back(b-1);
    G.at(b-1).push_back(a-1);
  }
  
  vector<int>depth(N,0);
  calculate_depth(G,depth);
  
  vector<vector<int>>parents(N);
  vector<vector<int>>children(N);
  separate(G,depth,parents,children);
  
  vector<vector<int>>Nodes(N);
  vector<Counter>A(N);
  convert(parents,children,C,Nodes,A);

  segtree<Counter,op,e> seg(A);
  int T,x,y,r,l;
  Counter R,L,P;
  for(int i=0;i<Q;i++){
    cin>>T>>x>>y;
    x--;
    if(T==1){
      l=Nodes.at(x).at(0);
      r=Nodes.at(x).at(1);
      L=seg.get(l);
      R=seg.get(r);
      seg.set(l,L^change(y));
      seg.set(r,R^change(y));
    }else{
      l=Nodes.at(x).at(0);
      r=Nodes.at(x).at(1);
      P=seg.prod(l,r+1);
      cout<<calculate(P)<<endl;
    }
  }
  return 0;
}
0