結果

問題 No.1524 Upward Mobility
ユーザー 沙耶花沙耶花
提出日時 2021-05-13 23:36:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,208 ms / 6,000 ms
コード長 6,131 bytes
コンパイル時間 5,574 ms
コンパイル使用メモリ 286,892 KB
実行使用メモリ 499,988 KB
最終ジャッジ日時 2024-04-17 07:19:20
合計ジャッジ時間 37,948 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,788 ms
279,372 KB
testcase_01 AC 1,772 ms
282,244 KB
testcase_02 AC 2,208 ms
449,284 KB
testcase_03 AC 606 ms
198,628 KB
testcase_04 AC 1,540 ms
338,804 KB
testcase_05 AC 1,537 ms
339,452 KB
testcase_06 AC 1,547 ms
339,036 KB
testcase_07 AC 1,514 ms
337,732 KB
testcase_08 AC 1,512 ms
335,836 KB
testcase_09 AC 1,542 ms
336,856 KB
testcase_10 AC 1,590 ms
338,860 KB
testcase_11 AC 181 ms
61,444 KB
testcase_12 AC 187 ms
61,704 KB
testcase_13 AC 231 ms
61,316 KB
testcase_14 AC 588 ms
199,344 KB
testcase_15 AC 584 ms
198,128 KB
testcase_16 AC 1,554 ms
335,112 KB
testcase_17 AC 1,321 ms
276,416 KB
testcase_18 AC 260 ms
60,092 KB
testcase_19 AC 1,186 ms
251,260 KB
testcase_20 AC 376 ms
77,008 KB
testcase_21 AC 444 ms
91,096 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 818 ms
337,184 KB
testcase_30 AC 816 ms
337,188 KB
testcase_31 AC 958 ms
337,264 KB
testcase_32 AC 2,086 ms
499,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)()>
struct dynamic_lazy_segtree {
	struct node{
		int nxt[2];
		S v;
		F lz;
		node(){
			nxt[0] = -1;
			nxt[1] = -1;
			v = e();
			lz = id();
		}
	};
	
	vector<node> nodes;
	
	dynamic_lazy_segtree() : dynamic_lazy_segtree(0) {}
	dynamic_lazy_segtree(long long n){
		log = 0;
		while((1LL<<log) < n){
			log++;
		}
		_n = n;
		nodes.resize(1,node());
	}
	
	int get(int cur,int ind){
		if(nodes[cur].nxt[ind]==-1){
			nodes[cur].nxt[ind] = nodes.size();
			nodes.push_back(node());
		}
		
		return nodes[cur].nxt[ind];
	}
	
	void set(long long p, S x) {
        assert(0 <= p && p < _n);
		vector<int> t(1,0);
        for(int i=log-1;i>=0;i--){
			t.push_back(get(t.back(),(p>>i)&1));
		}
		
		rep(i,t.size()-1){
			push(t[i]);
		}
		reverse(t.begin(),t.end());
		rep(i,t.size()){
			if(i==0)nodes[t[i]].v = x;
			else{
				update(t[i]);
			}
		}
    }
	
	S get(long long p) {
        assert(0 <= p && p < _n);
		int cur = 0;
        for(int i=log-1;i>=0;i--){
			push(cur);
			cur = get(cur,(p>>i)&1);
		}
		return nodes[cur].v;
    }
	
	S prod(long long l,long long r,int cur,int depth){
		if(cur==-1||r-l==0)return e();
		if(r-l==(1LL<<depth))return nodes[cur].v;
		push(cur);
		r--;
		if((l>>(depth-1))&1){
			l ^= 1LL<<(depth-1);
			r ^= 1LL<<(depth-1);
			return prod(l,r+1,nodes[cur].nxt[1],depth-1);
		}
		if(((r>>(depth-1))&1)==0){
			return prod(l,r+1,nodes[cur].nxt[0],depth-1);
		}

		r ^= 1LL<<(depth-1);
		return op(prod(l,1LL<<(depth-1),nodes[cur].nxt[0],depth-1),prod(0,r+1,nodes[cur].nxt[1],depth-1));
	}
	
	S prod(long long l, long long r) {
        assert(0 <= l && l <= r && r <= _n);
        return prod(l,r,0,log);
    }

	S all_prod() { return nodes[0].v; }
	
	void apply(long long p,F f){
		assert(0 <= p && p < _n);
		vector<int> t(1,0);
        for(int i=log-1;i>=0;i--){
			t.push_back(get(t.back(),(p>>i)&1));
		}
		
		rep(i,t.size()-1){
			push(t[i]);
		}
		reverse(t.begin(),t.end());
		rep(i,t.size()){
			if(i==0)nodes[t[i]].v = mapping(f,nodes[t[i]].v);
			else{
				update(t[i]);
			}
		}		
	}
	
	void apply(long long l,long long r,int cur,int depth,F f){
		if(r-l==0)return;
		if(r-l==(1LL<<depth)){
			all_apply(cur,f);
			return;
		}
		push(cur);
		r--;
		if((l>>(depth-1))&1){
			l ^= 1LL<<(depth-1);
			r ^= 1LL<<(depth-1);
			apply(l,r+1,get(cur,1),depth-1,f);
		}
		else if(((r>>(depth-1))&1)==0){
			apply(l,r+1,get(cur,0),depth-1,f);
		}
		else{
			r ^= 1LL<<(depth-1);
			apply(l,1LL<<(depth-1),get(cur,0),depth-1,f);
			apply(0,r+1,get(cur,1),depth-1,f);
		}
		
		update(cur);
		return;
	}
	
