結果
問題 | No.1054 Union add query |
ユーザー |
![]() |
提出日時 | 2020-05-15 22:31:06 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 365 ms / 2,000 ms |
コード長 | 2,351 bytes |
コンパイル時間 | 1,761 ms |
コンパイル使用メモリ | 175,700 KB |
実行使用メモリ | 50,792 KB |
最終ジャッジ日時 | 2024-09-19 11:29:13 |
合計ジャッジ時間 | 4,626 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 8 |
ソースコード
#include <bits/stdc++.h> #include <iomanip> using namespace std; #define reps(i,s,n) for(int i = s; i < n; i++) #define rep(i,n) reps(i,0,n) #define Rreps(i,n,e) for(int i = n - 1; i >= e; --i) #define Rrep(i,n) Rreps(i,n,0) #define ALL(a) a.begin(), a.end() #define fi first #define se second typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; ll N,M,H,W,Q,K,A,B; string S; const ll MOD = 998244353; //const ll MOD = (1e+9) + 7; typedef pair<ll, ll> P; const ll INF = (1LL<<60); int main() { cin>>N>>Q; class union_find{ ll nn; vector<long> parent, tree_size, when_parent; vector<vector<P>> v_lib; public: union_find(unsigned long _n) : parent(_n), tree_size(_n, 1),when_parent(_n, -1), v_lib(_n, vector<P>(1, P(-1,0))), nn(_n){ for(unsigned long i = 0; i < _n; ++i)parent[i] = i; } ll find(ll x){ while(parent[x] != x)x = parent[x]; return x; } ll score(ll x, ll id){ ll score = v_lib[x][v_lib[x].size() - 1].se; //cout<<id<<' '<<x<<endl; while(parent[x] != x){ auto ite = lower_bound(ALL(v_lib[parent[x]]), P(when_parent[x], -1)); x = parent[x]; --ite; //cout<<score<<' '<<v_lib[x][v_lib[x].size() - 1].se<<' '<<ite->se<<endl; score += v_lib[x][v_lib[x].size() - 1].se - ite->se; } return score; } void add(ll x, ll b, ll id){ x = find(x); v_lib[x].emplace_back(id, b + v_lib[x][v_lib[x].size() - 1].se); } void merge(ll a, ll b, ll id){ a = find(a); b = find(b); if(a==b) return; if(tree_size[a] < tree_size[b])swap(a, b); tree_size[a] += tree_size[b]; parent[b] = a; when_parent[b] = id; } }; union_find uf(N + 1); rep(i,Q){ int a, b, c; scanf("%d%d%d", &a, &b, &c); if(a == 1){ uf.merge(b, c, i); }else if(a == 2){ uf.add(b, c, i); }else{ printf("%lld\n", uf.score(b, i)); } } }