結果

問題 No.399 動的な領主
ユーザー arg-53arg-53
提出日時 2018-04-26 16:06:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 4,052 bytes
コンパイル時間 1,770 ms
コンパイル使用メモリ 105,272 KB
実行使用メモリ 19,152 KB
最終ジャッジ日時 2023-09-10 05:36:34
合計ジャッジ時間 5,186 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 9 ms
4,384 KB
testcase_06 AC 123 ms
14,712 KB
testcase_07 AC 123 ms
14,764 KB
testcase_08 AC 118 ms
14,380 KB
testcase_09 AC 117 ms
14,712 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 8 ms
4,384 KB
testcase_12 AC 80 ms
15,196 KB
testcase_13 AC 79 ms
15,420 KB
testcase_14 AC 63 ms
19,148 KB
testcase_15 AC 65 ms
19,152 KB
testcase_16 AC 64 ms
17,028 KB
testcase_17 AC 121 ms
14,320 KB
testcase_18 AC 120 ms
14,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// solution by min_25, modified
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
// #pragma GCC target ("sse4") // SPOJ, codechef

#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <functional>
#include <tuple>

#define _rep(_1, _2, _3, _4, name, ...) name
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c))
#define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__)

using namespace std;

using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f80 = long double;

using Edges = vector< vector<int> >;

// ref. pekempey さん
struct HLD {
  struct node {
    int id, par, head;
  };

  HLD(const Edges& edges) : N(edges.size()), edges(edges), tree(N) {
    dfs();
    path();
  }

  int dfs(int v=0, int p=-1) {
    int cnt = 1, x = 0, h = -1;
    for (auto w : edges[v]) if (w != p) {
      tree[w].par = v;
      int c = dfs(w, v);
      if (c > x) x = c, h = w;
      cnt += c;
    }
    tree[v].head = h;
    return cnt;
  }

  void path() {
    int id = 0;
    tree[id].par = -1;
    queue<int> que; que.push(id);
    while (!que.empty()) {
      int len = 0, v = que.front(); que.pop();
      for (int w = v, h = -1; w >= 0; w = h, ++len) {
        tree[w].id = id++; h = tree[w].head; tree[w].head = v;
        for (auto x : edges[w]) if (x != h && x != tree[w].par) que.push(x);
      }
      tree[v].head = -len;
    }
  }

  int head(int v) const {
    return tree[v].head < 0 ? v : tree[v].head;
  }

  template <typename func_t>
  void update(int v, int w, func_t func) {
    while (1) {
      if (tree[v].id < tree[w].id) swap(v, w);
      int hv = head(v), ofs = tree[hv].id, size = -tree[hv].head;
      if (hv != head(w)) {
        func(0, tree[v].id - ofs + 1, ofs, size);
        v = tree[hv].par;
        continue;
      }
      func(tree[w].id - ofs, tree[v].id - ofs + 1, ofs, size);
      break;
    }
  }
  int N;
  const Edges& edges;
  vector<node> tree;
};
Edges edges;

struct FenwickTree {
  struct node {
    node() : d0(0), d1(0) {}
    i64 d0, d1;
  };
  FenwickTree(int n) : buff_size(2 * n) {
    buff = new node[buff_size];
    rep(i, buff_size) buff[i] = node();
  }
  ~FenwickTree() { delete [] buff; }

  void reset(int o, int s) {
    size = s;
    tree = buff + o * 1;
  }

  void add(int l, i64 v0, i64 v1) {
    for (; l <= size; l += l & -l) {
      tree[l].d0 += v0;
      tree[l].d1 += v1;
    }
  }

  void add(int l, int r, int v, int o, int s) {
    reset(o, s);
    add(l + 1, i64(-v) * l, v);
    add(r + 1, i64(v) * r, -v);
  }

  i64 sum(int l) {
    int ll = l;
    i64 r0 = 0, r1 = 0;
    for (; l; l &= l - 1) {
      r0 += tree[l].d0;
      r1 += tree[l].d1;
    }
    return r0 + ll * r1;
  }

  i64 sum(int l, int r, int o, int s) {
    reset(o, s);
    return sum(r) - sum(l);
  }

  int buff_size;
  node* buff;

  int size;
  node* tree;
};

void solve() {
  int N;
  while (~scanf("%d", &N)) {
    edges.clear();
    edges.resize(N);
    rep(i, N) edges[i].reserve(8);

    rep(i, N - 1) {
      int v, w; scanf("%d %d", &v, &w);
      --v; --w;
      edges[v].push_back(w);
      edges[w].push_back(v);
    }

    auto hld = HLD(edges);
    auto tree = FenwickTree(N);

    int Q; scanf("%d", &Q);
    rep(i, Q) {
      int v, w; scanf("%d %d", &v, &w); --v, --w;
      hld.update(v, w, [&](int l, int r, int o, int s) { tree.add(l, r, 1, o, s); });
    }

    auto tri = [](int n) { return i64(n) * (n + 1) / 2; };
    i64 ans = 0;
    rep(i, N) if (hld.tree[i].head < 0) {
      int o = hld.tree[i].id;
      int l = -hld.tree[i].head;
      rep(j, l) ans += tri(tree.sum(j, j + 1, o, l));
    }
    printf("%lld\n", ans);
  }
}

int main() {
  clock_t beg = clock();
  solve();
  clock_t end = clock();
  fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC);
  return 0;
}
0