結果
問題 |
No.1054 Union add query
|
ユーザー |
![]() |
提出日時 | 2020-05-15 23:26:21 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 306 ms / 2,000 ms |
コード長 | 1,798 bytes |
コンパイル時間 | 3,332 ms |
コンパイル使用メモリ | 207,812 KB |
実行使用メモリ | 64,884 KB |
最終ジャッジ日時 | 2024-09-19 13:40:43 |
合計ジャッジ時間 | 6,336 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 8 |
ソースコード
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<long long, long long>; #define rep(i, n) for(int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() constexpr char ln = '\n'; constexpr long long MOD = 1000000007LL; //constexpr long long MOD = 998244353LL; template<class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true;} return false; } template<class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true;} return false; } //////////////////////////////////////////////////////////////////////////////////////////////////////////// int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N,Q; cin >> N >> Q; vector<ll> arr(N); vector<int> root(N); vector<set<int>> dic(N); iota(all(root),0); rep(i,N) dic[i].emplace(i); while (Q--) { int T,A,B; cin >> T >> A >> B; if (T==1) { A--; B--; if (root[A] == root[B]) continue; if (dic[root[A]].size() > dic[root[B]].size()) swap(A,B); int r = root[A]; ll val = arr[root[A]]; for (auto it : dic[root[A]]) { if (it==r) arr[it] -= arr[root[B]]; else arr[it] += val - arr[root[B]]; root[it] = root[B]; dic[root[B]].emplace(it); } } else if (T==2) { A--; arr[root[A]] += B; } else { A--; if (A==root[A]) cout << arr[A] << ln; else cout << arr[A] + arr[root[A]] << ln; } } }