結果

問題 No.899 γatheree
ユーザー chocoruskchocorusk
提出日時 2019-10-05 00:16:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 838 ms / 2,000 ms
コード長 4,069 bytes
コンパイル時間 2,022 ms
コンパイル使用メモリ 137,384 KB
実行使用メモリ 16,516 KB
最終ジャッジ日時 2024-04-14 12:21:38
合計ジャッジ時間 18,566 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 825 ms
16,256 KB
testcase_07 AC 814 ms
16,260 KB
testcase_08 AC 806 ms
16,256 KB
testcase_09 AC 807 ms
16,260 KB
testcase_10 AC 816 ms
16,128 KB
testcase_11 AC 820 ms
16,256 KB
testcase_12 AC 838 ms
16,264 KB
testcase_13 AC 822 ms
16,384 KB
testcase_14 AC 811 ms
16,124 KB
testcase_15 AC 823 ms
16,128 KB
testcase_16 AC 819 ms
16,260 KB
testcase_17 AC 819 ms
16,128 KB
testcase_18 AC 823 ms
16,128 KB
testcase_19 AC 816 ms
16,132 KB
testcase_20 AC 821 ms
16,132 KB
testcase_21 AC 801 ms
16,512 KB
testcase_22 AC 808 ms
16,384 KB
testcase_23 AC 797 ms
16,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<typename Monoid, typename OperatorMonoid=Monoid>
struct LazySegmentTree{
	using F=function<Monoid(Monoid, Monoid)>;
	using G=function<Monoid(Monoid, OperatorMonoid, int)>;
	using H=function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
	int sz;
	vector<Monoid> data;
	vector<OperatorMonoid> lazy;
	const F f;
	const G g;
	const H h;
	const Monoid e1;
	const OperatorMonoid e0;

	LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &e1, const OperatorMonoid &e0): f(f), g(g), h(h), e1(e1), e0(e0){
		sz=1;
		while(sz<n) sz<<=1;
		data.resize(2*sz-1, e1);
		lazy.resize(2*sz-1, e0);
	}

	void build(vector<Monoid> v){
		for(int i=0; i<v.size(); i++) data[i+sz-1]=v[i];
		for(int i=sz-2; i>=0; i--) data[i]=f(data[2*i+1], data[2*i+2]);
	}

	void eval(int k, int l, int r){
		if(lazy[k]!=e0){
			data[k]=g(data[k], lazy[k], r-l);
			if(k<sz-1){
				lazy[2*k+1]=h(lazy[2*k+1], lazy[k]);
				lazy[2*k+2]=h(lazy[2*k+2], lazy[k]);
			}
		}
		lazy[k]=e0;
	}

	void update(int a, int b, const OperatorMonoid &x, int k, int l, int r){
		eval(k, l, r);
		if(r<=a || b<=l) return;
		if(a<=l && r<=b){
			lazy[k]=h(lazy[k], x);
			eval(k, l, r);
		}else{
			update(a, b, x, 2*k+1, l, (l+r)/2);
			update(a, b, x, 2*k+2, (l+r)/2, r);
			data[k]=f(data[2*k+1], data[2*k+2]);
		}
	}
	void update(int a, int b, const OperatorMonoid &x){
		return update(a, b, x, 0, 0, sz);
	}

	Monoid find(int a, int b, int k, int l, int r){
		eval(k, l, r);
		if(b<=l || r<=a) return e1;
		if(a<=l && r<=b) return data[k];
		else return f(find(a, b, 2*k+1, l, (l+r)/2), find(a, b, 2*k+2, (l+r)/2, r));
	}
	Monoid find(int a, int b){
		return find(a, b, 0, 0, sz);
	}
	Monoid operator[](const int &k){
		return find(k, k+1);
	}
};
vector<vector<int>> g;
ll a[100010];
int ind[100010];
int par[100010];
int main()
{
	int n; cin>>n;
	g.resize(n);
	for(int i=0; i<n-1; i++){
		int u, v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=0; i<n; i++) cin>>a[i];
	queue<int> que;
	que.push(0);
	par[0]=-1;
	int c=0;
	while(!que.empty()){
		int x=que.front();
		que.pop();
		ind[x]=c;
		c++;
		for(auto y:g[x]){
			if(y!=par[x]){
				que.push(y);
				par[y]=x;
			}
		}
	}
	for(int i=0; i<n; i++){
		for(auto &y:g[i]){
			if(y==par[i]){
				swap(y, g[i].back());
				g[i].pop_back();
				break;
			}
		}
		sort(g[i].begin(), g[i].end(), [&](int y, int z){ return ind[y]<ind[z];});
	}
	vector<ll> va(n);
	for(int i=0; i<n; i++) va[ind[i]]=a[i];
	auto f=[](ll a, ll b){ return a+b;};
	auto g1=[](ll a, ll x, int len){ return x*len;};
	auto h=[](ll x, ll y){ return y;};
	const ll INF=1e18;
	LazySegmentTree<ll> seg(n, f, g1, h, 0, INF);
	seg.build(va);
	int l2[100010], r2[100010];
	for(int i=0; i<n; i++){
		l2[i]=r2[i]=-1;
		for(auto y:g[i]){
			if(!g[y].empty()){
				l2[i]=g[y][0];
				break;
			}
		}
		for(auto y:g[i]){
			if(!g[y].empty()){
				r2[i]=g[y].back();
			}
		}
	}
	int Q; cin>>Q;
	for(int i=0; i<Q; i++){
		int x; cin>>x;
		ll s=seg[ind[x]];
		seg.update(ind[x], ind[x]+1, 0);
		if(par[x]!=-1){
			int p=par[x];
			s+=seg[ind[p]];
			seg.update(ind[p], ind[p]+1, 0);
			if(par[p]!=-1){
				s+=seg[ind[par[p]]];
				seg.update(ind[par[p]], ind[par[p]]+1, 0);
			}
			int l=ind[g[p][0]], r=ind[g[p].back()];
			s+=seg.find(l, r+1);
			seg.update(l, r+1, 0);
		}
		if(!g[x].empty()){
			int l=ind[g[x][0]], r=ind[g[x].back()];
			s+=seg.find(l, r+1);
			seg.update(l, r+1, 0);
			if(l2[x]!=-1){
				s+=seg.find(ind[l2[x]], ind[r2[x]]+1);
				seg.update(ind[l2[x]], ind[r2[x]]+1, 0);
			}
		}
		cout<<s<<endl;
		seg.update(ind[x], ind[x]+1, s);
	}
	return 0;
}
0