結果

問題 No.1197 モンスターショー
ユーザー 沙耶花沙耶花
提出日時 2020-08-22 15:32:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,353 bytes
コンパイル時間 2,951 ms
コンパイル使用メモリ 218,320 KB
実行使用メモリ 31,872 KB
最終ジャッジ日時 2024-04-23 09:51:51
合計ジャッジ時間 48,168 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
31,872 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1,192 ms
27,392 KB
testcase_08 AC 368 ms
13,952 KB
testcase_09 AC 1,615 ms
21,760 KB
testcase_10 TLE -
testcase_11 AC 1,329 ms
9,600 KB
testcase_12 AC 599 ms
6,656 KB
testcase_13 AC 528 ms
15,232 KB
testcase_14 AC 1,950 ms
15,744 KB
testcase_15 AC 1,221 ms
9,216 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 AC 922 ms
10,624 KB
testcase_19 TLE -
testcase_20 AC 320 ms
9,344 KB
testcase_21 AC 1,074 ms
5,376 KB
testcase_22 AC 78 ms
5,376 KB
testcase_23 AC 2,843 ms
16,768 KB
testcase_24 AC 1,689 ms
16,512 KB
testcase_25 AC 651 ms
12,160 KB
testcase_26 AC 1,454 ms
12,672 KB
testcase_27 AC 2,024 ms
23,040 KB
testcase_28 AC 1,833 ms
14,080 KB
testcase_29 AC 892 ms
16,128 KB
testcase_30 AC 528 ms
18,176 KB
testcase_31 AC 882 ms
14,592 KB
testcase_32 AC 984 ms
5,376 KB
testcase_33 AC 471 ms
12,416 KB
testcase_34 TLE -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 998244353
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000005

struct lca{
	
	vector<int> depth;//depth[i] 頂点iの深さ
	vector<vector<int>> parents;//parents[i][j] 頂点iから2^j個上の親
	
	int max_j = 30;
	
	lca(int n,vector<vector<int>> &E){
		depth.assign(E.size(),-1);
		parents.assign(E.size(),vector<int>(max_j,-1));
		
		stack<int> s;
		s.push(n);
		depth[n] = 0;
		while(s.size()!=0){
			int k = s.top();
			s.pop();
			for(int i=0;i<E[k].size();i++){
				int x = E[k][i];
				if(depth[x]!=-1)continue;
				s.push(x);
				depth[x] = depth[k]+1;
				for(int j=0;j<max_j;j++){
					if(j==0){
						parents[x][j] = k;
					}
					else{
						parents[x][j] = parents[parents[x][j-1]][j-1];
					}
					if(parents[x][j] == -1)break;
				}
			}
		}
	}
	
	//頂点uのk番目の親
	int kth_parent(int u,int k){
		for(int i=0;i<max_j;i++){
			if(k==0)break;
			if(u==-1)break;
			if(k%2){
				u = parents[u][i];
			}
			k/=2;
		}
		return u;
	}
	
	int query(int u,int v){
		if(depth[u]<depth[v])swap(u,v);
		u = kth_parent(u,depth[u]-depth[v]);
		
		if(u==v){
			return u;
		}
		
		for(int i=max_j-1;i>=0;i--){
			if(parents[u][i]!=parents[v][i]){
				u = parents[u][i];
				v = parents[v][i];
			}
		}
		
		return parents[u][0];
		
	}
	
	int get_distance(int u,int v){
		return depth[u]+depth[v]-2*depth[query(u,v)];
	}
	
	
};

int N,K,Q;
vector<int> dis;
vector<long long> calc;
vector<int> cnt;
vector<int> C;

void dfs(vector<vector<int>> &E,int now,int p){
	cnt[now] = C[now];
	for(int i=0;i<E[now].size();i++){
		if(p==E[now][i])continue;
		dfs(E,E[now][i],now);
		cnt[now] += cnt[E[now][i]];
	}
}

void dfs2(vector<vector<int>> &E,int now,int p){
	if(p==-1){
		calc[now] = 0;
		for(int i=0;i<N;i++){
			calc[now] += (long long)dis[i] * C[i];
		}
	}
	else{
		calc[now] = calc[p];
		calc[now] -= cnt[now];
		calc[now] += K-cnt[now];
	}

	for(int i=0;i<E[now].size();i++){
		if(p==E[now][i])continue;
		dfs2(E,E[now][i],now);
	}
}

int main(){
	
	cin>>N>>K>>Q;
	C.resize(N,0);
	vector<int> pos(K);
	for(int i=0;i<K;i++){
		scanf("%d",&pos[i]);
		pos[i]--;
		C[pos[i]]++;
	}
	
	vector<vector<int>> E(N);
	for(int i=0;i<N-1;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		a--;b--;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	
	lca L(0,E);
	
	dis.resize(N);
	for(int i=0;i<N;i++){
		dis[i] = L.get_distance(0,i);
	}
	
	calc.resize(N);
	cnt.resize(N);
	dfs(E,0,-1);
	dfs2(E,0,-1);
	vector<pair<int,int>> Movement;
	for(int i=0;i<Q;i++){
		
		if(Movement.size()>200){
			for(int j=0;j<Movement.size();j++){
				C[pos[Movement[j].first]]--;
				pos[Movement[j].first] = Movement[j].second;
				C[pos[Movement[j].first]]++;
			}
			Movement.resize(0);
			dfs(E,0,-1);
			dfs2(E,0,-1);
		}
		
		int t;
		scanf("%d",&t);
		
		if(t==1){
			int p,d;
			scanf("%d %d",&p,&d);
			p--;d--;
			
			bool f = false;
			for(int j=0;j<Movement.size();j++){
				if(Movement[j].first==p){
					Movement[j].second = d;
					f=true;
					break;
				}
			}
			if(!f)Movement.emplace_back(p,d);
			
		}
		else{
			int e;
			scanf("%d",&e);
			e--;
			long long ans = calc[e];
			for(int j=0;j<Movement.size();j++){
				ans -= L.get_distance(e,pos[Movement[j].first]);
				ans += L.get_distance(e,Movement[j].second);
			}
			
			printf("%lld\n",ans);
			
		}
	}
	
	return 0;
}
0