結果

問題 No.899 γatheree
ユーザー 👑 kmjpkmjp
提出日時 2019-10-05 01:01:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 488 ms / 2,000 ms
コード長 2,019 bytes
コンパイル時間 1,829 ms
コンパイル使用メモリ 174,240 KB
実行使用メモリ 23,404 KB
最終ジャッジ日時 2024-04-14 22:27:56
合計ジャッジ時間 11,582 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
13,652 KB
testcase_01 AC 6 ms
11,732 KB
testcase_02 AC 5 ms
11,860 KB
testcase_03 AC 5 ms
11,856 KB
testcase_04 AC 6 ms
13,776 KB
testcase_05 AC 6 ms
13,912 KB
testcase_06 AC 472 ms
23,248 KB
testcase_07 AC 451 ms
23,380 KB
testcase_08 AC 441 ms
23,312 KB
testcase_09 AC 456 ms
23,316 KB
testcase_10 AC 474 ms
23,252 KB
testcase_11 AC 450 ms
23,256 KB
testcase_12 AC 464 ms
23,252 KB
testcase_13 AC 452 ms
22,996 KB
testcase_14 AC 457 ms
23,000 KB
testcase_15 AC 462 ms
23,312 KB
testcase_16 AC 487 ms
23,252 KB
testcase_17 AC 488 ms
22,996 KB
testcase_18 AC 454 ms
23,316 KB
testcase_19 AC 454 ms
23,256 KB
testcase_20 AC 447 ms
23,060 KB
testcase_21 AC 277 ms
23,312 KB
testcase_22 AC 273 ms
23,404 KB
testcase_23 AC 274 ms
23,184 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))
//-------------------------------------------------------

int N,Q;
vector<int> E[101010];
int D[101010],P[101010];
int CL[101010],CR[101010],GL[101010],GR[101010];
int DN[101010],id[101010];
set<pair<int,ll>> S[101010];

void dfs(int cur,int pre,int d) {
	D[cur]=d;
	P[cur]=pre;
	id[cur]=++DN[D[cur]];
	CL[cur]=GL[cur]=1<<20;
	CR[cur]=GR[cur]=-1;
	FORR(e,E[cur]) if(e!=pre) {
		dfs(e,cur,d+1);
		CL[cur]=min(CL[cur],id[e]);
		CR[cur]=max(CR[cur],id[e]);
		GL[cur]=min(GL[cur],CL[e]);
		GR[cur]=max(GR[cur],CR[e]);
	}
	
}

ll poke(int e) {
	ll tot=0;
	auto it=S[D[e]].lower_bound(make_pair(id[e],0));
	if(it!=S[D[e]].end() && it->first==id[e]) {
		tot=it->second;
		S[D[e]].erase(it);
	}
	return tot;
}

ll poke_range(int d,int L,int R) {
	ll tot=0;
	while(1) {
		auto it=S[d].lower_bound({L,0});
		if(it==S[d].end() || it->first>R) break;
		tot+=it->second;
		S[d].erase(it);
	}
	return tot;
}


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N-1) {
		cin>>x>>y;
		E[x].push_back(y);
		E[y].push_back(x);
	}
	dfs(0,0,0);
	FOR(i,N) {
		cin>>x;
		S[D[i]].insert({id[i],x});
	}
	cin>>Q;
	while(Q--) {
		cin>>x;
		ll tot=poke(x);
		
		if(D[x]>=2) {
			tot+=poke(P[P[x]]);
		}
		if(D[x]>=1) {
			tot+=poke(P[x]);
			tot+=poke_range(D[x],CL[P[x]],CR[P[x]]);
		}
		tot+=poke_range(D[x]+1,CL[x],CR[x]);
		tot+=poke_range(D[x]+2,GL[x],GR[x]);
		S[D[x]].insert({id[x],tot});
		cout<<tot<<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