結果
問題 | No.2294 Union Path Query (Easy) |
ユーザー | Kude |
提出日時 | 2023-05-05 23:20:17 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 194 ms / 4,000 ms |
コード長 | 4,658 bytes |
コンパイル時間 | 2,623 ms |
コンパイル使用メモリ | 233,156 KB |
実行使用メモリ | 29,084 KB |
最終ジャッジ日時 | 2024-11-23 12:17:39 |
合計ジャッジ時間 | 14,429 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 183 ms
29,008 KB |
testcase_04 | AC | 142 ms
18,008 KB |
testcase_05 | AC | 132 ms
15,996 KB |
testcase_06 | AC | 187 ms
28,872 KB |
testcase_07 | AC | 194 ms
28,992 KB |
testcase_08 | AC | 160 ms
29,044 KB |
testcase_09 | AC | 159 ms
29,056 KB |
testcase_10 | AC | 190 ms
28,928 KB |
testcase_11 | AC | 186 ms
28,896 KB |
testcase_12 | AC | 188 ms
29,056 KB |
testcase_13 | AC | 164 ms
29,032 KB |
testcase_14 | AC | 141 ms
29,052 KB |
testcase_15 | AC | 178 ms
29,044 KB |
testcase_16 | AC | 117 ms
28,868 KB |
testcase_17 | AC | 144 ms
28,868 KB |
testcase_18 | AC | 142 ms
29,056 KB |
testcase_19 | AC | 135 ms
28,928 KB |
testcase_20 | AC | 133 ms
28,928 KB |
testcase_21 | AC | 134 ms
28,872 KB |
testcase_22 | AC | 125 ms
29,048 KB |
testcase_23 | AC | 138 ms
29,052 KB |
testcase_24 | AC | 136 ms
28,988 KB |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 1 ms
5,248 KB |
testcase_28 | AC | 85 ms
5,248 KB |
testcase_29 | AC | 95 ms
5,888 KB |
testcase_30 | AC | 100 ms
7,168 KB |
testcase_31 | AC | 106 ms
8,320 KB |
testcase_32 | AC | 108 ms
9,676 KB |
testcase_33 | AC | 111 ms
10,920 KB |
testcase_34 | AC | 117 ms
12,300 KB |
testcase_35 | AC | 120 ms
13,376 KB |
testcase_36 | AC | 127 ms
14,684 KB |
testcase_37 | AC | 129 ms
16,092 KB |
testcase_38 | AC | 135 ms
17,408 KB |
testcase_39 | AC | 140 ms
18,560 KB |
testcase_40 | AC | 144 ms
19,952 KB |
testcase_41 | AC | 146 ms
21,244 KB |
testcase_42 | AC | 150 ms
22,528 KB |
testcase_43 | AC | 154 ms
23,932 KB |
testcase_44 | AC | 157 ms
25,008 KB |
testcase_45 | AC | 161 ms
26,396 KB |
testcase_46 | AC | 166 ms
27,648 KB |
testcase_47 | AC | 163 ms
29,084 KB |
testcase_48 | AC | 143 ms
28,896 KB |
ソースコード
#include<bits/stdc++.h> namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include<atcoder/all> #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair<int,int>; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<ll>; using VVL = vector<VL>; // S : group template <class S, S (*op)(S, S), S (*e)(), S (*inv)(S)> struct weighted_union_find { public: weighted_union_find() : _n(0) {} explicit weighted_union_find(int n) : _n(n), parent_or_size(n, -1), weight(n, e()) {} int merge(int a, int b, S w) { // W(a->b) = Wa^-1 Wb = w assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); assert(x != y); if (-parent_or_size[x] < -parent_or_size[y]) { std::swap(x, y); std::swap(a, b); w = inv(w); } // Wa^-1 Wy Wb = w // Wy = Wa w Wb^-1 weight[y] = op(op(weight[a], w), inv(weight[b])); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } S diff(int a, int b) { // W(a->b) = Wa^-1 Wb int x = leader(a), y = leader(b); assert(x == y); return op(inv(weight[a]), weight[b]); } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); int pre = -1; while (parent_or_size[a] >= 0) { int na = parent_or_size[a]; parent_or_size[a] = pre; pre = a; a = na; } S w = e(); while (pre != -1) { w = op(w, weight[pre]); weight[pre] = w; int npre = parent_or_size[pre]; parent_or_size[pre] = a; pre = npre; } return a; } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector<std::vector<int>> groups() { std::vector<int> leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector<std::vector<int>> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector<int>& v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector<int> parent_or_size; std::vector<S> weight; }; int op(int x, int y) { return x ^ y; } int e() { return 0; } int inv(int x) { return x; } using mint = modint998244353; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, x, q; cin >> n >> x >> q; struct S { mint sm; int cnt[30]{}; }; vector<S> d(n); weighted_union_find<int, op, e, inv> uf(n); vector<mint> pow2(30); rep(i, 30) pow2[i] = mint(2).pow(i); rep(_, q) { int type; cin >> type; if (type == 1) { int v, w; cin >> v >> w; // cout << "merge" << v << ' ' << x << endl; int lv = uf.leader(v), lx = uf.leader(x); int szv = uf.size(lv), szx = uf.size(lx); int l = uf.merge(v, x, w); S nd; nd.sm += d[lv].sm + d[lx].sm; int diff = uf.diff(lv, lx); rep(k, 30) { if (diff >> k & 1) nd.sm += pow2[k] * (ll(szv - d[lv].cnt[k]) * (szx - d[lx].cnt[k]) + ll(d[lv].cnt[k]) * d[lx].cnt[k]); else nd.sm += pow2[k] * (ll(szv - d[lv].cnt[k]) * d[lx].cnt[k] + ll(d[lv].cnt[k]) * (szx - d[lx].cnt[k])); } rep(_, 2) { int diff = uf.diff(lv, l); rep(k, 30) { if (diff >> k & 1) nd.cnt[k] += szv - d[lv].cnt[k]; else nd.cnt[k] += d[lv].cnt[k]; } swap(lv, lx); swap(szv, szx); } d[l] = nd; } else if (type == 2) { int u, v; cin >> u >> v; int res = !uf.same(u, v) ? -1 : uf.diff(u, v); cout << res << '\n'; if (res != -1) x = (x + res) % n; } else if (type == 3) { int v; cin >> v; cout << d[uf.leader(v)].sm.val() << '\n'; } else { int value; cin >> value; x = (x + value) % n; } } }