結果

問題 No.2295 Union Path Query (Medium)
ユーザー 👑 potato167potato167
提出日時 2023-05-05 22:03:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,208 ms / 4,000 ms
コード長 3,791 bytes
コンパイル時間 4,883 ms
コンパイル使用メモリ 223,784 KB
実行使用メモリ 157,368 KB
最終ジャッジ日時 2023-08-15 04:07:46
合計ジャッジ時間 34,863 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1,189 ms
157,368 KB
testcase_04 AC 917 ms
131,928 KB
testcase_05 AC 581 ms
89,716 KB
testcase_06 AC 356 ms
59,892 KB
testcase_07 AC 946 ms
131,700 KB
testcase_08 AC 213 ms
42,772 KB
testcase_09 AC 215 ms
42,840 KB
testcase_10 AC 236 ms
43,748 KB
testcase_11 AC 349 ms
49,816 KB
testcase_12 AC 527 ms
57,060 KB
testcase_13 AC 1,208 ms
73,612 KB
testcase_14 AC 728 ms
72,820 KB
testcase_15 AC 126 ms
38,456 KB
testcase_16 AC 127 ms
38,456 KB
testcase_17 AC 199 ms
42,812 KB
testcase_18 AC 500 ms
80,676 KB
testcase_19 AC 554 ms
83,572 KB
testcase_20 AC 364 ms
58,716 KB
testcase_21 AC 400 ms
62,984 KB
testcase_22 AC 575 ms
83,372 KB
testcase_23 AC 371 ms
52,400 KB
testcase_24 AC 371 ms
52,400 KB
testcase_25 AC 150 ms
39,096 KB
testcase_26 AC 375 ms
53,892 KB
testcase_27 AC 375 ms
55,216 KB
testcase_28 AC 394 ms
56,764 KB
testcase_29 AC 387 ms
56,568 KB
testcase_30 AC 386 ms
56,724 KB
testcase_31 AC 384 ms
57,116 KB
testcase_32 AC 388 ms
57,380 KB
testcase_33 AC 394 ms
58,312 KB
testcase_34 AC 391 ms
57,596 KB
testcase_35 AC 394 ms
57,808 KB
testcase_36 AC 391 ms
57,928 KB
testcase_37 AC 398 ms
59,256 KB
testcase_38 AC 394 ms
58,428 KB
testcase_39 AC 402 ms
59,268 KB
testcase_40 AC 388 ms
58,360 KB
testcase_41 AC 391 ms
58,364 KB
testcase_42 AC 396 ms
59,104 KB
testcase_43 AC 397 ms
58,284 KB
testcase_44 AC 378 ms
50,108 KB
testcase_45 AC 373 ms
52,404 KB
testcase_46 AC 455 ms
62,316 KB
testcase_47 AC 476 ms
65,072 KB
testcase_48 AC 529 ms
69,364 KB
testcase_49 AC 481 ms
64,216 KB
testcase_50 AC 247 ms
46,204 KB
testcase_51 AC 332 ms
52,892 KB
testcase_52 AC 323 ms
50,692 KB
testcase_53 AC 374 ms
57,300 KB
testcase_54 AC 383 ms
59,664 KB
testcase_55 AC 352 ms
56,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const int mod=998244353;
#define rep(i,a,b) for (int i=a;i<b;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}


namespace po167{
struct UFtree
{
	using _F=ll;
	int _n;
	std::vector<_F> wei;
	vector<ll> cou;
	std::vector<int> q;
	vector<ll> ans;
	vector<set<int>> s;
	int component;
	UFtree(int n,int qu):_n(n),wei(n),cou(n),ans(qu),s(n),component(n),par(n){
		for(int i=0;i<n;i++){
			wei[i]=0,par[i]=i;
			cou[i]=1;
		}
		rep(i,0,qu) ans[i]=-1;
	}
	void rev(int a,int b,int t){
		if(a==b){
			ans[t]=0;
			return;
		}
		s[a].insert(t);
		s[b].insert(t);
		//cout<<a<<" "<<b<<" "<<s[a].size()<<" "<<s[b].size()<<endl;
	}
	void intialize(){
		for(auto x:q){
			wei[root(x)]=1;
			par[x]=x;
		}
		component=(int)par.size();
		q={};
	}
	//根っこを返す
	int root(int a){
		assert(0<=a&&a<_n);
		if(a==par[a]) return a;
		return par[a]=root(par[a]);
	}
	//trueなら1,falseなら0
	int same(int a,int b){
		assert(0<=a&&a<_n);
		assert(0<=b&&b<_n);
		if(root(a)==root(b)) return 1;
		else return 0;
	}
	_F size(int a){
		return wei[root(a)];
	}
	//a,bが違う根っこの元なら結合する,結合したらtrueを返す
	bool unite(int a,int b,ll w){
		a=root(a),b=root(b);
		if(a==b) return false;
		if(cou[a]<cou[b]) std::swap(a,b);
		par[b]=a;
		q.push_back(b);
		wei[a]+=wei[b];
		wei[a]+=(((cou[a]*cou[b])%mod)*w)%mod;
		wei[a]%=mod;
		cou[a]+=cou[b];
		if(s[a].size()<s[b].size()) swap(s[a],s[b]);
		//cout<<s[a].size()<<" "<<s[b].size()<<endl;
		for(auto x:s[b]){
			if(!s[a].insert(x).second){
				ans[x]=w;
				s[a].erase(x);
			}
		}
		component--;
		return true;
	}
	private:
	std::vector<int> par;
};
}
using po167::UFtree;


void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,0,t) solve();
}

void solve(){
	ll N,X,Q;
	cin>>N>>X>>Q;
	UFtree T(N,Q);
	vector<ll> typ(Q);
	vector<vector<ll>> op(Q);
	rep(i,0,Q){
		cin>>typ[i];
		if(typ[i]<=2){
			op[i].resize(2);
		}
		else op[i].resize(1);
		for(auto &x:op[i]) cin>>x;
		if(typ[i]==2){
			T.rev(op[i][0],op[i][1],i);
		}
	}
	rep(i,0,Q){
		//vec_out(T.ans);
		if(typ[i]==1){
			T.unite(op[i][0],X,op[i][1]);
		}
		if(typ[i]==2){
			ll v=T.ans[i];
			cout<<v<<"\n";
			if(v!=-1) X=(X+v)%N;
		}
		if(typ[i]==3){
			op[i][0]=T.root(op[i][0]);
			cout<<T.wei[op[i][0]]%mod<<"\n";
		}
		if(typ[i]==4){
			X=(X+op[i][0])%N;
		}
	}
}
0