結果

問題 No.2295 Union Path Query (Medium)
ユーザー 👑 kmjpkmjp
提出日時 2023-05-05 23:11:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,935 ms / 4,000 ms
コード長 1,994 bytes
コンパイル時間 4,297 ms
コンパイル使用メモリ 216,072 KB
実行使用メモリ 93,196 KB
最終ジャッジ日時 2023-08-15 06:17:43
合計ジャッジ時間 70,624 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
24,144 KB
testcase_01 AC 11 ms
22,492 KB
testcase_02 AC 11 ms
22,572 KB
testcase_03 AC 1,935 ms
81,636 KB
testcase_04 AC 1,622 ms
79,284 KB
testcase_05 AC 1,173 ms
73,124 KB
testcase_06 AC 896 ms
69,520 KB
testcase_07 AC 1,550 ms
74,848 KB
testcase_08 AC 573 ms
44,592 KB
testcase_09 AC 574 ms
44,576 KB
testcase_10 AC 653 ms
46,032 KB
testcase_11 AC 892 ms
55,236 KB
testcase_12 AC 1,241 ms
67,748 KB
testcase_13 AC 1,858 ms
93,196 KB
testcase_14 AC 565 ms
38,392 KB
testcase_15 AC 645 ms
40,668 KB
testcase_16 AC 645 ms
40,748 KB
testcase_17 AC 997 ms
53,364 KB
testcase_18 AC 1,409 ms
63,600 KB
testcase_19 AC 1,646 ms
69,916 KB
testcase_20 AC 1,242 ms
62,252 KB
testcase_21 AC 1,278 ms
63,520 KB
testcase_22 AC 1,647 ms
69,888 KB
testcase_23 AC 1,159 ms
64,044 KB
testcase_24 AC 1,136 ms
63,864 KB
testcase_25 AC 377 ms
38,696 KB
testcase_26 AC 1,163 ms
62,664 KB
testcase_27 AC 1,164 ms
61,964 KB
testcase_28 AC 1,199 ms
63,372 KB
testcase_29 AC 1,169 ms
61,192 KB
testcase_30 AC 1,184 ms
61,208 KB
testcase_31 AC 1,191 ms
61,020 KB
testcase_32 AC 1,193 ms
60,752 KB
testcase_33 AC 1,198 ms
60,552 KB
testcase_34 AC 1,202 ms
60,376 KB
testcase_35 AC 1,208 ms
60,324 KB
testcase_36 AC 1,209 ms
60,204 KB
testcase_37 AC 1,226 ms
60,452 KB
testcase_38 AC 1,200 ms
60,284 KB
testcase_39 AC 1,237 ms
60,244 KB
testcase_40 AC 1,200 ms
60,076 KB
testcase_41 AC 1,203 ms
60,240 KB
testcase_42 AC 1,226 ms
60,476 KB
testcase_43 AC 1,196 ms
60,448 KB
testcase_44 AC 1,057 ms
59,356 KB
testcase_45 AC 1,134 ms
63,880 KB
testcase_46 AC 1,300 ms
60,056 KB
testcase_47 AC 1,344 ms
60,384 KB
testcase_48 AC 1,546 ms
60,496 KB
testcase_49 AC 1,527 ms
59,580 KB
testcase_50 AC 812 ms
48,928 KB
testcase_51 AC 1,076 ms
55,724 KB
testcase_52 AC 1,056 ms
57,124 KB
testcase_53 AC 1,162 ms
58,728 KB
testcase_54 AC 1,132 ms
58,984 KB
testcase_55 AC 1,035 ms
57,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

ll SD[202020];

set<int> S[202020];
int AA[302020],BB[302020],CC[302020];
int C[202020];
int N,X,Q;
const ll mo=998244353;
map<pair<int,int>,int> ret;
set<int> cand[202020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>X>>Q;
	FOR(i,N) {
		C[i]=i;
		S[i].insert(i);
	}
	FOR(i,Q) {
		cin>>AA[i]>>BB[i];
		
		if(AA[i]==1) {
			cin>>CC[i];
		}
		if(AA[i]==2) {
			cin>>CC[i];
			if(BB[i]==CC[i]) {
				ret[{BB[i],CC[i]}]=0;
			}
			else {
				cand[BB[i]].insert(CC[i]);
				cand[CC[i]].insert(BB[i]);
			}
		}
	}
	
	FOR(i,Q) {
		if(AA[i]==1) {
			int a=C[BB[i]];
			int b=C[X];
			if(a==b) continue;
			if(S[a].size()<S[b].size()) swap(a,b);
			SD[a]=(SD[a]+SD[b]+1LL*S[a].size()*S[b].size()%mo*CC[i])%mo;
			
			FORR(c,S[b]) {
				FORR(d,cand[c]) if(S[a].count(d)) {
					ret[{c,d}]=ret[{d,c}]=CC[i];
				}
			}
			FORR(c,S[b]) {
				C[c]=a;
				S[a].insert(c);
			}
			S[b].clear();
		}
		else if(AA[i]==2) {
			if(C[BB[i]]!=C[CC[i]]) {
				cout<<-1<<endl;
			}
			else {
				assert(ret.count({BB[i],CC[i]}));
				x=ret[{BB[i],CC[i]}];
				cout<<x<<endl;
				X=(X+x)%N;
			}
		}
		else if(AA[i]==3) {
			cout<<SD[C[BB[i]]]<<endl;
		}
		else if(AA[i]==4) {
			X=(X+BB[i])%N;
		}
	}
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0