結果

問題 No.1197 モンスターショー
ユーザー 沙耶花沙耶花
提出日時 2020-08-22 16:12:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,212 bytes
コンパイル時間 3,134 ms
コンパイル使用メモリ 211,444 KB
実行使用メモリ 44,576 KB
最終ジャッジ日時 2024-04-23 17:57:32
合計ジャッジ時間 35,314 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 226 ms
44,576 KB
testcase_08 AC 171 ms
19,752 KB
testcase_09 AC 665 ms
33,624 KB
testcase_10 AC 1,452 ms
32,680 KB
testcase_11 AC 455 ms
12,588 KB
testcase_12 AC 174 ms
8,064 KB
testcase_13 AC 243 ms
22,280 KB
testcase_14 AC 849 ms
22,596 KB
testcase_15 AC 438 ms
11,836 KB
testcase_16 AC 1,386 ms
38,908 KB
testcase_17 AC 1,320 ms
39,412 KB
testcase_18 AC 346 ms
14,472 KB
testcase_19 AC 1,284 ms
32,512 KB
testcase_20 AC 122 ms
12,336 KB
testcase_21 AC 266 ms
5,760 KB
testcase_22 AC 22 ms
5,504 KB
testcase_23 AC 1,156 ms
24,436 KB
testcase_24 AC 703 ms
24,188 KB
testcase_25 AC 264 ms
17,060 KB
testcase_26 AC 582 ms
17,852 KB
testcase_27 AC 852 ms
35,732 KB
testcase_28 AC 732 ms
20,020 KB
testcase_29 AC 392 ms
23,436 KB
testcase_30 AC 255 ms
26,568 KB
testcase_31 AC 376 ms
21,188 KB
testcase_32 AC 218 ms
5,376 KB
testcase_33 AC 193 ms
17,288 KB
testcase_34 AC 2,284 ms
41,364 KB
testcase_35 AC 2,060 ms
41,596 KB
testcase_36 AC 2,058 ms
41,548 KB
testcase_37 AC 2,022 ms
41,580 KB
testcase_38 AC 2,113 ms
41,536 KB
testcase_39 AC 2,096 ms
41,496 KB
testcase_40 TLE -
testcase_41 AC 3 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

template <typename T>
struct sparse_table{
	const T init_value = make_pair(1000000001,1000001);
	
	vector<T> v;
	int n;
	int sz;
	sparse_table(vector<T> &x){
		sz = x.size();
		for(int i=0;true;i++){
			if((1<<i)>sz){
				n = i;
				break;
			}
		}
		v.resize(sz*n,init_value);
		
		for(int i=0;i<sz;i++){
			v[i] = x[i];
		}
		
		for(int i=1;i<n;i++){
			for(int j=0;j<sz+1-(1<<i);j++){
				int temp = j+(1<<(i-1));
				v[i*sz+j] = func(v[(i-1)*sz+j],v[(i-1)*sz+temp]);
			}
		}
	}
	
	T query(int l,int r){
		int x = 31-__builtin_clz(r-l);
		return func(v[sz*x+l],v[sz*x+r-(1<<x)]);
	}
	
	T func(T a,T b){
		return min(a,b);
	}
};

void dfs(vector<vector<int>> &E,int now,int p){
	cnt[now] = C[now];
	for(int i=0;i<E[now].size();i++){
		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++){
		dfs2(E,E[now][i],now);
	}
}

void Erasedfs(vector<vector<int>> &E,int now,int p,int d=0){
	dis[now] =d;
	L[now] = order.size();
	order.emplace_back(d,now);
	vector<int> t;
	for(int i=0;i<E[now].size();i++){
		if(E[now][i]==p)continue;
		t.push_back(E[now][i]);
		Erasedfs(E,E[now][i],now,d+1);
		order.emplace_back(d,now);
	}
	E[now] = t;
	
}

int get_distance(sparse_table<pair<int,int>> &S,int u,int v){
	if(u==v)return 0;
	int l = min(L[u],L[v]);
	int r = max(L[u],L[v]);
	int x = S.query(l,r+1).second;
	return dis[u]+dis[v]-2*dis[x];
}
	

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);
	}
	dis.resize(N);
	L.resize(N);
	Erasedfs(E,0,-1);
	
	
	sparse_table<pair<int,int>> S(order);
	
	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 -= get_distance(S,e,pos[Movement[j].first]);
				ans += get_distance(S,e,Movement[j].second);
			}
			
			printf("%lld\n",ans);
			
		}
	}
	
	return 0;
}
0