結果

問題 No.1641 Tree Xor Query
ユーザー harurunharurun
提出日時 2021-07-10 13:03:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 353 ms / 5,000 ms
コード長 2,407 bytes
コンパイル時間 1,433 ms
コンパイル使用メモリ 105,060 KB
実行使用メモリ 27,432 KB
最終ジャッジ日時 2023-10-17 02:26:48
合計ジャッジ時間 3,500 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 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 353 ms
27,432 KB
testcase_14 AC 351 ms
27,432 KB
testcase_15 AC 7 ms
4,508 KB
testcase_16 AC 22 ms
5,440 KB
testcase_17 AC 15 ms
4,784 KB
testcase_18 AC 13 ms
5,176 KB
testcase_19 AC 10 ms
4,348 KB
testcase_20 AC 247 ms
25,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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;
}

void convert(const vector<vector<int>>& parents,vector<vector<int>>& children,vector<int>& C,vector<vector<int>>& Nodes,vector<int>& A){
  int n=parents.size(),next,now=0,cnt=0;
  vector<bool> check(n,false);
  while(true){
    if(!check.at(now)){
      Nodes.at(now).push_back((int)A.size());
      A.push_back(C.at(now));
      cnt++;
      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(cnt);
      next=parents.at(now).back();
    }else{
      Nodes.at(now).push_back(cnt);
      break;
    }
    now=next;
  }
  return;
}

int op(const int a,const int b){
  return a^b;
}

int e(){
  return 0;
}

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<int>A;
  convert(parents,children,C,Nodes,A);

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