結果

問題 No.1054 Union add query
ユーザー たたき@競プロたたき@競プロ
提出日時 2023-09-19 22:53:58
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 760 ms / 2,000 ms
コード長 1,030 bytes
コンパイル時間 7,819 ms
コンパイル使用メモリ 308,060 KB
実行使用メモリ 37,132 KB
最終ジャッジ日時 2023-09-19 22:54:13
合計ジャッジ時間 13,741 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 626 ms
10,968 KB
testcase_04 AC 760 ms
37,132 KB
testcase_05 AC 565 ms
7,092 KB
testcase_06 AC 591 ms
17,680 KB
testcase_07 AC 529 ms
17,508 KB
testcase_08 AC 480 ms
17,388 KB
testcase_09 AC 742 ms
36,224 KB
testcase_10 AC 301 ms
36,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using mint=modint998244353; //1000000007;
using ll=long long;
using pp=pair<int,int>;
#define sr string 
#define vc vector
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define pb push_back
#define all(v) v.begin(),v.end()
#define pque priority_queue
#define bpc(a) __builtin_popcount(a)
int main(){
  int n,q;cin>>n>>q;
  vc<int>p(n); rep(i,n)p[i]=i;
  vc<int>x(n,0),xx(n,0);
  vc v(n,vc<int>(0)); rep(i,n)v[i].pb(i);
  auto par=[&](auto f,int a)->int{
    if(p[a]==a)return a;
    return p[a]=f(f,p[a]);
  };
  auto merge=[&](int a,int b)->void{
    a=par(par,a); b=par(par,b);
    if(a==b)return;
    if(v[a].size()>v[b].size())swap(a,b);
    for(auto au:v[a]){
      x[au]+=xx[a]-xx[b];
      v[b].pb(au);
    }
    p[a]=b;
    return;
  };
  rep(z,q){
    int t,a,b;cin>>t>>a>>b;
    if(t==1)merge(a-1,b-1);
    if(t==2)xx[par(par,a-1)]+=b;
    if(t==3)cout<<xx[par(par,a-1)]+x[a-1]<<"\n";
  }
}
    
0