結果
問題 | No.2295 Union Path Query (Medium) |
ユーザー | Ecrade_ |
提出日時 | 2023-05-05 23:26:22 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 432 ms / 4,000 ms |
コード長 | 2,725 bytes |
コンパイル時間 | 1,642 ms |
コンパイル使用メモリ | 170,708 KB |
実行使用メモリ | 84,928 KB |
最終ジャッジ日時 | 2024-11-23 12:31:43 |
合計ジャッジ時間 | 19,752 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 3 ms
5,248 KB |
testcase_02 | AC | 3 ms
5,248 KB |
testcase_03 | AC | 124 ms
24,064 KB |
testcase_04 | AC | 139 ms
32,128 KB |
testcase_05 | AC | 160 ms
44,288 KB |
testcase_06 | AC | 179 ms
52,480 KB |
testcase_07 | AC | 138 ms
24,064 KB |
testcase_08 | AC | 154 ms
81,792 KB |
testcase_09 | AC | 153 ms
81,792 KB |
testcase_10 | AC | 165 ms
82,048 KB |
testcase_11 | AC | 205 ms
83,072 KB |
testcase_12 | AC | 208 ms
81,024 KB |
testcase_13 | AC | 178 ms
68,480 KB |
testcase_14 | AC | 74 ms
12,032 KB |
testcase_15 | AC | 390 ms
82,432 KB |
testcase_16 | AC | 386 ms
82,560 KB |
testcase_17 | AC | 299 ms
80,128 KB |
testcase_18 | AC | 288 ms
79,360 KB |
testcase_19 | AC | 289 ms
79,360 KB |
testcase_20 | AC | 270 ms
79,616 KB |
testcase_21 | AC | 258 ms
79,360 KB |
testcase_22 | AC | 432 ms
81,792 KB |
testcase_23 | AC | 194 ms
83,328 KB |
testcase_24 | AC | 201 ms
83,456 KB |
testcase_25 | AC | 136 ms
83,456 KB |
testcase_26 | AC | 211 ms
83,328 KB |
testcase_27 | AC | 214 ms
83,456 KB |
testcase_28 | AC | 222 ms
83,328 KB |
testcase_29 | AC | 216 ms
83,328 KB |
testcase_30 | AC | 222 ms
83,456 KB |
testcase_31 | AC | 214 ms
83,328 KB |
testcase_32 | AC | 213 ms
83,456 KB |
testcase_33 | AC | 221 ms
83,456 KB |
testcase_34 | AC | 224 ms
83,328 KB |
testcase_35 | AC | 219 ms
83,328 KB |
testcase_36 | AC | 208 ms
83,456 KB |
testcase_37 | AC | 224 ms
83,328 KB |
testcase_38 | AC | 231 ms
83,328 KB |
testcase_39 | AC | 222 ms
83,328 KB |
testcase_40 | AC | 215 ms
83,200 KB |
testcase_41 | AC | 224 ms
83,456 KB |
testcase_42 | AC | 223 ms
83,328 KB |
testcase_43 | AC | 219 ms
83,456 KB |
testcase_44 | AC | 177 ms
83,328 KB |
testcase_45 | AC | 206 ms
83,200 KB |
testcase_46 | AC | 232 ms
83,840 KB |
testcase_47 | AC | 232 ms
84,352 KB |
testcase_48 | AC | 320 ms
84,928 KB |
testcase_49 | AC | 337 ms
84,848 KB |
testcase_50 | AC | 190 ms
83,456 KB |
testcase_51 | AC | 211 ms
83,456 KB |
testcase_52 | AC | 213 ms
83,456 KB |
testcase_53 | AC | 216 ms
83,584 KB |
testcase_54 | AC | 226 ms
84,096 KB |
testcase_55 | AC | 224 ms
84,480 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 998244353; ll t = 1,n,m,k,q,l,r,a,b,c,u,v,x,y,z,o,X,hd[200009],tot,dep[200009],anc[200009][20],dis[200009][20],rt[200009],id[200009],sz[200009],ans[200009]; string s; struct st{ll x,y,z; /*bool operator < (const st &a) const{return x < a.x;};*/}eg[400009]; void addedge(ll u,ll v,ll w){eg[++ tot] = (st){v,hd[u],w},hd[u] = tot;} bool cmp(const st &a,const st &b){return a.x < b.x;} inline ll read(){ ll s = 0,w = 1; char ch = getchar(); while (ch > '9' || ch < '0'){ if (ch == '-') w = -1; ch = getchar();} while (ch <= '9' && ch >= '0') s = (s << 1) + (s << 3) + (ch ^ 48),ch = getchar(); return s * w; } ll qp(ll x,ll y){ ll a = 1,b = x; while (y){ if (y & 1) a = a * b % mod; b = b * b % mod,y >>= 1; } return a; } inline void add(ll &x,ll y){x += y; if (x >= mod) x -= mod;} ll find(ll x){return x == id[x] ? x : id[x] = find(id[x]);} void dfs(ll u,ll fa,ll rt){ for (ll i = 1;i < 20;i += 1) anc[u][i] = anc[anc[u][i - 1]][i - 1]; for (ll i = 1;i < 20;i += 1) dis[u][i] = max(dis[u][i - 1],dis[anc[u][i - 1]][i - 1]); for (ll i = hd[u];i;i = eg[i].y){ ll v = eg[i].x; if (v == fa) continue; dep[v] = dep[u] + 1,anc[v][0] = u,dis[v][0] = eg[i].z,dfs(v,u,rt); } } void merge(ll x,ll y,ll z){ ll qwq = find(x),awa = find(y); if (qwq == awa) return; if (sz[qwq] > sz[awa]) swap(x,y),swap(qwq,awa); id[qwq] = awa,ans[awa] += z * sz[qwq] % mod * sz[awa] % mod + ans[qwq],sz[awa] += sz[qwq],ans[awa] %= mod; dep[x] = dep[y] + 1,anc[x][0] = y,dis[x][0] = z,addedge(x,y,z),addedge(y,x,z),dfs(x,y,awa); } ll query(ll x,ll y){ ll ans = 0; for (ll i = 19;i >= 0;i -= 1){ if (!anc[x][i]) continue; if (dep[anc[x][i]] < dep[y]) continue; ans = max(ans,dis[x][i]),x = anc[x][i]; } for (ll i = 19;i >= 0;i -= 1){ if (!anc[y][i]) continue; if (dep[anc[y][i]] < dep[x]) continue; ans = max(ans,dis[y][i]),y = anc[y][i]; } for (ll i = 19;i >= 0;i -= 1){ if (anc[x][i] == anc[y][i]) continue; ans = max(ans,dis[x][i]),ans = max(ans,dis[y][i]); x = anc[x][i],y = anc[y][i]; } if (x != y) ans = max(ans,dis[x][0]),ans = max(ans,dis[y][0]); return ans; } int main(){//t = read(); while (t --){ n = read(),X = read() + 1,q = read(); for (ll i = 1;i <= n;i += 1){ rt[i] = id[i] = i,sz[i] = 1; } while (q --){ o = read(); if (o == 1){ x = read() + 1,y = read(); merge(x,X,y); } if (o == 2){ x = read() + 1,y = read() + 1; if (find(x) != find(y)) puts("-1"); else{ ll qwq = query(x,y); X += qwq; printf("%lld\n",qwq); } } if (o == 3){ x = read() + 1; printf("%lld\n",ans[find(x)]); } if (o == 4) X += read(); X = (X + n - 1) % n + 1; } } return 0; }