結果

問題 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,607 ms / 4,000 ms
コード長 1,994 bytes
コンパイル時間 2,234 ms
コンパイル使用メモリ 218,196 KB
実行使用メモリ 91,904 KB
最終ジャッジ日時 2024-05-02 18:19:37
合計ジャッジ時間 60,419 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
22,272 KB
testcase_01 AC 16 ms
22,400 KB
testcase_02 AC 15 ms
22,272 KB
testcase_03 AC 1,607 ms
81,664 KB
testcase_04 AC 1,408 ms
77,952 KB
testcase_05 AC 1,053 ms
72,192 KB
testcase_06 AC 815 ms
68,736 KB
testcase_07 AC 1,246 ms
73,344 KB
testcase_08 AC 501 ms
44,544 KB
testcase_09 AC 505 ms
44,544 KB
testcase_10 AC 584 ms
46,080 KB
testcase_11 AC 744 ms
55,168 KB
testcase_12 AC 999 ms
67,200 KB
testcase_13 AC 1,463 ms
91,904 KB
testcase_14 AC 548 ms
37,248 KB
testcase_15 AC 475 ms
40,704 KB
testcase_16 AC 472 ms
40,704 KB
testcase_17 AC 840 ms
53,248 KB
testcase_18 AC 1,191 ms
63,616 KB
testcase_19 AC 1,365 ms
69,888 KB
testcase_20 AC 1,076 ms
62,208 KB
testcase_21 AC 1,120 ms
63,488 KB
testcase_22 AC 1,375 ms
69,888 KB
testcase_23 AC 920 ms
62,592 KB
testcase_24 AC 917 ms
62,592 KB
testcase_25 AC 315 ms
37,504 KB
testcase_26 AC 1,012 ms
61,440 KB
testcase_27 AC 950 ms
60,800 KB
testcase_28 AC 962 ms
62,208 KB
testcase_29 AC 1,001 ms
59,904 KB
testcase_30 AC 968 ms
59,904 KB
testcase_31 AC 963 ms
59,520 KB
testcase_32 AC 985 ms
59,392 KB
testcase_33 AC 978 ms
59,264 KB
testcase_34 AC 937 ms
59,136 KB
testcase_35 AC 942 ms
59,136 KB
testcase_36 AC 1,135 ms
58,880 KB
testcase_37 AC 1,165 ms
59,136 KB
testcase_38 AC 1,098 ms
58,880 KB
testcase_39 AC 1,000 ms
59,136 KB
testcase_40 AC 1,010 ms
58,880 KB
testcase_41 AC 1,029 ms
59,136 KB
testcase_42 AC 953 ms
59,008 KB
testcase_43 AC 1,000 ms
59,136 KB
testcase_44 AC 900 ms
57,984 KB
testcase_45 AC 991 ms
62,592 KB
testcase_46 AC 1,039 ms
59,008 KB
testcase_47 AC 1,115 ms
59,904 KB
testcase_48 AC 1,275 ms
60,544 KB
testcase_49 AC 1,280 ms
59,648 KB
testcase_50 AC 660 ms
47,616 KB
testcase_51 AC 830 ms
54,656 KB
testcase_52 AC 860 ms
55,808 KB
testcase_53 AC 1,005 ms
57,472 KB
testcase_54 AC 949 ms
58,496 KB
testcase_55 AC 882 ms
56,576 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