結果

問題 No.1215 都市消滅ビーム
ユーザー ei1333333ei1333333
提出日時 2020-08-30 17:31:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 13,532 bytes
コンパイル時間 4,900 ms
コンパイル使用メモリ 277,100 KB
実行使用メモリ 24,320 KB
最終ジャッジ日時 2024-11-15 13:50:36
合計ジャッジ時間 53,889 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 8 ms
5,248 KB
testcase_08 AC 3 ms
5,248 KB
testcase_09 AC 4 ms
5,248 KB
testcase_10 AC 1 ms
5,248 KB
testcase_11 AC 4 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 1,500 ms
10,364 KB
testcase_14 AC 962 ms
9,076 KB
testcase_15 AC 2,722 ms
16,732 KB
testcase_16 AC 573 ms
7,680 KB
testcase_17 AC 852 ms
14,884 KB
testcase_18 AC 1,010 ms
12,792 KB
testcase_19 AC 471 ms
6,816 KB
testcase_20 AC 2,037 ms
15,052 KB
testcase_21 AC 123 ms
6,816 KB
testcase_22 AC 1,479 ms
11,808 KB
testcase_23 AC 1,397 ms
14,156 KB
testcase_24 AC 1,170 ms
10,848 KB
testcase_25 AC 689 ms
9,920 KB
testcase_26 AC 1,428 ms
14,760 KB
testcase_27 AC 1,542 ms
18,104 KB
testcase_28 AC 936 ms
9,140 KB
testcase_29 AC 854 ms
9,284 KB
testcase_30 AC 409 ms
14,848 KB
testcase_31 AC 307 ms
7,168 KB
testcase_32 AC 73 ms
13,568 KB
testcase_33 AC 82 ms
11,648 KB
testcase_34 AC 5,138 ms
24,088 KB
testcase_35 AC 4,617 ms
24,112 KB
testcase_36 AC 4,564 ms
24,064 KB
testcase_37 WA -
testcase_38 AC 3,534 ms
24,176 KB
testcase_39 AC 4,583 ms
24,144 KB
testcase_40 AC 2 ms
6,816 KB
testcase_41 AC 2 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

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 Heavy-Light-Decomposition(HL分解)
 * @see https://smijake3.hatenablog.com/entry/2019/09/15/200200
 */
template< typename T = int >
struct HeavyLightDecomposition : Graph< T > {
public:
  using Graph< T >::Graph;
  using Graph< T >::g;
  vector< int > sz, in, out, head, rev, par, dep;

  void build() {
    sz.assign(g.size(), 0);
    in.assign(g.size(), 0);
    out.assign(g.size(), 0);
    head.assign(g.size(), 0);
    rev.assign(g.size(), 0);
    par.assign(g.size(), 0);
    dep.assign(g.size(), 0);
    dfs_sz(0, -1, 0);
    int t = 0;
    dfs_hld(0, -1, t);
  }

  /* k: 0-indexed */
  int la(int v, int k) {
    while(1) {
      int u = head[v];
      if(in[v] - k >= in[u]) return rev[in[v] - k];
      k -= in[v] - in[u] + 1;
      v = par[u];
    }
  }

  int lca(int u, int v) const {
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v);
      if(head[u] == head[v]) return u;
    }
  }

  int dist(int u, int v) const {
    return dep[u] + dep[v] - 2 * dep[lca(u, v)];
  }

  template< typename E, typename Q, typename F, typename S >
  E query(int u, int v, const E &ti, const Q &q, const F &f, const S &s, bool edge = false) {
    E l = ti, r = ti;
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v), swap(l, r);
      if(head[u] == head[v]) break;
      l = f(q(in[head[v]], in[v] + 1), l);
    }
    return s(f(q(in[u] + edge, in[v] + 1), l), r);
  }

  template< typename E, typename Q, typename F >
  E query(int u, int v, const E &ti, const Q &q, const F &f, bool edge = false) {
    return query(u, v, ti, q, f, f, edge);
  }

  template< typename Q >
  void add(int u, int v, const Q &q, bool edge = false) {
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v);
      if(head[u] == head[v]) break;
      q(in[head[v]], in[v] + 1);
    }
    q(in[u] + edge, in[v] + 1);
  }

  /* {parent, child} */
  vector< pair< int, int > > compress(vector< int > &remark) {
    auto cmp = [&](int a, int b) { return in[a] < in[b]; };
    sort(begin(remark), end(remark), cmp);
    remark.erase(unique(begin(remark), end(remark)), end(remark));
    int K = (int) remark.size();
    for(int k = 1; k < K; k++) remark.emplace_back(lca(remark[k - 1], remark[k]));
    sort(begin(remark), end(remark), cmp);
    remark.erase(unique(begin(remark), end(remark)), end(remark));
    vector< pair< int, int > > es;
    stack< int > st;
    for(auto &k : remark) {
      while(!st.empty() && out[st.top()] <= in[k]) st.pop();
      if(!st.empty()) es.emplace_back(st.top(), k);
      st.emplace(k);
    }
    return es;
  }

  explicit HeavyLightDecomposition(const Graph< T > &g) : Graph< T >(g) {}

