結果

問題 No.1641 Tree Xor Query
ユーザー harurunharurun
提出日時 2021-07-10 12:50:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,491 bytes
コンパイル時間 1,246 ms
コンパイル使用メモリ 105,104 KB
実行使用メモリ 27,432 KB
最終ジャッジ日時 2023-10-17 02:26:43
合計ジャッジ時間 4,949 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 WA -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 360 ms
27,432 KB
testcase_14 AC 354 ms
27,432 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 248 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);
      //A.push_back(0);
      cnt++;
      next=parents.at(now).back();
    }else{
      Nodes.at(now).push_back(cnt);
      cnt++;
      //A.push_back(0);
      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,(l+r+1)/2);
      cout<<P<<endl;
    }
  }
  return 0;
}
0