結果

問題 No.1524 Upward Mobility
ユーザー ei1333333ei1333333
提出日時 2021-05-28 21:11:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 9,050 bytes
コンパイル時間 2,594 ms
コンパイル使用メモリ 218,000 KB
実行使用メモリ 21,668 KB
最終ジャッジ日時 2024-11-07 08:47:46
合計ジャッジ時間 10,971 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
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 <bits/stdc++.h>

using namespace std;


using int64 = long long;
const int mod = 1e9 + 7;
// const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

template< typename T = int >
struct Edge {
  int from, to;
  T cost;
  int idx;

  Edge() = default;

  Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}

  operator int() const { return to; }
};

template< typename T = int >
struct Graph {
  vector< vector< Edge< T > > > g;
  int es;

  Graph() = default;

  explicit Graph(int n) : g(n), es(0) {}

  size_t size() const {
    return g.size();
  }

  void add_directed_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es++);
  }

  void add_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es);
    g[to].emplace_back(to, from, cost, es++);
  }

  void read(int M, int padding = -1, bool weighted = false, bool directed = false) {
    for(int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      a += padding;
      b += padding;
      T c = T(1);
      if(weighted) cin >> c;
      if(directed) add_directed_edge(a, b, c);
      else add_edge(a, b, c);
    }
  }
};

template< typename T = int >
using Edges = vector< Edge< T > >;

/**
 * @brief Slope-Trick
 * @docs docs/slope-trick.md
 * @see https://maspypy.com/slope-trick-1-%E8%A7%A3%E8%AA%AC%E7%B7%A8
 */
template< typename T >
struct LazySplayTree {
public:

  struct Node {
    Node *l, *r, *p;
    T key;
    T val, sum;
    size_t sz;
    T add;

    bool is_root() const {
      return !p || (p->l != this && p->r != this);
    }

    Node(const T &key, const T &val, const T &add) :
        key(key), val(val), sum(val), sz(1), add(add), l(nullptr), r(nullptr), p(nullptr) {}
  };

  LazySplayTree() = default;

  inline size_t count(const Node *t) { return t ? t->sz : 0; }

  Node *alloc(const T &key, const T &val, const T &add = T()) {
    return new Node(key, val, add);
  }

  void splay(Node *t) {
    push(t);
    while(!t->is_root()) {
      auto *q = t->p;
      if(q->is_root()) {
        push(q), push(t);
        if(q->l == t) rotr(t);
        else rotl(t);
      } else {
        auto *r = q->p;
        push(r), push(q), push(t);
        if(r->l == q) {
          if(q->l == t) rotr(q), rotr(t);
          else rotl(t), rotr(t);
        } else {
          if(q->r == t) rotl(q), rotl(t);
          else rotr(t), rotl(t);
        }
      }
    }
  }

  Node *erase(Node *t) {
    splay(t);
    Node *x = t->l, *y = t->r;
    delete t;
    if(!x) {
      t = y;
      if(t) t->p = nullptr;
    } else if(!y) {
      t = x;
      t->p = nullptr;
    } else {
      x->p = nullptr;
      t = get_right(x);
      splay(t);
      t->r = y;
      y->p = t;
    }
    return t;
  }

  Node *get_left(Node *t) const {
    while(t->l) t = t->l;
    return t;
  }

  Node *get_right(Node *t) const {
    while(t->r) t = t->r;
    return t;
  }

  void set_propagate(Node *t, const T &add) {
    splay(t);
    propagate(t, add);
    push(t);
  }

  pair< Node *, Node * > split(Node *t, int k) {
    if(!t) return {nullptr, nullptr};
    push(t);
    if(k <= count(t->l)) {
      auto x = split(t->l, k);
      t->l = x.second;
      t->p = nullptr;
      if(x.second) x.second->p = t;
      return {x.first, update(t)};
    } else {
      auto x = split(t->r, k - count(t->l) - 1);
      t->r = x.first;
      t->p = nullptr;
      if(x.first) x.first->p = t;
      return {update(t), x.second};
    }
  }

  tuple< Node *, Node *, Node * > split3(Node *t, int a, int b) {
    splay(t);
    auto x = split(t, a);
    auto y = split(x.second, b - a);
    return make_tuple(x.first, y.first, y.second);
  }