private:
  void dfs_sz(int idx, int p, int d) {
    dep[idx] = d;
    par[idx] = p;
    sz[idx] = 1;
    if(g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back());
    for(auto &to : g[idx]) {
      if(to == p) continue;
      dfs_sz(to, idx, d + 1);
      sz[idx] += sz[to];
      if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to);
    }
  }

  void dfs_hld(int idx, int p, int &times) {
    in[idx] = times++;
    rev[in[idx]] = idx;
    for(auto &to : g[idx]) {
      if(to == p) continue;
      head[to] = (g[idx][0] == to ? head[idx] : to);
      dfs_hld(to, idx, times);
    }
    out[idx] = times;
  }
};


/**
 * @brief Binary-Indexed-Tree(BIT)
 * @docs docs/binary-indexed-tree.md
 */
template< typename T >
struct BinaryIndexedTree {
  vector< T > data;

  BinaryIndexedTree() = default;

  explicit BinaryIndexedTree(size_t sz) : data(sz + 1, 0) {}

  explicit BinaryIndexedTree(const vector< T > &vs) : data(vs.size() + 1, 0) {
    for(size_t i = 0; i < vs.size(); i++) data[i + 1] = vs[i];
    for(size_t i = 1; i < data.size(); i++) {
      size_t j = i + (i & -i);
      if(j < data.size()) data[j] += data[i];
    }
  }

  void add(int k, const T &x) {
    for(++k; k < (int) data.size(); k += k & -k) data[k] += x;
  }

  T query(int k) const {
    T ret = T();
    for(++k; k > 0; k -= k & -k) ret += data[k];
    return ret;
  }

  int lower_bound(T x) const {
    int i = 0;
    for(int k = 1 << (__lg(data.size() - 1) + 1); k > 0; k >>= 1) {
      if(i + k < data.size() && data[i + k] < x) {
        x -= data[i + k];
        i += k;
      }
    }
    return i;
  }

  int upper_bound(T x) const {
    int i = 0;
    for(int k = 1 << (__lg(data.size() - 1) + 1); k > 0; k >>= 1) {
      if(i + k < data.size() && data[i + k] <= x) {
        x -= data[i + k];
        i += k;
      }
    }
    return i;
  }
};

#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using pi = pair< int64, int64 >;
using Tree = tree< pi, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update >;


