結果

問題 No.399 動的な領主
ユーザー OrisanoOrisano
提出日時 2017-11-20 03:39:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 363 ms / 2,000 ms
コード長 5,833 bytes
コンパイル時間 1,454 ms
コンパイル使用メモリ 73,728 KB
実行使用メモリ 30,488 KB
最終ジャッジ日時 2024-11-26 01:35:08
合計ジャッジ時間 5,809 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 3 ms
5,248 KB
testcase_05 AC 19 ms
5,248 KB
testcase_06 AC 363 ms
19,976 KB
testcase_07 AC 350 ms
20,104 KB
testcase_08 AC 340 ms
22,212 KB
testcase_09 AC 331 ms
22,072 KB
testcase_10 AC 3 ms
5,248 KB
testcase_11 AC 14 ms
5,504 KB
testcase_12 AC 206 ms
30,360 KB
testcase_13 AC 208 ms
30,488 KB
testcase_14 AC 72 ms
14,168 KB
testcase_15 AC 91 ms
14,168 KB
testcase_16 AC 112 ms
22,080 KB
testcase_17 AC 354 ms
22,200 KB
testcase_18 AC 355 ms
22,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <functional>
#include <vector>

struct HLDecomposition {
  struct Node {
    int ord, par, depth;
    int idx;
    int path_id = -1;
    Node() {}
  };

  const int N;
  std::vector<std::vector<int>> tree, pathes;
  std::vector<Node> nodes;
  std::vector<int> Q;

  HLDecomposition(int N) : N(N), tree(N), nodes(N) {}
  void add_edge(int u, int v) {
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  void build(int root = 0) {
    Q.reserve(N);
    Q.push_back(root);
    nodes[root].par = -1;
    nodes[root].depth = 0;
    for (int i = 0; i < Q.size(); i++) {
      int u = Q[i];
      auto& U = nodes[u];
      U.ord = i;
      for (int v : tree[u]) {
        if (U.par == v) continue;
        auto& V = nodes[v];
        V.par = u;
        V.depth = U.depth + 1;
        Q.push_back(v);
      }
    }
    decomposition();
  }
  void for_each(int u, int v, std::function<void(int, int, int)> f) const {
    while (nodes[u].path_id != nodes[v].path_id) {
      int head_u = pathes[nodes[u].path_id][0];
      int head_v = pathes[nodes[v].path_id][0];
      if (nodes[head_u].depth > nodes[head_v].depth) {
        std::swap(u, v);
        std::swap(head_u, head_v);
      }
      f(nodes[v].path_id, 0, nodes[v].idx + 1);
      v = nodes[head_v].par;
    }
    if (nodes[v].idx < nodes[u].idx) std::swap(u, v);
    f(nodes[v].path_id, nodes[u].idx, nodes[v].idx + 1);
  }
  int lca(int u, int v) const {
    int x;
    for_each(u, v, [&](int p, int l, int r) { x = pathes[p][l]; });
    return x;
  }
  void decomposition() {
    std::vector<int> subtree_size(N, 1);
    for (int i = N - 1; i > 0; i--) {
      subtree_size[nodes[Q[i]].par] += subtree_size[Q[i]];
    }
    for (int u : Q) {
      auto& U = nodes[u];
      if (U.path_id == -1) {
        U.path_id = pathes.size();
        pathes.emplace_back();
      }
      pathes[U.path_id].push_back(u);

      int max_subsize = -1, selected = -1;
      for (int v : tree[u]) {
        if (U.par == v) continue;
        if (max_subsize >= subtree_size[v]) continue;
        max_subsize = subtree_size[v];
        selected = v;
      }
      if (selected != -1) nodes[selected].path_id = U.path_id;
    }
    for (auto&& path : pathes) {
      for (int i = 0; i < path.size(); i++) {
        nodes[path[i]].idx = i;
      }
    }
  }
};

template <typename Monoid, typename Op>
struct LazySegTree {
  using value_type = typename Monoid::value_type;
  using lazy_type = typename Op::value_type;
  using Index0 = int;