  pair< Node *, Node * > split_lower_bound(Node *t, const T &key) {
    if(!t) return {nullptr, nullptr};
    push(t);
    if(key <= t->key) {
      auto x = split_lower_bound(t->l, key);
      t->l = x.second;
      t->p = nullptr;
      if(x.second) x.second->p = t;
      return {x.first, update(t)};
    } else {
      auto x = split_lower_bound(t->r, key);
      t->r = x.first;
      t->p = nullptr;
      if(x.first) x.first->p = t;
      return {update(t), x.second};
    }
  }

  template< typename ... Args >
  Node *merge(Node *l, Args ...rest) {
    Node *r = merge(rest...);
    if(!l && !r) return nullptr;
    if(!l) return splay(r), r;
    if(!r) return splay(l), l;
    splay(l), splay(r);
    l = get_right(l);
    splay(l);
    l->r = r;
    r->p = l;
    update(l);
    return l;
  }

  Node *insert_lower_bound(Node *t, const T &key, const T &val) {
    if(t) {
      splay(t);
      auto x = split_lower_bound(t, key);
      return merge(merge(x.first, alloc(key, val)), x.second);
    } else {
      return alloc(key, val);
    }
  }

  Node *update(Node *t) {
    t->sz = 1;
    t->sum = t->val;
    if(t->l) t->sz += t->l->sz, t->sum = max(t->sum, t->l->sum);
    if(t->r) t->sz += t->r->sz, t->sum = max(t->sum, t->r->sum);
    return t;
  }

  void propagate(Node *t, const T &x) {
    t->add += x;
    t->sum += x;
    t->val += x;
  }

  void push(Node *t) {
    if(t->add) {
      if(t->l) propagate(t->l, t->add);
      if(t->r) propagate(t->r, t->add);
      t->add = 0;
    }
  }

private:
  void rotr(Node *t) {
    auto *x = t->p, *y = x->p;
    if((x->l = t->r)) t->r->p = x;
    t->r = x, x->p = t;
    update(x), update(t);
    if((t->p = y)) {
      if(y->l == x) y->l = t;
      if(y->r == x) y->r = t;
      update(y);
    }
  }

  void rotl(Node *t) {
    auto *x = t->p, *y = x->p;
    if((x->r = t->l)) t->l->p = x;
    t->l = x, x->p = t;
    update(x), update(t);
    if((t->p = y)) {
      if(y->l == x) y->l = t;
      if(y->r == x) y->r = t;
      update(y);
    }
  }

  Node *merge(Node *l) {
    return l;
  }
};


int main() {
  int N;
  cin >> N;
  Graph<> g(N);
  for(int i = 1; i < N; i++) {
    int x;
    cin >> x;
    --x;
    g.add_directed_edge(x, i);
  }
  vector< int > A(N), B(N);
  cin >> A >> B;
  for(auto &a : A) --a;
  LazySplayTree< int64 > lst;
  vector< LazySplayTree< int64 >::Node * > dp(N);
  vector< pair< int64, int64 > > adds;

  auto dfs = MFP([&](auto dfs, int idx) -> void {
    int64 sum = 0;
    for(auto &to : g.g[idx]) {
      dfs(to);
      if(lst.count(dp[to]) > lst.count(dp[idx])) {
        swap(dp[to], dp[idx]);
      }
      int64 pre_key = -inf;
      while(lst.count(dp[to])) {
        dp[to] = lst.get_left(dp[to]);
        lst.splay(dp[to]);
        int64 key = dp[to]->key;
        int64 val = dp[to]->val;
        auto[l, buf] = lst.split_lower_bound(dp[idx], pre_key);
        auto[m, r] = lst.split_lower_bound(buf, dp[to]->key);
        if(m) lst.set_propagate(m, val);
        if(r) val += r->sum;
        dp[idx] = lst.merge(l, m, r);
        adds.emplace_back(key, val);
        pre_key = key;
        dp[to] = lst.erase(dp[to]);
      }
      for(auto&[key, val] : adds) {
        dp[idx] = lst.insert_lower_bound(dp[idx], key, val);
      }
      adds.clear();
    }
    auto[l, r]=lst.split_lower_bound(dp[idx], A[idx]);
    if(r)B[idx] += r->sum;
    dp[idx] = lst.merge(dp[idx], lst.alloc(A[idx], B[idx]), r);
  });
  dfs(0);
  cout << dp[0]->sum << "\n";
}

0