結果
問題 | No.1054 Union add query |
ユーザー |
![]() |
提出日時 | 2023-08-13 09:30:00 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 325 ms / 2,000 ms |
コード長 | 982 bytes |
コンパイル時間 | 7,036 ms |
コンパイル使用メモリ | 344,668 KB |
実行使用メモリ | 37,376 KB |
最終ジャッジ日時 | 2024-11-21 03:22:11 |
合計ジャッジ時間 | 11,216 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 8 |
ソースコード
#ifndef DEBUG #pragma GCC target("avx2") #pragma GCC optimize("Ofast,unroll-loops") #endif #include <bits/stdc++.h> #include <atcoder/all> #define rep(i, n) for (int i = 0; i < n; i++) #define ALL(a) a.begin(), a.end() #define vc vector #define ll long long using namespace std; using namespace atcoder; constexpr int INF = 5e5 + 1; vector<int> g[INF]; int ans1[INF]; int ans2[INF]; void solve() { int n, q; scanf("%d %d", &n, &q); dsu DSU(n); rep(i, n) g[i].push_back(i); rep(qqqqq, q) { int ty, a, b; scanf("%d %d %d", &ty, &a, &b); a--; if (ty == 1) { a = DSU.leader(a); b = DSU.leader(b - 1); if (a == b) continue; const int c = DSU.merge(a, b); const int d = a ^ b ^ c; for (int i : g[d]) { ans2[i] += ans1[d] - ans1[c]; g[c].push_back(i); } ans1[d] = 0; g[d].clear(); } else if (ty == 2) ans1[DSU.leader(a)] += b; else printf("%d\n", ans2[a] + ans1[DSU.leader(a)]); } } int main() { solve(); return 0; }