結果

問題 No.2296 Union Path Query (Hard)
ユーザー kotatsugamekotatsugame
提出日時 2023-05-05 23:20:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,628 bytes
コンパイル時間 1,129 ms
コンパイル使用メモリ 84,152 KB
実行使用メモリ 222,464 KB
最終ジャッジ日時 2024-11-23 12:16:52
合計ジャッジ時間 236,220 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
123,520 KB
testcase_01 AC 57 ms
55,296 KB
testcase_02 AC 57 ms
146,048 KB
testcase_03 AC 56 ms
144,896 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 1,942 ms
182,656 KB
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 AC 392 ms
79,420 KB
testcase_26 AC 391 ms
74,044 KB
testcase_27 AC 1,178 ms
74,048 KB
testcase_28 AC 1,194 ms
74,048 KB
testcase_29 AC 1,409 ms
78,204 KB
testcase_30 AC 1,376 ms
78,068 KB
testcase_31 AC 1,861 ms
82,780 KB
testcase_32 AC 1,760 ms
80,948 KB
testcase_33 AC 1,501 ms
77,520 KB
testcase_34 AC 1,146 ms
74,120 KB
testcase_35 AC 1,009 ms
71,468 KB
testcase_36 TLE -
testcase_37 AC 1,871 ms
71,816 KB
testcase_38 AC 1,615 ms
72,008 KB
testcase_39 AC 1,610 ms
72,456 KB
testcase_40 AC 1,613 ms
72,452 KB
testcase_41 AC 22 ms
28,032 KB
testcase_42 AC 22 ms
28,032 KB
testcase_43 AC 22 ms
28,032 KB
testcase_44 TLE -
testcase_45 TLE -
testcase_46 TLE -
testcase_47 TLE -
testcase_48 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<set>
#include<cassert>
using namespace std;
int N,X,Q;
vector<pair<int,int> >G[2<<17];
int par[2<<17];
int prD[2<<17];
int pr[18][2<<17],depth[2<<17];
int myrank[2<<17];
long long D[2<<17];
set<pair<long long,int> >mD[2<<17];
vector<int>V[2<<17];
long long ans[2<<17];
long long dfs(int u,int p,int dep,long long dist,int prd,int AP)
{
	par[u]=AP;
	pr[0][u]=p;
	for(int k=1;k<18;k++)
	{
		if(pr[k-1][u]==-1)pr[k][u]=-1;
		else pr[k][u]=pr[k-1][pr[k-1][u]];
	}
	depth[u]=dep;
	D[u]=dist;
	prD[u]=prd;
	V[AP].push_back(u);
	mD[u].clear();
	long long md=0;
	for(pair<int,int>e:G[u])if(e.first!=p)
	{
		long long d=dfs(e.first,u,dep+1,dist+e.second,e.second,AP)+e.second;
		mD[u].insert(make_pair(d,e.first));
		md=max(md,d);
	}
	return md;
}
int lca(int u,int v)
{
	if(depth[u]>depth[v])swap(u,v);
	for(int k=0;k<18;k++)if(depth[v]-depth[u]>>k&1)v=pr[k][v];
	if(u==v)return u;
	for(int k=18;k--;)if(pr[k][u]!=pr[k][v])
	{
		u=pr[k][u];
		v=pr[k][v];
	}
	return pr[0][u];
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>N>>X>>Q;
	for(int i=0;i<N;i++)
	{
		V[i].push_back(i);
		for(int k=0;k<18;k++)pr[k][i]=-1;
		myrank[i]=1;
		par[i]=i;
	}
	for(;Q--;)
	{
		int op;cin>>op;
		if(op==1)
		{
			int v,w;cin>>v>>w;
			int u=X;
			if(myrank[par[u]]>myrank[par[v]])swap(u,v);
			myrank[par[u]]=max(myrank[par[u]],myrank[par[v]]+depth[u]+1);
			ans[par[u]]=max(ans[par[u]],ans[par[v]]);
			long long nd=dfs(v,u,depth[u]+1,D[u]+w,w,par[u])+w;
			if(mD[u].empty())
			{
				ans[par[u]]=max(ans[par[u]],nd);
			}
			else
			{
				auto it=mD[u].end();it--;
				ans[par[u]]=max(ans[par[u]],it->first+nd);
			}
			mD[u].insert(make_pair(nd,v));
			{
				int x=u;
				while(pr[0][x]!=-1)
				{
					int y=pr[0][x];
					auto it=mD[x].end();it--;
					while(!mD[y].empty()&&mD[y].rbegin()->second==x)
					{
						auto jt=mD[y].end();jt--;
						mD[y].erase(jt);
					}
					if(mD[y].empty())
					{
						ans[par[u]]=max(ans[par[u]],it->first+prD[x]);
					}
					else
					{
						auto jt=mD[y].end();
						jt--;
						assert(jt->second!=x);
						ans[par[u]]=max(ans[par[u]],it->first+prD[x]+jt->first);
					}
					mD[y].insert(make_pair(it->first+prD[x],x));
					x=y;
				}
			}
			G[u].push_back(make_pair(v,w));
			G[v].push_back(make_pair(u,w));
		}
		else if(op==2)
		{
			int u,v;cin>>u>>v;
			if(par[u]!=par[v])cout<<"-1\n";
			else
			{
				int w=lca(u,v);
				long long d=D[u]+D[v]-2*D[w];
				cout<<d<<"\n";
				X=(X+d%N)%N;
			}
		}
		else if(op==3)
		{
			int v;cin>>v;
			v=par[v];
			cout<<ans[v]<<"\n";
		}
		else
		{
			int v;cin>>v;
			X=(X+v)%N;
		}
	}
}
0