結果
問題 | No.1054 Union add query |
ユーザー |
|
提出日時 | 2025-02-05 09:49:35 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 951 ms / 2,000 ms |
コード長 | 812 bytes |
コンパイル時間 | 1,969 ms |
コンパイル使用メモリ | 105,404 KB |
実行使用メモリ | 36,900 KB |
最終ジャッジ日時 | 2025-02-05 09:49:47 |
合計ジャッジ時間 | 10,178 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 8 |
ソースコード
#include <cassert> #include <iostream> #include <vector> #include <atcoder/dsu> using namespace std; using ll = long long; int main() { int N, Q; cin >> N >> Q; atcoder::dsu uf(N); vector<int> ans(N); vector<vector<int>> G(N); for(int i = 0; i < N; i++) G[i].push_back(i); vector<int> Add(N); while(Q--) { int t, a, b; cin >> t >> a >> b; a--; if(t == 1) { b--; int la = uf.leader(a), lb = uf.leader(b); if(la == lb) continue; int nv = uf.merge(a, b); int other = la ^ lb ^ nv; for(int i = 0; i < G[other].size(); i++) { ans[G[other][i]] += Add[other] - Add[nv]; G[nv].push_back(G[other][i]); } G[other].clear(); Add[other] = 0; } else if(t == 2) { int la = uf.leader(a); Add[la] += b; } else { cout << ans[a] + Add[uf.leader(a)] << '\n'; } } }