結果

問題 No.2295 Union Path Query (Medium)
ユーザー 沙耶花沙耶花
提出日時 2023-05-05 22:49:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,281 bytes
コンパイル時間 5,074 ms
コンパイル使用メモリ 274,076 KB
実行使用メモリ 217,012 KB
最終ジャッジ日時 2024-11-23 10:35:00
合計ジャッジ時間 192,320 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
38,352 KB
testcase_01 AC 3 ms
10,496 KB
testcase_02 AC 3 ms
10,496 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 AC 3,713 ms
63,712 KB
testcase_06 AC 2,387 ms
113,704 KB
testcase_07 TLE -
testcase_08 AC 2,452 ms
113,948 KB
testcase_09 AC 2,390 ms
113,948 KB
testcase_10 AC 2,999 ms
142,516 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 2,408 ms
15,776 KB
testcase_15 AC 682 ms
108,444 KB
testcase_16 AC 671 ms
108,444 KB
testcase_17 AC 1,667 ms
60,316 KB
testcase_18 AC 3,903 ms
58,652 KB
testcase_19 TLE -
testcase_20 AC 2,713 ms
58,780 KB
testcase_21 AC 2,703 ms
58,524 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 AC 1,178 ms
108,572 KB
testcase_26 AC 3,890 ms
108,588 KB
testcase_27 AC 3,661 ms
108,564 KB
testcase_28 AC 3,687 ms
108,696 KB
testcase_29 AC 3,362 ms
108,444 KB
testcase_30 AC 3,596 ms
108,436 KB
testcase_31 AC 3,404 ms
108,440 KB
testcase_32 AC 3,279 ms
108,436 KB
testcase_33 AC 3,260 ms
108,440 KB
testcase_34 AC 3,195 ms
108,436 KB
testcase_35 AC 3,152 ms
108,440 KB
testcase_36 AC 3,169 ms
108,440 KB
testcase_37 AC 3,141 ms
108,440 KB
testcase_38 AC 3,094 ms
108,440 KB
testcase_39 AC 3,068 ms
108,444 KB
testcase_40 AC 2,986 ms
108,568 KB
testcase_41 AC 3,030 ms
108,440 KB
testcase_42 AC 3,108 ms
108,444 KB
testcase_43 AC 3,197 ms
108,444 KB
testcase_44 TLE -
testcase_45 TLE -
testcase_46 AC 3,136 ms
108,400 KB
testcase_47 AC 3,333 ms
108,568 KB
testcase_48 TLE -
testcase_49 TLE -
testcase_50 AC 2,461 ms
108,436 KB
testcase_51 AC 3,123 ms
108,440 KB
testcase_52 AC 3,463 ms
108,564 KB
testcase_53 AC 3,103 ms
108,440 KB
testcase_54 AC 3,151 ms
108,460 KB
testcase_55 AC 3,141 ms
217,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001

template <typename T>
struct perArray{
	struct node{
		T value;
		int l=-1,r=-1;
	};
	
	int sz;
	vector<node> nodes;
	vector<int> roots;
	
	void build(vector<T> v,int now = -1){
		if(now==-1){
			sz = v.size();
			nodes.push_back(node());
			now = 0;
			roots.push_back(0);
		}
		
		if(v.size()==1){
			nodes[now].value = v[0];
			return;
		}
		
		vector<T> l,r;
		rep(i,v.size()){
			if(i<v.size()/2){
				l.push_back(v[i]);
			}
			else{
				r.push_back(v[i]);
			}
		}
		
		nodes[now].l = nodes.size();
		nodes.push_back(node());
		build(l,nodes[now].l);
		
		nodes[now].r = nodes.size();
		nodes.push_back(node());
		build(r,nodes[now].r);
	}

	int set(int p, int ind, T x, int now = -1, int length = -1){
		int ret = roots.size();
		if(now==-1){
			roots.push_back(nodes.size());
			now = nodes.size();
			nodes.push_back(nodes[roots[p]]);
			length = sz;
		}
		
		if(length==1){
			nodes[now].value = x;
		}
		else{
			if(ind < (length>>1)){
				int temp = nodes[now].l;
				nodes[now].l = nodes.size();
				nodes.push_back(nodes[temp]);
				set(p,ind,x,nodes.size()-1,(length>>1));
			}
			else{
				int temp = nodes[now].r;
				nodes[now].r = nodes.size();
				nodes.push_back(nodes[temp]);
				set(p,ind - (length>>1),x,nodes.size()-1,(length+1)>>1);
			}
		}
		return ret;
	}
	
