結果

問題 No.1197 モンスターショー
ユーザー HIR180HIR180
提出日時 2020-08-22 13:55:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,897 bytes
コンパイル時間 5,040 ms
コンパイル使用メモリ 256,376 KB
実行使用メモリ 79,912 KB
最終ジャッジ日時 2024-04-23 17:50:42
合計ジャッジ時間 44,229 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
7,808 KB
testcase_01 AC 9 ms
7,936 KB
testcase_02 AC 9 ms
7,808 KB
testcase_03 AC 10 ms
7,808 KB
testcase_04 AC 9 ms
7,808 KB
testcase_05 AC 9 ms
7,808 KB
testcase_06 AC 9 ms
7,808 KB
testcase_07 AC 322 ms
71,172 KB
testcase_08 AC 233 ms
39,808 KB
testcase_09 AC 773 ms
64,804 KB
testcase_10 AC 1,680 ms
63,440 KB
testcase_11 AC 661 ms
27,124 KB
testcase_12 AC 301 ms
17,668 KB
testcase_13 AC 327 ms
45,028 KB
testcase_14 AC 1,137 ms
46,016 KB
testcase_15 AC 647 ms
25,592 KB
testcase_16 AC 1,650 ms
75,200 KB
testcase_17 AC 1,577 ms
75,904 KB
testcase_18 AC 489 ms
30,864 KB
testcase_19 AC 1,490 ms
63,004 KB
testcase_20 AC 192 ms
26,352 KB
testcase_21 AC 532 ms
13,152 KB
testcase_22 AC 48 ms
12,544 KB
testcase_23 AC 1,427 ms
49,704 KB
testcase_24 AC 863 ms
48,624 KB
testcase_25 AC 360 ms
34,644 KB
testcase_26 AC 799 ms
36,380 KB
testcase_27 AC 1,021 ms
68,560 KB
testcase_28 AC 969 ms
40,516 KB
testcase_29 AC 531 ms
47,360 KB
testcase_30 AC 347 ms
53,376 KB
testcase_31 AC 514 ms
42,836 KB
testcase_32 AC 433 ms
10,796 KB
testcase_33 AC 277 ms
35,192 KB
testcase_34 AC 2,410 ms
79,768 KB
testcase_35 AC 2,350 ms
79,772 KB
testcase_36 AC 2,723 ms
79,908 KB
testcase_37 AC 2,272 ms
79,900 KB
testcase_38 AC 2,393 ms
79,800 KB
testcase_39 AC 2,308 ms
79,912 KB
testcase_40 TLE -
testcase_41 AC 10 ms
7,808 KB
testcase_42 AC 9 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 = 500;
vector<P1>Q;
ll sum[100005];
int sub[100005];
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);
	}
}
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();
	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);
		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