結果

問題 No.1054 Union add query
ユーザー tko919tko919
提出日時 2021-08-27 20:04:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,616 bytes
コンパイル時間 2,394 ms
コンパイル使用メモリ 213,412 KB
実行使用メモリ 64,080 KB
最終ジャッジ日時 2023-08-13 05:52:25
合計ジャッジ時間 7,216 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

struct UnionFind{
   vector<int> par; int n;
   UnionFind(){}
   UnionFind(int _n):par(_n,-1),n(_n){}
   int root(int x){return par[x]<0?x:par[x]=root(par[x]);}
   bool same(int x,int y){return root(x)==root(y);}
   int size(int x){return -par[root(x)];}
   bool unite(int x,int y){
      x=root(x),y=root(y); if(x==y)return false;
      if(size(x)>size(y))swap(x,y);
      par[y]+=par[x]; par[x]=y; n--; return true;
   }
};

using P=pair<map<int,ll>,ll>;
void merge(P& a,P& b){
   if(a.first.size()<b.first.size())swap(a,b);
   for(auto& [i,v]:b.first){
      a.first[i]=v+b.second-a.second;
   }
}

int main(){
   int n,q;
   cin>>n>>q;

   vector<P> V(n);
   UnionFind uni(n);
   rep(i,0,n)V[i].first[i]=0;
   while(q--){
      int t,a,b;
      printf("%d%d%d",&t,&a,&b);
      if(t==1){
         a--; b--;
         a=uni.root(a);
         b=uni.root(b);
         merge(V[a],V[b]);
         uni.unite(a,b);
         swap(V[uni.root(a)],V[a]);
      }
      else if(t==2){
         a--;
         V[uni.root(a)].second+=b;
      }
      else{
         a--;
         int r=uni.root(a);
         printf("%lld\n",V[r].first[a]+V[r].second);
      }
   }
   return 0;
}
0