	void apply(long long l,long long r,F f){
		assert(0 <= l && l <= r && r <= _n);
        apply(l,r,0,log,f);
	}
	

	void update(int k){
		nodes[k].v = e();
		rep(i,2){
			if(nodes[k].nxt[i]==-1)continue;
			nodes[k].v = op(nodes[k].v,nodes[nodes[k].nxt[i]].v);
		}
	}
	
	void all_apply(int k, F f) {
        nodes[k].v = mapping(f, nodes[k].v);
        nodes[k].lz = composition(f, nodes[k].lz);
    }
    void push(int k) {
		rep(i,2){
			int t = get(k,i);
			all_apply(t,nodes[k].lz);
		}
		nodes[k].lz = id();
    }
	
	long long _n,log;
	
};

int N;
vector<vector<int>> E;
vector<int> a;
vector<long long> b;

vector<int> sz;

long long op(long long a,long long b){
	return max(a,b);
}

long long e(){
	return 0;
}

long long mapping(long long a,long long b){
	return a+b;
}

long long composition(long long a,long long b){
	return a+b;
}

long long id(){
	return 0;
}

using Segtree = dynamic_lazy_segtree<long long,op,e,long long,mapping,composition,id>;

vector<set<int>> ps;
vector<Segtree> seg;

void dfs0(int cur){
	sz[cur] = 1;
	rep(i,E[cur].size()){
		int to = E[cur][i];
		dfs0(to);
		sz[cur] += sz[to];
	}
	
	sort(E[cur].rbegin(),E[cur].rend(),[&](int a,int b){
		return sz[a]<sz[b];
	});
	
}


void dfs2(int cur){
	if(E[cur].size()==0){
		seg[cur].set(a[cur],b[cur]);
		seg[cur].set(N,0);
		ps[cur].insert(a[cur]);
		ps[cur].insert(N);
		return;
	}
	
	if(E[cur].size()==1){
		int to = E[cur][0];
		dfs2(to);
		swap(ps[cur],ps[to]);
		swap(seg[cur],seg[to]);

		seg[cur].set(a[cur],seg[cur].prod(a[cur]+1,N+1)+b[cur]);
		ps[cur].insert(a[cur]);
		return;
	}
	
	rep(i,E[cur].size()){
		int to = E[cur][i];
		dfs2(to);
	}
	vector<int> pp;
	
	rep(i,E[cur].size()){
		if(i==0)continue;
		int to = E[cur][i];
		for(auto j:ps[to]){
			pp.push_back(j);
		}
	}
	pp.push_back(a[cur]);
	
	sort(pp.begin(),pp.end());
	
	pp.erase(unique(pp.begin(),pp.end()),pp.end());
	
	vector<long long> imos(pp.size(),0LL);
	
	rep(i,E[cur].size()){
		if(i==0)continue;
		int to = E[cur][i];
		int d0 = -1;
		int d1;
		for(auto j:ps[to]){
			d1 = distance(pp.begin(),lower_bound(pp.begin(),pp.end(),j));			
			long long v = seg[to].prod(j,N+1);
			imos[d1] += v;
			if(d0>=0)imos[d0] -= v;
			d0 = d1;
		}
	}
	
	for(int i=imos.size()-1;i>=1;i--){
		imos[i-1] += imos[i];
	}
	
	vector<long long> nv;
	
	rep(i,pp.size()){
		long long D = imos[i];
		int to = E[cur][0];
		D += seg[to].prod(pp[i]+1,N+1);
		if(pp[i]==a[cur])D += b[cur];
		nv.push_back(D);
	}
	
	swap(seg[cur],seg[E[cur][0]]);
	swap(ps[cur],ps[E[cur][0]]);
	
	rep(i,E[cur].size()){
		if(i==0)continue;
		int to = E[cur][i];
		int d = 0;
		for(auto j:ps[to]){
			long long v = seg[to].prod(j,N+1);
			seg[cur].apply(d,j,v);
			ps[cur].insert(j);
			d = j;
		}
	}
	
	rep(i,pp.size()){
		seg[cur].set(pp[i],nv[i]);
	}
	
	ps[cur].insert(a[cur]);
	
}

int main(){
	
	cin>>N;
	
	E.resize(N);
	
	rep(i,N-1){
		int p;
		scanf("%d",&p);
		p--;
		E[p].push_back(i+1);
	}
	
	a.resize(N);
	b.resize(N);
	
	rep(i,N){
		scanf("%d",&a[i]);
		a[i]--;
	}
	rep(i,N)scanf("%lld",&b[i]);
	
	sz.resize(N);
	dfs0(0);
	seg.resize(N,dynamic_lazy_segtree<long long,op,e,long long,mapping,composition,id>(N+1));
	ps.resize(N);
	
	dfs2(0);
	
	
	cout<<seg[0].all_prod()<<endl;
	
    return 0;
}
0