結果

問題 No.1197 モンスターショー
ユーザー 👑 kmjpkmjp
提出日時 2020-08-27 00:40:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 620 ms / 3,000 ms
コード長 3,804 bytes
コンパイル時間 2,807 ms
コンパイル使用メモリ 211,628 KB
実行使用メモリ 178,640 KB
最終ジャッジ日時 2024-04-25 04:22:58
合計ジャッジ時間 19,452 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
134,528 KB
testcase_01 AC 90 ms
134,528 KB
testcase_02 AC 87 ms
134,784 KB
testcase_03 AC 88 ms
134,528 KB
testcase_04 AC 87 ms
134,656 KB
testcase_05 AC 86 ms
134,656 KB
testcase_06 AC 87 ms
134,656 KB
testcase_07 AC 188 ms
173,176 KB
testcase_08 AC 270 ms
143,872 KB
testcase_09 AC 285 ms
150,912 KB
testcase_10 AC 405 ms
150,400 KB
testcase_11 AC 338 ms
139,708 KB
testcase_12 AC 304 ms
137,156 KB
testcase_13 AC 178 ms
145,152 KB
testcase_14 AC 424 ms
145,536 KB
testcase_15 AC 361 ms
139,520 KB
testcase_16 AC 418 ms
153,856 KB
testcase_17 AC 464 ms
154,112 KB
testcase_18 AC 251 ms
140,972 KB
testcase_19 AC 390 ms
150,144 KB
testcase_20 AC 160 ms
139,776 KB
testcase_21 AC 407 ms
135,856 KB
testcase_22 AC 119 ms
135,856 KB
testcase_23 AC 456 ms
146,432 KB
testcase_24 AC 358 ms
146,432 KB
testcase_25 AC 260 ms
142,208 KB
testcase_26 AC 426 ms
142,592 KB
testcase_27 AC 430 ms
152,064 KB
testcase_28 AC 369 ms
143,812 KB
testcase_29 AC 291 ms
145,920 KB
testcase_30 AC 245 ms
147,712 KB
testcase_31 AC 231 ms
144,384 KB
testcase_32 AC 308 ms
135,168 KB
testcase_33 AC 167 ms
142,336 KB
testcase_34 AC 620 ms
155,264 KB
testcase_35 AC 615 ms
155,392 KB
testcase_36 AC 607 ms
155,264 KB
testcase_37 AC 589 ms
155,392 KB
testcase_38 AC 600 ms
155,332 KB
testcase_39 AC 593 ms
155,084 KB
testcase_40 AC 392 ms
178,640 KB
testcase_41 AC 87 ms
134,656 KB
testcase_42 AC 87 ms
134,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

template<class V, int ME> class BIT_r {
public:
	V bit[2][1<<ME];
	BIT_r(){clear();};
	void clear() {ZERO(bit);};
	
	void update(int entry, V val0, V val1) {
		entry++;
		while(entry <= 1<<ME) bit[0][entry-1]+=val0, bit[1][entry-1] += val1, entry += entry & -entry;
	}
	V total(int entry) {
		if(entry<0) return 0;
		int e=entry++;
		V v0=0,v1=0;
		while(entry>0) v0+=bit[0][entry-1], v1+=bit[1][entry-1], entry -= entry & -entry;
		return e*v0+v1;
	}
	void add(int L, int R, V val) { // add val to L<=x<=R
		update(L,val,-val*(L-1));
		update(R+1,-val,val*R);
	}
};
BIT_r<ll,23> bt;

struct HLdecomp {
	static const int MD=20;
	int N,NE,id;
	vector<vector<int>> E;
	vector<int> D,S,B,C; // depth, size, base,heavy child
	
