結果

問題 No.1054 Union add query
ユーザー kiyoshi0205kiyoshi0205
提出日時 2020-05-15 22:57:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,583 bytes
コンパイル時間 3,173 ms
コンパイル使用メモリ 208,508 KB
実行使用メモリ 31,520 KB
最終ジャッジ日時 2023-10-19 16:16:39
合計ジャッジ時間 7,081 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,696 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #include<ext/pb_ds/tag_and_trait.hpp>
// using namespace __gnu_pbds;
// #include<boost/multiprecision/cpp_int.hpp>
// namespace multiprecisioninteger = boost::multiprecision;
// using cint=multiprecisioninteger::cpp_int;
using namespace std;
using ll=long long;
#define double long double
using datas=pair<ll,ll>;
using ddatas=pair<double,double>;
using tdata=pair<ll,datas>;
using vec=vector<ll>;
using mat=vector<vec>;
using pvec=vector<datas>;
using pmat=vector<pvec>;
// using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;
#define For(i,a,b) for(i=a;i<(ll)b;++i)
#define bFor(i,b,a) for(i=b,--i;i>=(ll)a;--i)
#define rep(i,N) For(i,0,N)
#define rep1(i,N) For(i,1,N)
#define brep(i,N) bFor(i,N,0)
#define brep1(i,N) bFor(i,N,1)
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define vsort(v) sort(all(v))
#define vrsort(v) sort(allr(v))
#define endl "\n"
#define eb emplace_back
#define print(v) cout<<v<<endl
#define printyes cout<<"Yes"<<endl
#define printno cout<<"No"<<endl
#define printYES cout<<"YES"<<endl
#define printNO cout<<"NO"<<endl
#define output(v) do{bool f=0;for(auto outi:v){cout<<(f?" ":"")<<outi;f=1;}cout<<endl;}while(0)
#define matoutput(v) do{for(auto outimat:v)output(outimat);}while(0)
const ll mod=1000000007;
// const ll mod=998244353;
const ll inf=1LL<<60;
const double PI = acos(-1);
const double eps = 1e-9;
template<class T> inline bool chmax(T& a,T b){bool x=a<b;if(x)a=b;return x;} 
template<class T> inline bool chmin(T& a,T b){bool x=a>b;if(x)a=b;return x;} 

struct unionfind{
  private:
  int maxN;
  vector<int> par;
  public:unionfind(int N) :maxN(N),par(N){
    for(int i=0;i<maxN;++i)par[i]=i;
  }
  int root(int x){
    if(par[x]==x)return x;
    int r=root(par[x]);
    if(par[x]!=r){
      par[x]=r;
    }
    return r;
  }
  bool unite(int x,int y){
    int px=root(x);
    int py=root(y);
    // if(px>py)swap(px,py);
    if(px!=py){
      par[px]=py;
    }
    return px!=py;
  }
  bool parcheck(int x,int y){
    return root(x)==root(y);
  }
  void clear(){
    int i;
    for(i=0;i<maxN;++i){
      par[i]=i;
    }
  }
};

