結果
| 問題 |
No.1054 Union add query
|
| コンテスト | |
| ユーザー |
tko919
|
| 提出日時 | 2021-08-27 20:04:25 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,616 bytes |
| コンパイル時間 | 2,373 ms |
| コンパイル使用メモリ | 208,000 KB |
| 最終ジャッジ日時 | 2025-01-24 02:18:19 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 3 |
| other | RE * 8 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:44:16: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=]
44 | printf("%d%d%d",&t,&a,&b);
| ~^ ~~
| | |
| int int*
| %n
main.cpp:44:18: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int*’ [-Wformat=]
44 | printf("%d%d%d",&t,&a,&b);
| ~^ ~~
| | |
| int int*
| %n
main.cpp:44:20: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int*’ [-Wformat=]
44 | printf("%d%d%d",&t,&a,&b);
| ~^ ~~
| | |
| int int*
| %n
ソースコード
#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;
}
tko919