結果

問題 No.1524 Upward Mobility
ユーザー 沙耶花沙耶花
提出日時 2021-05-13 13:47:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 803 ms / 6,000 ms
コード長 3,863 bytes
コンパイル時間 5,359 ms
コンパイル使用メモリ 278,228 KB
実行使用メモリ 62,104 KB
最終ジャッジ日時 2024-04-17 07:15:47
合計ジャッジ時間 19,372 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 726 ms
50,672 KB
testcase_01 AC 696 ms
50,672 KB
testcase_02 AC 803 ms
50,676 KB
testcase_03 AC 244 ms
46,300 KB
testcase_04 AC 647 ms
45,488 KB
testcase_05 AC 640 ms
45,236 KB
testcase_06 AC 650 ms
45,748 KB
testcase_07 AC 628 ms
44,844 KB
testcase_08 AC 656 ms
44,600 KB
testcase_09 AC 645 ms
45,492 KB
testcase_10 AC 662 ms
45,880 KB
testcase_11 AC 133 ms
62,104 KB
testcase_12 AC 136 ms
62,100 KB
testcase_13 AC 156 ms
62,100 KB
testcase_14 AC 218 ms
48,368 KB
testcase_15 AC 217 ms
48,496 KB
testcase_16 AC 613 ms
44,388 KB
testcase_17 AC 526 ms
38,912 KB
testcase_18 AC 115 ms
12,364 KB
testcase_19 AC 482 ms
36,140 KB
testcase_20 AC 156 ms
15,092 KB
testcase_21 AC 191 ms
17,428 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 236 ms
34,508 KB
testcase_30 AC 242 ms
34,500 KB
testcase_31 AC 290 ms
34,376 KB
testcase_32 AC 760 ms
53,076 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

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 = lazy_segtree<long long,op,e,long long,mapping,composition,id>;

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] = Segtree(t.size());
	}
	
}

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].set(d,b[cur]);
		seg[cur].set(ps[cur].size()-1,0);
		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].set(d,seg[cur].prod(d+1,ps[cur].size())+b[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];
		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].prod(j,ps[to].size());
			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]));
		D += seg[to].prod(d,ps[to].size());
		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];
		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].prod(j,ps[to].size());
			seg[cur].apply(d0,d1,v);
		}
	}
	
	rep(i,pp.size()){
		int d = distance(ps[cur].begin(),lower_bound(ps[cur].begin(),ps[cur].end(),pp[i]));
		seg[cur].set(d,nv[i]);
	}
	
}

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].all_prod()<<endl;
	
    return 0;
}
0