int main() {
  int N, K;
  cin >> N >> K;
  vector< int > C(K), D(K);
  cin >> C >> D;
  for(auto &c: C) --c;
  HeavyLightDecomposition< int > g(N);
  g.read(N - 1);
  g.build();

  vector< int > latte(K);
  vector< int64 > sum_latte(K);
  latte[0] = C[0];
  sum_latte[0] = D[0];
  for(int i = 1; i < K; i++) {
    latte[i] = g.lca(latte[i - 1], C[i]);
    sum_latte[i] = sum_latte[i - 1] + D[i];
  }
  vector< int > malta(K);
  vector< int64 > sum_malta(K);
  malta[K - 1] = C[K - 1];
  sum_malta[K - 1] = D[K - 1];
  for(int i = K - 2; i >= 0; i--) {
    malta[i] = g.lca(malta[i + 1], C[i]);
    sum_malta[i] = sum_malta[i + 1] + D[i];
  }

  auto check2 = [&](int64 val) {
    int64 ret = 0;
    ret += sum_latte.back() <= val;
    Tree tree;
    tree.insert(make_pair(0, -1));
    for(int right = 0; right < K; right++) {
      ret += tree.order_of_key(make_pair(val - sum_latte.back() + sum_latte[right], inf));
      tree.insert(make_pair(sum_latte[right], right));
    }
    return ret;
  };
  int64 pre_ok = infll / 100, pre_ng = -inf / 100;
  while(pre_ok - pre_ng > 1) {
    int64 pre_mid = (pre_ok + pre_ng) / 2;
    if(check2(pre_mid) >= ((1LL * K * (K + 1) / 2 + 1) + 1) / 2 + 1) pre_ok = pre_mid;
    else pre_ng = pre_mid;
  }


  vector< int > dep(N);
  auto make = MFP([&](auto make, int idx, int par) -> void {
    if(~par) dep[idx] = dep[par] + 1;
    for(auto &to : g.g[idx]) {
      if(to != par) make(to, idx);
    }
  });
  make(0, -1);


  int64 vt = dep[latte.back()] + sum_latte.back();

  vector< int > id(N);
  auto check = [&](int64 ck) {
    int64 ret = 0;

    auto rec = MFP([&](auto rec, int left, int right) -> void {
      if(left + 1 >= right) {
        int lca1 = left == 0 ? -1 : latte[left - 1];
        int lca2 = right == K ? -1 : malta[right];
        int lca = lca1 == -1 ? lca2 : (lca2 == -1 ? lca1 : g.lca(lca1, lca2));
        int64 sum1 = left == 0 ? 0 : sum_latte[left - 1];
        int64 sum2 = right == K ? 0 : sum_malta[right];
        if(dep[lca] + sum1 + sum2 <= ck) ++ret;
        return;
      }
      int mid = (left + right) / 2;
      rec(left, mid);
      rec(mid, right);
      // [left, mid) -> [mid, right)
      vector< int > vs{0};
      for(int k = left; k < mid; k++) {
        int lca = k == 0 ? -1 : latte[k - 1];
        if(~lca) vs.emplace_back(lca);
      }
      for(int k = mid; k < right; k++) {
        int lca = k + 1 == K ? -1 : malta[k + 1];
        if(~lca) vs.emplace_back(lca);
      }
      sort(begin(vs), end(vs));
      vs.erase(unique(begin(vs), end(vs)), end(vs));
      auto es = g.compress(vs);
      int ptr = 0;
      for(auto &p : es) id[p.first] = -1;
      for(auto &p : es) id[p.second] = -1;
      id[0] = ptr++;
      vector< int > rev{0};
      for(auto &p : es) {
        if(id[p.first] == -1) {
          id[p.first] = ptr++;
          rev.emplace_back(p.first);
        }
        if(id[p.second] == -1) {
          id[p.second] = ptr++;
          rev.emplace_back(p.second);
        }
      }
      vector< vector< int > > subg(rev.size());
      for(auto &p : es) {
        subg[id[p.first]].emplace_back(id[p.second]);
      }
      vector< Tree > tap(rev.size()), ris(rev.size());
      vector< int64 > x, y;
      for(int k = left; k < mid; k++) {
        int lca = k == 0 ? -1 : latte[k - 1];
        int64 sum = k == 0 ? 0 : sum_latte[k - 1];
        if(~lca) tap[id[lca]].insert({sum, k});
        else x.emplace_back(sum);
      }
      for(int k = mid; k < right; k++) {
        int lca = k + 1 == K ? -1 : malta[k + 1];
        int64 sum = k + 1 == K ? 0 : sum_malta[k + 1];
        if(~lca) ris[id[lca]].insert({sum, k});
        else y.emplace_back(sum);
      }
      sort(begin(x), end(x));
      sort(begin(y), end(y));
      auto dfs = MFP([&](auto dfs, int idx) -> void {
        for(auto &val : tap[idx]) {
          ret += upper_bound(begin(y), end(y), ck - val.first - dep[rev[idx]]) - begin(y);
        }
        for(auto &val : ris[idx]) {
          ret += upper_bound(begin(x), end(x), ck - val.first - dep[rev[idx]]) - begin(x);
        }
        for(auto &val : tap[idx]) {
          ret += ris[idx].order_of_key(make_pair(ck - val.first - dep[rev[idx]], inf));
        }
        for(auto &to : subg[idx]) {
          dfs(to);
          if(tap[to].size() < ris[idx].size()) {
            for(auto &val : tap[to]) {
              ret += ris[idx].order_of_key(make_pair(ck - val.first - dep[rev[idx]], inf));
            }
          } else {
            for(auto &val : ris[idx]) {
              ret += tap[to].order_of_key(make_pair(ck - val.first - dep[rev[idx]], inf));
            }
          }
          if(ris[to].size() < tap[idx].size()) {
            for(auto &val : ris[to]) {
              ret += tap[idx].order_of_key(make_pair(ck - val.first - dep[rev[idx]], inf));
            }
          } else {
            for(auto &val : tap[idx]) {
              ret += ris[to].order_of_key(make_pair(ck - val.first - dep[rev[idx]], inf));
            }
          }
          if(tap[to].size() > tap[idx].size()) tap[idx].swap(tap[to]);
          if(ris[to].size() > ris[idx].size()) ris[idx].swap(ris[to]);
          for(auto &p : tap[to]) tap[idx].insert(p);
          for(auto &p : ris[to]) ris[idx].insert(p);
          tap[to].clear();
          ris[to].clear();
        }
      });
      dfs(0);
    });
    rec(0, K);
    ret += vt <= ck;
    return ret + 1;
  };
  int64 ok = pre_ok + *max_element(begin(dep), end(dep)), ng = pre_ok - 10;
  if(N < 5000) ok = infll / 100, ng = -infll / 100;
  while(ok - ng > 1) {
    int64 mid = (ok + ng) / 2;
    if(check(mid) >= ((1LL * K * (K + 1) / 2 + 1) + 1) / 2) ok = mid;
    else ng = mid;
  }
  cout << ok << "\n";
}
0