	T get(int p,int ind,int now = -1,int length = -1){
		if(now==-1){
			now = roots[p];
			length = sz;
		}
		
		if(length==1)return nodes[now].value;
		if(ind<(length>>1)){
			return get(p,ind,nodes[now].l,length>>1);
		}
		else{
			return get(p,ind - (length>>1),nodes[now].r,(length+1)>>1);
		}
	}
	
	int size(){
		return sz;
	}
	
};

struct perDsu{
	perArray<int> A;
	vector<int> roots;
	perDsu(int n){
		vector<int> t(n,-1);
		A.build(t);
		roots.push_back(0);
	}
	
	int merge(int p,int a,int b){
		int ret = roots.size();
		int La = leader(p,a);
		int Lb = leader(p,b);
		if(La==Lb)return p;
		if(size(p,La)<size(p,Lb))swap(La,Lb);
		int temp = A.set(roots[p],La,-(size(p,La)+size(p,Lb)));
		roots.push_back(A.set(temp,Lb,La));
		return ret;
	}
	
	bool same(int p,int a,int b){
		return (leader(p,a)==leader(p,b));
	}
	
	int size(int p,int a){
		int temp = A.get(roots[p],a);
		if(temp<0)return -temp;
		return size(p,temp);
	}
	
	int leader(int p,int a){
		int temp = A.get(roots[p],a);
		if(temp<0)return a;
		return leader(p,temp);
	}
	
	vector<vector<int>> groups(int p){
		vector<vector<int>> temp(A.size(),vector<int>());
		rep(i,A.size()){
			temp[leader(p,i)].push_back(i);
		}
		vector<vector<int>> ret;
		rep(i,temp.size()){
			if(temp[i].size()>0)ret.push_back(temp[i]);
		}
		return ret;
	}
	
};

int main(){
	
	int n,x0,q;
	cin>>n>>x0>>q;
	perDsu D(n);
	
	vector<int> t(q+1),a(q+1),b(q+1),p(q+1);
	int x=  x0;
	for(int i=1;i<=q;i++){
		cin>>t[i];
		p[i] = p[i-1];
		if(t[i]==1){
			cin>>a[i]>>b[i];
			p[i] = D.merge(p[i],a[i],x);
		}
		if(t[i]==2){
			cin>>a[i]>>b[i];
			if(a[i]==b[i])continue;
			if(D.same(p[i],a[i],b[i])){
				int ok = 0,ng = i;
				while(ng-ok>1){
					int mid = (ok+ng)/2;
					if(D.same(p[mid],a[i],b[i]))ng = mid;
					else ok = mid;
				}
				x += b[ok+1];
				x %= n;
			}
		}
		if(t[i]==3){
			cin>>a[i];
		}
		if(t[i]==4){
			cin>>a[i];
			x += a[i];
			x %= n;
		}
	}
	//cout<<'a'<<endl;
	x = x0;
	dsu D2(n);
	vector<mint> A(n);
	
	for(int i=1;i<=q;i++){
		if(t[i]==1){
			int la = D2.leader(a[i]),lb = D2.leader(x);
			if(D2.same(la,lb))continue;
			mint tt = D2.size(a[i]);
			tt *= D2.size(x);
			tt *= b[i];
			tt += A[la] + A[lb];
			
			D2.merge(la,lb);
			int lc = D2.leader(la);
			int ld = lc^la^lb;
			A[ld] = 0;
			A[lc] = tt;
		}
		if(t[i]==2){
			if(a[i]==b[i]){
				cout<<0<<endl;
				continue;
			}
			if(D.same(p[i],a[i],b[i])){
				int ok = 0,ng = i;
				while(ng-ok>1){
					int mid = (ok+ng)/2;
					if(D.same(p[mid],a[i],b[i]))ng = mid;
					else ok = mid;
				}
				x += b[ok+1];
				x %= n;
				cout<<b[ok+1]<<endl;
			}
			else cout<<-1<<endl;
		}
		if(t[i]==3){
			cout<<A[D2.leader(a[i])].val()<<endl;
		}
		if(t[i]==4){
			x += a[i];
			x %= n;
		}
	}
	
	
	return 0;
}
0