結果

問題 No.2296 Union Path Query (Hard)
ユーザー 👑 kmjpkmjp
提出日時 2023-05-13 09:52:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,711 bytes
コンパイル時間 4,131 ms
コンパイル使用メモリ 207,352 KB
実行使用メモリ 30,748 KB
最終ジャッジ日時 2023-08-19 11:36:49
合計ジャッジ時間 16,704 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,088 KB
testcase_01 AC 12 ms
27,688 KB
testcase_02 AC 12 ms
27,756 KB
testcase_03 AC 12 ms
30,748 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

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,X,Q;
vector<pair<int,int>> E[200005];
int P[21][200005],D[200005];
ll LD[202020];
int root[202020];
int md[202020];
int V[202020];
pair<int,int> DM[202020];
ll DS[202020];

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

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) {
	int i=x;
	while(root[i]!=i) i=root[i];
	return root[x]=i;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>X>>Q;
	FOR(i,N) {
		root[i]=i;
		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,getroot(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