結果

問題 No.1524 Upward Mobility
ユーザー 沙耶花沙耶花
提出日時 2021-05-05 14:09:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,062 bytes
コンパイル時間 5,202 ms
コンパイル使用メモリ 274,560 KB
実行使用メモリ 50,424 KB
最終ジャッジ日時 2024-04-17 07:12:49
合計ジャッジ時間 23,112 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 325 ms
27,144 KB
testcase_01 AC 309 ms
27,148 KB
testcase_02 AC 400 ms
27,144 KB
testcase_03 AC 5,974 ms
31,564 KB
testcase_04 AC 328 ms
23,608 KB
testcase_05 AC 337 ms
23,632 KB
testcase_06 AC 336 ms
23,820 KB
testcase_07 AC 329 ms
23,388 KB
testcase_08 AC 338 ms
23,572 KB
testcase_09 AC 342 ms
23,600 KB
testcase_10 AC 348 ms
24,024 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

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

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

vector<int> sz;

struct Data{
	long long v = 0;;
	bool f = false;
};

using Segtree = vector<long long>;

vector<vector<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 dfs1(int cur,vector<int> &t){
	if(t.size()==0){
		queue<int> Q;
		Q.push(cur);
		t.push_back(N);
		while(Q.size()>0){
			int u = Q.front();
			Q.pop();
			t.push_back(a[u]);
			rep(i,E[u].size()){
				int to = E[u][i];
				Q.push(to);
			}
		}
		sort(t.begin(),t.end());
	}
	
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(i==0){
			dfs1(to,t);
		}
		else{
			t.resize(0);
			dfs1(to,t);
		}
	}
	
	if(E[cur].size()==0){
		ps[cur] = t;
		seg[cur].resize(t.size(),0LL);
	}
	
}

void dfs2(int cur){
	if(E[cur].size()==0){
		int d = distance(ps[cur].begin(),lower_bound(ps[cur].begin(),ps[cur].end(),a[cur]));
		seg[cur][d] = max(seg[cur][d],b[cur]);
		seg[cur].back() = 0;
		
		for(int i=seg[cur].size()-1;i>=1;i--){
			seg[cur][i-1] = max(seg[cur][i-1],seg[cur][i]);
		}
		return;
	}
	
	if(E[cur].size()==1){
		int to = E[cur][0];
		dfs2(to);
		swap(ps[cur],ps[to]);
		swap(seg[cur],seg[to]);

		int d = distance(ps[cur].begin(),lower_bound(ps[cur].begin(),ps[cur].end(),a[cur]));

		seg[cur][d] = max(seg[cur][d] + b[cur],seg[cur][d]);
		
		for(int i=seg[cur].size()-1;i>=1;i--){
			seg[cur][i-1] = max(seg[cur][i-1],seg[cur][i]);
		}
		
		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];
		rep(j,ps[to].size()){
			pp.push_back(ps[to][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];
		rep(j,ps[to].size()){
			int d0;
			if(j!=0)d0 = distance(pp.begin(),lower_bound(pp.begin(),pp.end(),ps[to][j-1]));
			else d0 = -1;
			int d1 = distance(pp.begin(),lower_bound(pp.begin(),pp.end(),ps[to][j]));
			long long v = seg[to][j];
			imos[d1] += v;
			if(d0>=0)imos[d0] -= v;
		}
	}
	
	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];
		int d = distance(ps[to].begin(),upper_bound(ps[to].begin(),ps[to].end(),pp[i]));
		if(d<ps[to].size())D += seg[to][d];
		if(pp[i]==a[cur])D += b[cur];
		nv.push_back(D);
	}
	/*
	cout<<cur<<endl;
	rep(i,pp.size()){
		cout<<pp[i]<<','<<nv[i]<<endl;
	}
	*/
	
	
	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];
		rep(j,ps[to].size()){
			int d0;
			if(j!=0) d0 = distance(ps[cur].begin(),lower_bound(ps[cur].begin(),ps[cur].end(),ps[to][j-1]));
			else d0 = 0;
			int d1 = distance(ps[cur].begin(),lower_bound(ps[cur].begin(),ps[cur].end(),ps[to][j]));
			long long v = seg[to][j];
			for(int k=d0;k<d1;k++){
				seg[cur][k] += v;
			}
		}
	}
	
	rep(i,pp.size()){
		int d = distance(ps[cur].begin(),lower_bound(ps[cur].begin(),ps[cur].end(),pp[i]));
		seg[cur][d] = max(seg[cur][d],nv[i]);
	}
	long long l = seg[cur].back();
	for(int i=seg[cur].size()-1;i>=1;i--){
		l = max(l,seg[cur][i-1]);
		seg[cur][i-1] = l;
	}
	
	//cout<<cur<<','<<seg[cur][0]<<endl;
}

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);
	ps.resize(N);
	vector<int> t;
	dfs1(0,t);
	dfs2(0);
	
	cout<<seg[0][0]<<endl;
	
    return 0;
}
0