結果
問題 | No.2295 Union Path Query (Medium) |
ユーザー |
|
提出日時 | 2023-05-05 23:18:28 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,255 bytes |
コンパイル時間 | 2,538 ms |
コンパイル使用メモリ | 215,360 KB |
最終ジャッジ日時 | 2025-02-12 19:57:34 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 WA * 1 |
other | AC * 18 WA * 35 |
ソースコード
#include<bits/stdc++.h>using namespace std;//#pragma GCC optimize("Ofast")#define rep(i,n) for(ll i=0;i<n;i++)#define repl(i,l,r) for(ll i=(l);i<(r);i++)#define per(i,n) for(ll i=(n)-1;i>=0;i--)#define perl(i,r,l) for(ll i=r-1;i>=l;i--)#define fi first#define se second#define pb push_back#define ins insert#define pqueue(x) priority_queue<x,vector<x>,greater<x>>#define all(x) (x).begin(),(x).end()#define CST(x) cout<<fixed<<setprecision(x)#define rev(x) reverse(x);using ll=long long;using vl=vector<ll>;using vvl=vector<vector<ll>>;using pl=pair<ll,ll>;using vpl=vector<pl>;using vvpl=vector<vpl>;const ll MOD=1000000007;const ll MOD9=998244353;const int inf=1e9+10;const ll INF=4e18;const ll dy[8]={1,0,-1,0,1,1,-1,-1};const ll dx[8]={0,1,0,-1,1,-1,1,-1};template <typename T> inline bool chmax(T &a, T b) {return ((a < b) ? (a = b, true) : (false));}template <typename T> inline bool chmin(T &a, T b) {return ((a > b) ? (a = b, true) : (false));}vl ans(300010,-1);vector<vpl> meet;struct UnionFind {vector<int> par;vector<int> edge;vector<ll> fs;vector<set<ll>> com;UnionFind(int n) : par(n, -1),edge(n, 0) {fs.resize(n);com.resize(n);rep(i,n)com[i].insert(i);}int root(int x) {if (par[x] < 0) return x;else return par[x] = root(par[x]);}bool same(int x, int y) {return root(x) == root(y);}bool merge(int x, int y,ll w) {x = root(x); y = root(y);if (x == y) {edge[x]++;return false;}if (par[x] > par[y]) {swap(x, y);}ll p=size(x),q=size(y);par[x] += par[y];par[y] = x;edge[x] += edge[y]+1;p*=q;p%=MOD9;p*=w;fs[x]=(fs[x]+fs[y])%MOD9;fs[x]=(fs[x]+p)%MOD9;if(com[x].size()<com[y].size())swap(com[x],com[y]);for(auto p:com[y]){for(auto q:meet[p]){if(com[x].count(q.first)){ans[q.second]=w;}}}for(auto p:com[y])com[x].insert(p);return true;}int size(int x) {return -par[root(x)];}ll query(int x) {x=root(x);return fs[x];}};int main(){ll n,x,q;cin >> n >> x >> q;meet.resize(n);UnionFind uf(n);vvl qs(q);ll idx=0;//return 0;rep(i,q){ll t;cin >> t;qs[i].emplace_back(t);if(t==1){ll v,w;cin >> v >> w;qs[i].emplace_back(v);qs[i].emplace_back(w);}else if(t==2){ll u,v;cin >> u >> v;meet[u].push_back({v,idx});meet[v].push_back({u,idx});qs[i].emplace_back(idx);idx++;}else if(t==3){ll v;cin >> v;qs[i].emplace_back(v);}else{ll v;cin >> v;qs[i].emplace_back(v);}}//return 0;rep(i,q){ll t=qs[i][0];if(t==1){uf.merge(qs[i][1],x,qs[i][2]);}else if(t==2){ll f=ans[qs[i][1]];cout << f << endl;if(f!=-1)x+=f;x%=n;}else if(t==3){cout << uf.query(qs[i][1]) << endl;}else{ll v=qs[i][1];x+=v;x%=n;}/*rep(i,n){for(auto p:uf.sts[i])cout << p.first <<" " << p.second <<" ";cout << endl;}*/}}