結果
問題 | No.1054 Union add query |
ユーザー |
![]() |
提出日時 | 2023-09-19 22:53:58 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 781 ms / 2,000 ms |
コード長 | 1,030 bytes |
コンパイル時間 | 5,409 ms |
コンパイル使用メモリ | 309,688 KB |
実行使用メモリ | 37,248 KB |
最終ジャッジ日時 | 2024-07-05 21:46:57 |
合計ジャッジ時間 | 11,695 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 8 |
ソースコード
#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"; } }