結果

問題 No.1197 モンスターショー
ユーザー HIR180HIR180
提出日時 2020-08-22 14:51:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,342 ms / 3,000 ms
コード長 5,428 bytes
コンパイル時間 5,724 ms
コンパイル使用メモリ 259,392 KB
実行使用メモリ 85,988 KB
最終ジャッジ日時 2024-10-15 18:29:38
合計ジャッジ時間 29,732 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
7,680 KB
testcase_01 AC 11 ms
7,680 KB
testcase_02 AC 11 ms
7,680 KB
testcase_03 AC 11 ms
7,680 KB
testcase_04 AC 11 ms
7,680 KB
testcase_05 AC 11 ms
7,680 KB
testcase_06 AC 10 ms
7,680 KB
testcase_07 AC 267 ms
69,512 KB
testcase_08 AC 190 ms
40,192 KB
testcase_09 AC 496 ms
65,768 KB
testcase_10 AC 992 ms
64,352 KB
testcase_11 AC 590 ms
27,440 KB
testcase_12 AC 325 ms
17,708 KB
testcase_13 AC 238 ms
45,560 KB
testcase_14 AC 746 ms
46,644 KB
testcase_15 AC 586 ms
25,888 KB
testcase_16 AC 906 ms
76,312 KB
testcase_17 AC 933 ms
77,020 KB
testcase_18 AC 414 ms
31,120 KB
testcase_19 AC 879 ms
64,020 KB
testcase_20 AC 177 ms
26,496 KB
testcase_21 AC 619 ms
13,236 KB
testcase_22 AC 56 ms
12,544 KB
testcase_23 AC 923 ms
50,384 KB
testcase_24 AC 554 ms
49,292 KB
testcase_25 AC 299 ms
34,964 KB
testcase_26 AC 620 ms
36,724 KB
testcase_27 AC 634 ms
69,560 KB
testcase_28 AC 679 ms
41,180 KB
testcase_29 AC 369 ms
47,680 KB
testcase_30 AC 261 ms
54,064 KB
testcase_31 AC 363 ms
43,424 KB
testcase_32 AC 534 ms
10,972 KB
testcase_33 AC 233 ms
35,528 KB
testcase_34 AC 1,306 ms
80,948 KB
testcase_35 AC 1,256 ms
80,944 KB
testcase_36 AC 1,293 ms
80,944 KB
testcase_37 AC 1,242 ms
80,820 KB
testcase_38 AC 1,342 ms
80,948 KB
testcase_39 AC 1,299 ms
80,960 KB
testcase_40 AC 1,045 ms
85,988 KB
testcase_41 AC 10 ms
7,680 KB
testcase_42 AC 11 ms
7,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
template<class T>
void dmp(T a){
	rep(i,a.size()) cout << a[i] << " ";
	cout << endl;
}
template<class T>
bool chmax(T&a, T b){
	if(a < b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a, T b){
	if(a > b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}

#define SZ 100005
vector<int>edge[SZ];
P ar[SZ*2]={};
int pos[SZ]={},id=0,up[SZ],dep[SZ];
P mn[2][20][SZ*2]={};
int sz[SZ*2]={},in[SZ],out[SZ],rev[SZ*2];

struct adj_finder{
	//SZは元の木の頂点数より大
	//外部でedge[]に隣接状況を持って置く必要あり
	void dfs(int v,int u,int d){
		pos[v] = in[v] = id;
		up[v] = u;
		dep[v] = d;
		rev[id] = v;
		ar[id++] = mp(d,id);
		for(int i=0;i<edge[v].size();i++){
			if(edge[v][i] == u) continue;
			dfs(edge[v][i],v,d+1);
			rev[id] = v;
			ar[id++] = mp(d,id);
		}
		out[v] = id-1;
	}
	void prepare(){
		dfs(1,-1,0);
		for(int i=0;i<id;i++){
			mn[0][0][i] = ar[i];
			mn[1][0][i] = mp(INF,INF);
		}
		for(int j=0;j<19;j++){
			for(int i=0;i<id;i++){
				if(i+(1<<j) >= id) rep(x,2) mn[x][j+1][i] = mn[x][j][i];
				else{
					mn[0][j+1][i] = min(mn[0][j][i], mn[0][j][i+(1<<j)]);
					mn[1][j+1][i] = mp(INF,INF);
					int best_v = rev[mn[0][j+1][i].second];
					rep(x,2){
						if(mn[x][j][i].second != INF && rev[mn[x][j][i].second] != best_v) mn[1][j+1][i] = min(mn[1][j+1][i], mn[x][j][i]);
						if(mn[x][j][i+(1<<j)].second != INF && rev[mn[x][j][i+(1<<j)].second] != best_v) mn[1][j+1][i] = min(mn[1][j+1][i], mn[x][j][i+(1<<j)]);
					}
				}
			}
		}
		for(int i=1;i<SZ*2;i++){
			for(int j=0;j<20;j++){
				if((1<<j) <= i && i <= (2<<j)){
					sz[i] = j;
					break;
				}
			}
		}
	}
	int get1(int a,int b){
		int len = b-a+1;
		int ty = sz[len];
		P p = min(mn[0][ty][a],mn[0][ty][b-(1<<ty)+1]);
		return p.second;
	}
	int get_dist(int a, int b){
	    int c = rev[get1(min(pos[a],pos[b]), max(pos[a],pos[b]))];
	    return dep[a]+dep[b]-2*dep[c];
	}
	int get2(int a,int b){
		int len = b-a+1;
		int ty = sz[len];
		P p = mp(INF,INF);
		int x = rev[get1(a,b)];
		rep(i,2){
			if(mn[i][ty][a].second != INF && rev[mn[i][ty][a].second] != x) p = min(p, mn[i][ty][a]);
			if(mn[i][ty][b-(1<<ty)+1].second != INF && rev[mn[i][ty][b-(1<<ty)+1].second] != x) p = min(p, mn[i][ty][b-(1<<ty)+1]);
		}
		return rev[p.second];
	}
	int near_v(int u,int v){
		if(in[u] <= in[v] && out[v] <= out[u]){
			return get2(pos[v], out[u]);
		}
		else return up[u];
	}
}kaede;

int n, k, q, c[100005];
int cnt[100005];
constexpr int B = 614;
vector<P1>Q;
ll sum[100005];
int sub[100005];
int upp[100005], arr[100005], idd, D[100005];
void make(int v, int u){
	arr[idd++] = v;
	upp[v] = u;
	if(u == -1) D[v] = 0; else D[v] = D[u]+1;
	rep(i, edge[v].size()){
		if(edge[v][i] == u) continue;
		make(edge[v][i], v);
	}
}
/*void dfs(int v, int u, int d){
	sum[1] += 1LL * d * cnt[v];
	rep(i, edge[v].size()){
		if(edge[v][i] == u) continue;
		dfs(edge[v][i], v, d+1);
		sub[v] += sub[edge[v][i]];
	}
	sub[v] += cnt[v];
}
void dfs2(int v, int u){
	rep(i, edge[v].size()){
		if(edge[v][i] == u) continue;
		sum[edge[v][i]] = sum[v] + k - 2*sub[edge[v][i]];
		dfs2(edge[v][i], v);
	}
}*/
void maketree(){
	rep(i, n){
		int v = arr[i];
		sum[1] += 1LL*D[v]*cnt[v];
		sub[v] += cnt[v];
		if(v != 1) sub[upp[v]] += sub[v];
	}
	for(int i=n-1;i>=0;i--){
		int v = arr[i];
		if(v != 1){
			sum[v] = sum[upp[v]] + k - 2 * sub[v];
		}
	}
}
int main(){
	scanf("%d%d%d", &n, &k, &q);
	repn(i, k){
		scanf("%d", &c[i]);
	}
	rep(i, n-1){
		int a, b; scanf("%d%d", &a, &b);
		edge[a].pb(b);
		edge[b].pb(a);
	}
	kaede.prepare();
	make(1, -1);
	reverse(arr, arr+n);
	rep(i, q){
		int ty; scanf("%d", &ty);
		int a, b = -1;
		if(ty == 1) scanf("%d%d", &a, &b);
		else scanf("%d", &a);
		Q.pb(mp(ty, mp(a, b)));
	}
	for(int i=0;i<q;i+=B){
		memset(cnt, 0, sizeof(cnt));
		memset(sub, 0, sizeof(sub));
		repn(j, k) cnt[c[j]] ++;
		sum[1] = 0;
		//dfs(1, -1, 0);
		//dfs2(1, -1);
		maketree();
		vector<int>vec;
		for(int j=i;j<i+B && j<q;j++){
			if(Q[j].fi == 1) vec.pb(Q[j].sc.fi);
		}
		SORT(vec); ERASE(vec);
		vector<int>gen;
		rep(i, vec.size()) gen.pb(c[vec[i]]);
		for(int j=i;j<i+B && j<q;j++){
			if(Q[j].fi == 1) {
				c[Q[j].sc.fi] = Q[j].sc.sc;
			}
			else{
				ll ans = sum[Q[j].sc.fi];
				rep(k, vec.size()){
					ans -= kaede.get_dist(Q[j].sc.fi, gen[k]);
					ans += kaede.get_dist(Q[j].sc.fi, c[vec[k]]);
				}
				printf("%lld\n", ans);
			}
		}
	}
}
0