結果

問題 No.1197 モンスターショー
ユーザー autumn-eelautumn-eel
提出日時 2020-10-10 15:44:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,295 bytes
コンパイル時間 2,092 ms
コンパイル使用メモリ 183,276 KB
実行使用メモリ 58,916 KB
最終ジャッジ日時 2023-09-27 21:29:59
合計ジャッジ時間 20,367 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
27,740 KB
testcase_01 AC 8 ms
27,872 KB
testcase_02 AC 8 ms
27,948 KB
testcase_03 AC 9 ms
27,976 KB
testcase_04 AC 9 ms
27,880 KB
testcase_05 AC 9 ms
27,908 KB
testcase_06 AC 9 ms
27,844 KB
testcase_07 WA -
testcase_08 RE -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 RE -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 10 ms
27,816 KB
testcase_42 AC 9 ms
27,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll>P;

class LCA{
public:
	vector<vector<pair<int,int>>>E;
	vector<vector<int>>par;
	vector<int>d1;
	vector<long long>d2;
	int MAX_LOG=22;
	int n;
	LCA(){}
	LCA(int N){
		n=N;
		E=vector<vector<pair<int,int>>>(n);
		par=vector<vector<int>>(MAX_LOG,vector<int>(n));
		d1=vector<int>(n);
		d2=vector<long long>(n);
	}
	void add_edge(int a,int b,int c){
		E[a].push_back(make_pair(c,b));
		E[b].push_back(make_pair(c,a));
	}
	void add_edge(int a,int b){
		E[a].push_back(make_pair(1,b));
		E[b].push_back(make_pair(1,a));
	}
private:
	void dfs(int u,int p,int a,long long b){
		par[0][u]=p;d1[u]=a;d2[u]=b;
		for(auto&v:E[u]){
			if(v.second!=p)dfs(v.second,u,a+1,b+v.first);
		}
	}
	bool flag=false;
	void init(){
		dfs(0,-1,0,0);
		for(int i=1;i<MAX_LOG;i++)for(int j=0;j<n;j++){
			if(par[i-1][j]==-1)par[i][j]=-1;
			else par[i][j]=par[i-1][par[i-1][j]];
		}
		flag=true;
	}
public:
	int lca(int u,int v){
		if(!flag)init();
		if(d1[u]>d1[v])swap(u,v);
		for(int i=0;i<MAX_LOG;i++){
			if((d1[v]-d1[u])>>i&1)v=par[i][v];
		}
		if(u==v)return u;
		for(int i=MAX_LOG-1;i>=0;i--){
			if(par[i][u]!=par[i][v]){
				u=par[i][u];
				v=par[i][v];
			}
		}
		return par[0][u];
	}
	long long dist(int u,int v){
		int l=lca(u,v);
		return d2[u]+d2[v]-d2[l]*2;
	}
};

LCA lca;
vector<int>E[200000],G[200000];
bool used[200000];

int N;
int centroid;
int sz[200000];

int par[200000];

int id[200000];

ll sum1[200000],cnt1[200000];
vector<ll>sum2[200000],cnt2[200000];

void szdfs(int v,int p){
	sz[v]=1;
	for(int u:E[v]){
		if(u==p||used[u])continue;
		szdfs(u,v);
		sz[v]+=sz[u];
	}
}

void search_centroid(int v,int p){
	bool ok=true;
	for(int u:E[v]){
		if(u==p||used[u])continue;
		search_centroid(u,v);
		if(sz[u]>N/2)ok=false;
	}
	if(N-sz[v]>N/2)ok=false;
	if(ok)centroid=v;
}

void solve(int v,int p){
	szdfs(v,-1);
	N=sz[v];
	search_centroid(v,-1);
	int s=centroid;
	used[s]=true;
	par[s]=p;
	if(p!=-1)G[p].push_back(s);

	for(int u:E[s]){
		if(used[u])continue;
		solve(u,s);
	}
}

void solve2(int s){
	used[s]=true;

	int g=0;
	rep(i,E[s].size()){
		int u=E[s][i];
		if(used[u])continue;
		sum2[s].push_back({});
		cnt2[s].push_back({});
		id[G[s][g]]=g;
		g++;
	}

	for(int u:G[s]){
		solve2(u);
	}
}

void update(int x,int y){
	int X=x;
	int prv=-1;
	while(x!=-1){
		int d=lca.dist(X,x);
		sum1[x]+=d*y;cnt1[x]+=y;
		if(prv!=-1)sum2[x][prv]+=d*y,cnt2[x][prv]+=y;
		prv=id[x];
		x=par[x];
	}
}

ll query(int x){
	ll ans=0;
	int X=x;
	int prv=-1;
	while(x!=-1){
		ll d=lca.dist(X,x);
		ans+=d*cnt1[x];
		ans+=sum1[x];
		if(prv!=-1){
			ans-=sum2[x][prv];
			ans-=d*cnt2[x][prv];
		}
		prv=id[x];
		x=par[x];
	}
	return ans;
}

int c[200000];
int main(){
	int n,K,q;cin>>n>>K>>q;
	
	rep(i,K){
		scanf("%d",&c[i]),c[i]--;
	}
	
	lca=LCA(n);
	rep(i,n-1){
		int a,b;scanf("%d%d",&a,&b);a--;b--;
		E[a].push_back(b);
		E[b].push_back(a);
		lca.add_edge(a,b);
	}
	
	solve(0,-1);
	memset(used,false,sizeof(used));
	solve2(find(par,par+n,-1)-par);
	
	rep(i,K)update(c[i],1);
	
	rep(i,q){
		int ty;scanf("%d",&ty);
		if(ty==1){
			int p,d;scanf("%d%d",&p,&d);p--;d--;
			update(p,-1);
			update(d,1);
		}
		else{
			int d;scanf("%d",&d);d--;
			printf("%lld\n",query(d));
		}
	}
}
0