  const lazy_type none = Op::none();
  const int N, H;
  std::vector<value_type> nodes;
  std::vector<lazy_type> lazy;

  LazySegTree(int size) : LazySegTree(size, 32 - __builtin_clz(size - 1)) {}
  LazySegTree(int size, int h) : LazySegTree(size, h, 1 << h) {}
  LazySegTree(int size, int h, int aligned)
      : N(aligned), H(h), nodes(aligned * 2, Monoid::empty()), lazy(aligned, none) {}

  inline void apply(int p, lazy_type value, int k) {
    nodes[p] = Op::eval(nodes[p], value, k);
    if (p < N) lazy[p] = Op::merge(lazy[p], value);
  }
  inline void calc(int p, int k) {
    auto x = Monoid::append(nodes[p << 1], nodes[p << 1 | 1]);
    if (lazy[p] == none) {
      nodes[p] = x;
    } else {
      nodes[p] = Op::eval(x, lazy[p], k);
    }
  }
  void build(Index0 l, Index0 r) {
    int k = 2;
    for (l += N, r += N - 1; l > 1; k <<= 1) {
      l >>= 1, r >>= 1;
      for (int i = r; i >= l; --i) calc(i, k);
    }
  }
  void push(Index0 l, Index0 r) {
    int s = H, k = 1 << (H - 1);
    for (l += N, r += N - 1; s > 0; --s, k >>= 1) {
      for (int i = l >> s; i <= r >> s; ++i) {
        if (lazy[i] != none) {
          apply(i << 1, lazy[i], k);
          apply(i << 1 | 1, lazy[i], k);
          lazy[i] = none;
        }
      }
    }
  }
  void exec(Index0 l, Index0 r, lazy_type value) {
    if (value == none) return;
    int L = l, R = r, k = 1;
    push(L, L + 1);
    push(R - 1, R);
    for (l += N, r += N; l < r; l >>= 1, r >>= 1, k <<= 1) {
      if (l & 1) apply(l++, value, k);
      if (r & 1) apply(--r, value, k);
    }
    build(L, L + 1);
    build(R - 1, R);
  }
  value_type query(Index0 l, Index0 r) {
    push(l, l + 1);
    push(r - 1, r);
    value_type x = Monoid::empty();
    for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
      if (l & 1) x = Monoid::append(x, nodes[l++]);
      if (r & 1) x = Monoid::append(x, nodes[--r]);
    }
    return x;
  }
};
#include <cstdio>
#define mygc(c) (c) = getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
// clang-format off
template<typename T=int>inline T rd(){T x=0,m=0,k;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||'9'<k)break;x=x*10+k-'0';}if(m)x=-x;return x;}
template<typename T=int>inline void wr(T x,char c='\n'){int s=0,m=0;char b[32];if(x<0)m=1,x=-x;for(;x;x/=10)b[s++]=x%10;if(!s)b[s++]=0;if(m)mypc('-');for(;s--;)mypc(b[s]+'0');mypc(c);}
// clang-format on

using ll = long long;

struct Sum {
  using value_type = ll;
  static ll empty() { return 0; }
  static ll append(ll x, ll y) { return x + y; }
};

struct Add {
  using value_type = ll;
  static ll none() { return 0; }
  static ll eval(ll x, ll v, int k) { return x + v * k; }
  static ll merge(ll x, ll y) { return x + y; }
};

int main() {
  int N = rd();
  HLDecomposition hl(N);
  for (int i = 0; i < N - 1; i++) {
    int u = rd() - 1, v = rd() - 1;
    hl.add_edge(u, v);
  }
  hl.build();

  std::vector<LazySegTree<Sum, Add>> segs;
  segs.reserve(hl.pathes.size());
  for (auto&& path : hl.pathes) {
    segs.emplace_back(path.size() + 1);
  }

  int Q = rd();
  ll ans = 0;
  for (int i = 0; i < Q; i++) {
    int A = rd() - 1, B = rd() - 1;
    hl.for_each(A, B, [&](int p, int l, int r) {
      segs[p].exec(l, r, 1);
      ans += segs[p].query(l, r);
    });
  }
  wr(ans);
  return 0;
}
0