	vector<int> L,R,rev; // EulerTour
	vector<vector<int>> P,Cs; // parent for LCA,children
	void init(int N) { this->N=N, NE=0, E.clear(),E.resize(N); Cs.clear(),Cs.resize(N);
		D=S=B=C=L=R=rev=vector<int>(N,0); id=0; int i; P.clear(); FOR(i,MD+1) P.push_back(vector<int>(N,0));}
	void add_edge(int a,int b){ E[a].push_back(b),E[b].push_back(a); NE++;} // undir
	void dfs(int cur,int pre) { // get depth, parent, size, largest subtree
		int i;
		P[0][cur]=pre;S[cur]=1;C[cur]=-1;B[cur]=cur;
		D[cur]=(pre==cur)?0:(D[pre]+1);
		FOR(i,E[cur].size()) if(E[cur][i]!=pre) {
			int r=E[cur][i]; dfs(r,cur); S[cur]+=S[r];
			if(C[cur]==-1 || S[r]>S[C[cur]]) C[cur]=r;
		}
	}
	void dfs2(int cur,int pre) { // set base and list
		if(pre!=cur && C[pre]==cur) B[cur]=B[pre];
		else B[cur]=cur;
		Cs[B[cur]].push_back(cur);
		L[cur]=id++;
		rev[L[cur]]=cur;
		// DFS順を先行
		if(C[cur]!=-1) dfs2(C[cur],cur);
		FORR(r,E[cur]) if(r!=pre && r!=C[cur]) dfs2(r,cur);
		R[cur]=id;
	}
	pair<int,int> lca(int a,int b) {
		int ret=0,i,aa=a,bb=b;
		if(D[aa]>D[bb]) swap(aa,bb);
		for(i=19;i>=0;i--) if(D[bb]-D[aa]>=1<<i) bb=P[i][bb];
		for(i=19;i>=0;i--) if(P[i][aa]!=P[i][bb]) aa=P[i][aa], bb=P[i][bb];
		return make_pair((aa==bb)?aa:P[0][aa], D[a]+D[b]-2*D[(aa==bb)?aa:P[0][aa]]);
	}
	void decomp(int root=0){
		assert(NE==N-1);
		dfs(root,root); dfs2(root,root);
		int i,x; FOR(i,MD) FOR(x,N) P[i+1][x]=P[i][P[i][x]];
	}
};

HLdecomp hl;

void add(int f,int t,int v) {
	while(hl.B[f]!=hl.B[t]) {
		int nex=hl.P[0][hl.B[f]];
		int dif=hl.D[f]-hl.D[nex];
		bt.add(hl.L[f]-dif+1,hl.L[f],v);
		f=nex;
	}
	bt.add(hl.L[t],hl.L[f],v);
}
ll get(int f,int t) { // fはtの子孫
	ll ret = 0;
	while(hl.B[f]!=hl.B[t]) {
		int nex=hl.P[0][hl.B[f]];
		int dif=hl.D[f]-hl.D[nex];
		ret += bt.total(hl.L[f])-bt.total(hl.L[f]-dif);
		f=nex;
	}
	ret += bt.total(hl.L[f])-bt.total(hl.B[f]);
	return ret;
}

int N,K,Q;
int C[303030];
ll Dsum;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K>>Q;
	FOR(i,K) {
		cin>>C[i];
		C[i]--;
	}
	hl.init(N);
	FOR(i,N-1) {
		cin>>x>>y;
		hl.add_edge(x-1,y-1);
	}
	hl.decomp();
	FOR(i,K) {
		Dsum+=hl.D[C[i]];
		add(C[i],0,1);
	}
	
	
	while(Q--) {
		cin>>i;
		if(i==1) {
			cin>>x>>y;
			x--,y--;
			Dsum-=hl.D[C[x]];
			add(C[x],0,-1);
			C[x]=y;
			Dsum+=hl.D[C[x]];
			add(C[x],0,1);
		}
		else {
			cin>>x;
			x--;
			ll tmp=Dsum+1LL*K*hl.D[x]-2*get(x,0);
			cout<<tmp<<endl;
			
		}
	}
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0