結果

問題 No.2296 Union Path Query (Hard)
ユーザー 👑 kmjpkmjp
提出日時 2023-05-13 10:27:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 932 ms / 7,000 ms
コード長 2,487 bytes
コンパイル時間 3,058 ms
コンパイル使用メモリ 206,980 KB
実行使用メモリ 38,240 KB
最終ジャッジ日時 2023-08-19 11:55:49
合計ジャッジ時間 42,095 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
25,104 KB
testcase_01 AC 12 ms
29,220 KB
testcase_02 AC 12 ms
29,168 KB
testcase_03 AC 12 ms
29,156 KB
testcase_04 AC 790 ms
32,296 KB
testcase_05 AC 720 ms
32,216 KB
testcase_06 AC 635 ms
33,816 KB
testcase_07 AC 741 ms
35,236 KB
testcase_08 AC 747 ms
35,200 KB
testcase_09 AC 753 ms
31,512 KB
testcase_10 AC 739 ms
31,520 KB
testcase_11 AC 740 ms
31,480 KB
testcase_12 AC 704 ms
30,732 KB
testcase_13 AC 543 ms
25,828 KB
testcase_14 AC 529 ms
25,404 KB
testcase_15 AC 733 ms
34,520 KB
testcase_16 AC 727 ms
33,368 KB
testcase_17 AC 648 ms
28,112 KB
testcase_18 AC 902 ms
36,200 KB
testcase_19 AC 865 ms
31,064 KB
testcase_20 AC 907 ms
36,216 KB
testcase_21 AC 932 ms
34,684 KB
testcase_22 AC 930 ms
34,356 KB
testcase_23 AC 629 ms
36,680 KB
testcase_24 AC 779 ms
30,936 KB
testcase_25 AC 360 ms
35,688 KB
testcase_26 AC 371 ms
35,504 KB
testcase_27 AC 807 ms
35,500 KB
testcase_28 AC 824 ms
35,668 KB
testcase_29 AC 854 ms
36,448 KB
testcase_30 AC 824 ms
36,436 KB
testcase_31 AC 868 ms
38,172 KB
testcase_32 AC 839 ms
37,788 KB
testcase_33 AC 827 ms
36,324 KB
testcase_34 AC 696 ms
34,428 KB
testcase_35 AC 675 ms
34,112 KB
testcase_36 AC 692 ms
32,000 KB
testcase_37 AC 877 ms
36,876 KB
testcase_38 AC 881 ms
36,932 KB
testcase_39 AC 852 ms
36,824 KB
testcase_40 AC 871 ms
36,848 KB
testcase_41 AC 7 ms
25,136 KB
testcase_42 AC 7 ms
25,216 KB
testcase_43 AC 7 ms
25,092 KB
testcase_44 AC 662 ms
36,708 KB
testcase_45 AC 677 ms
36,760 KB
testcase_46 AC 718 ms
38,240 KB
testcase_47 AC 737 ms
37,896 KB
testcase_48 AC 730 ms
37,444 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;}
//-------------------------------------------------------

int N,Q;
ll X;
vector<pair<int,int>> E[200005];
int P[21][200005],D[200005];
ll LD[202020];
int V[202020];
pair<int,int> DM[202020];

void dfs(int cur,int pre,int d,ll ld) {
	P[0][cur]=pre;
	D[cur]=d;
	LD[cur]=ld;
	int i;
	FOR(i,20) P[i+1][cur]=P[i][P[i][cur]];
	FORR2(e,c,E[cur]) if(e!=pre) dfs(e,cur,d+1,ld+c);
	
}

int lca(int a,int b) {
	int ret=0,i,aa=a,bb=b;
	if(D[aa]>D[bb]) swap(aa,bb);
	for(i=19;i>=0;i--) if(D[bb]-D[aa]>=1<<i) bb=P[i][bb];
	for(i=19;i>=0;i--) if(P[i][aa]!=P[i][bb]) aa=P[i][aa], bb=P[i][bb];
	return (aa==bb)?aa:P[0][aa];               // vertex
}

int getroot(int x) {
	return P[20][x];
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>X>>Q;
	FOR(i,N) {
		V[i]=1;
		DM[i]={i,i};
		FOR(j,21) P[j][i]=i;
	}
	
	while(Q--) {
		cin>>i;
		if(i==1) {
			cin>>x>>y;
			i=X;
			j=x;
			if(V[getroot(i)]<V[getroot(j)]) {
				swap(i,j);
			}
			V[getroot(i)]+=V[getroot(j)];
			int oj=getroot(j);
			dfs(j,i,D[i]+1,LD[i]+y);
			E[i].push_back({j,y});
			E[j].push_back({i,y});
			i=getroot(i);
			j=oj;
			vector<int> V={DM[i].first,DM[i].second,DM[j].first,DM[j].second};
			ll md=-1;
			FORR(v,V) FORR(w,V) if(v<w) {
				ll lc=lca(v,w);
				ll ret=LD[v]+LD[w]-2*LD[lc];
				if(ret>md) {
					md=ret;
					DM[i]={v,w};
				}
			}
			
			
		}
		else if(i==2) {
			cin>>x>>y;
			
			if(getroot(x)!=getroot(y)) {
				cout<<-1<<endl;
			}
			else {
				ll lc=lca(x,y);
				ll ret=LD[x]+LD[y]-2*LD[lc];
				cout<<ret<<endl;
				X=(X+ret)%N;
			}
			
			
			
		}
		else if(i==3) {
			cin>>x;
			i=getroot(x);
			x=DM[i].first;
			y=DM[i].second;
			ll lc=lca(x,y);
			ll ret=LD[x]+LD[y]-2*LD[lc];
			cout<<ret<<endl;
		}
		else {
			cin>>x;
			X=(X+x)%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