template<typename T>
struct LazySegmentTree{
private:
  using func=function<T(T,T)>;
  func f,g;
  size_t N=1;
  T E;
  vector<T> node,lazy;
  vector<bool> vis;
  void eval(size_t& x){
    if(!vis[x])return;
    node[x]=g(node[x],lazy[x]);
    vis[x]=false;
    if(x<N){
      if(vis[x<<1])lazy[x<<1]=g(lazy[x<<1],lazy[x]);
      else{
        lazy[x<<1]=lazy[x];
        vis[x<<1]=true;
      }
      if(vis[x<<1|1])lazy[x<<1|1]=g(lazy[x<<1|1],lazy[x]);
      else{
        lazy[x<<1|1]=lazy[x];
        vis[x<<1|1]=true;
      }
    }
  }
  void update(size_t& a,size_t& b,T& x,size_t now,size_t l,size_t r){
    eval(now);
    if((b<=l)|(r<=a))return;
    if((a<=l)&(r<=b)){
      vis[now]=true;lazy[now]=x;
      eval(now);
    }else{
      update(a,b,x,now<<1,l,(l+r)>>1);
      update(a,b,x,now<<1|1,(l+r)>>1,r);
      node[now]=f(node[now<<1],node[now<<1|1]);
    }
  }
  T call(size_t& a,size_t &b,size_t now,size_t l,size_t r){
    eval(now);
    if((b<=l)|(r<=a))return E;
    if((a<=l)&(r<=b))return node[now];
    return f(call(a,b,now<<1,l,(l+r)>>1),call(a,b,now<<1|1,(l+r)>>1,r));
  }
public:
  LazySegmentTree(vector<T>& v,func F,func G,T Ie):f(F),g(G),E(Ie){
    size_t i=v.size();
    while(i){
      N<<=1;i>>=1;
    }
    node.resize(N<<1,E);
    lazy.resize(N<<1);
    vis.resize(N<<1,false);
    for(i=0;i<v.size();i++){
      node[N+i]=v[i];
    }
    for(i=N;i--;){
      node[i]=f(node[i<<1],node[i<<1|1]);
    }
  }
  //do g(p,x). p is all[a,b);
  void update(size_t a,size_t b,T x){
    if(a<b)update(a,b,x,1,0,N);
  }
  //get f(I) I=[a,b);
  T get(size_t a,size_t b){
    return call(a,b,1,0,N);
  }
};
int N;
vector<vector<int>> g;
vector<pair<int,int>> pos;
int euler_tour(int now,int cnt){
  pos[now].first=cnt;
  cnt++;
  for(int x:g[now]){
    cnt=euler_tour(x,cnt);
  }
  return pos[now].second=cnt;
}
int main(){
  int i,Q,a,b,cnt=0;
  scanf("%d %d",&N,&Q);
  g.resize(N*2-1);
  pos.resize(N*2-1);
  unionfind tree(N*2-1);
  vector<pair<int,pair<int,int>>> queli;
  while(Q--){
    scanf("%d %d %d",&i,&a,&b);
    --a;
    if(i==1){
      --b;
      a=tree.root(a);
      b=tree.root(b);
      if(a!=b){
        tree.unite(a,cnt+N);
        tree.unite(b,cnt+N);
        queli.eb(1,pair<int,int>(a,cnt+N));
        queli.eb(1,pair<int,int>(b,cnt+N));
        g[cnt+N].eb(a);
        g[cnt+N].eb(b);
        ++cnt;
      }
    }else{
      queli.eb(i,pair<int,int>(a,b));
    }
  }
  if(cnt==0){
    vector<int> v(N,0);
    for(auto x:queli){
      if(x.first==2){
        v[x.second.first]+=x.second.second;
      }else{
        printf("%d\n",v[x.second.first]);
      }
    }
    return 0;
  }
  i=1;
  while(cnt<N-1){
    a=tree.root(0);
    while(tree.root(i)!=a)++i;
    b=tree.root(i);
    tree.unite(a,cnt+N);
    tree.unite(b,cnt+N);
    g[cnt+N].eb(a);
    g[cnt+N].eb(b);
    ++cnt;
  }
  tree.clear();
  euler_tour(N*2-2,0);
  vector<int> seg(N*2-1,0);
  LazySegmentTree<int> v(seg,[](int a,int b){return a+b;},[](int a,int b){return a+b;},0);
  for(auto x:queli){
    if(x.first==1){
      tree.unite(x.second.first,x.second.second);
    }else if(x.first==2){
      a=tree.root(x.second.first);
      v.update(pos[a].first,pos[a].second,x.second.second);
    }else{
      printf("%d\n",v.get(pos[x.second.first].first,pos[x.second.first].first+1));
    }
  }
}
0