結果

問題 No.1524 Upward Mobility
ユーザー 👑 NachiaNachia
提出日時 2021-05-18 18:27:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 83 ms / 6,000 ms
コード長 1,185 bytes
コンパイル時間 1,175 ms
コンパイル使用メモリ 98,560 KB
実行使用メモリ 16,360 KB
最終ジャッジ日時 2024-04-17 07:18:38
合計ジャッジ時間 4,220 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
10,496 KB
testcase_01 AC 66 ms
10,368 KB
testcase_02 AC 72 ms
10,424 KB
testcase_03 AC 65 ms
12,492 KB
testcase_04 AC 78 ms
10,624 KB
testcase_05 AC 79 ms
10,428 KB
testcase_06 AC 83 ms
10,496 KB
testcase_07 AC 76 ms
10,584 KB
testcase_08 AC 76 ms
10,496 KB
testcase_09 AC 75 ms
10,408 KB
testcase_10 AC 76 ms
10,624 KB
testcase_11 AC 60 ms
16,232 KB
testcase_12 AC 59 ms
16,360 KB
testcase_13 AC 82 ms
16,228 KB
testcase_14 AC 68 ms
12,688 KB
testcase_15 AC 58 ms
12,692 KB
testcase_16 AC 79 ms
10,496 KB
testcase_17 AC 66 ms
9,600 KB
testcase_18 AC 18 ms
6,940 KB
testcase_19 AC 58 ms
9,088 KB
testcase_20 AC 22 ms
6,944 KB
testcase_21 AC 25 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 1 ms
6,944 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 1 ms
6,940 KB
testcase_29 AC 47 ms
9,356 KB
testcase_30 AC 47 ms
9,356 KB
testcase_31 AC 52 ms
9,356 KB
testcase_32 AC 65 ms
9,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <atcoder/segtree>

using namespace std;
using ll = long long;
using ull = unsigned long long;

int RQop(int l,int r){ return max(l,r); }
int RQe(){ return -1; }
using RQ = atcoder::segtree<int,RQop,RQe>;

int N;
vector<vector<int>> E;
vector<int> A;
vector<ll> B;
RQ G;
vector<ll> dp;

int dfs(int p,int i=0){
  int pre_i = i;
  for(int e : E[p]) i = dfs(e,i) + 1;
  G.set(A[p],i);
  dp[A[p]] = B[p];
  ll b = B[p];
  while(true){
    int nx = G.min_left(A[p],[pre_i](int j){ return pre_i > j; }) - 1;
    if(nx == -1) break;
    if(dp[nx] > b){ dp[nx] -= b; break; }
    b -= dp[nx]; dp[nx] = 0;
    G.set(nx,-1);
  }
  return i;
}

int main(){
  cin >> N;
  E.resize(N);
  for(int i=1; i<N; i++){
    int p; cin >> p; p--;
    E[p].push_back(i);
  }
  A.resize(N);
  for(int i=0; i<N; i++){ cin >> A[i]; A[i]--; }
  B.resize(N);
  for(int i=0; i<N; i++) cin >> B[i];

  dp.assign(N,0);
  G = RQ(N);

  dfs(0);

  ll ans = 0;
  for(int i=0; i<N; i++) ans += dp[i];
  cout << ans << "\n";
  return 0;
}


struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_instance;

0