結果

問題 No.399 動的な領主
ユーザー Min_25
提出日時 2016-07-21 01:58:11
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 5,194 bytes
コンパイル時間 1,439 ms
コンパイル使用メモリ 99,972 KB
最終ジャッジ日時 2025-03-29 15:51:46
合計ジャッジ時間 3,047 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function ‘void solve()’:
main.cpp:185:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  185 |       int v, w; scanf("%d %d", &v, &w);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:194:17: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  194 |     int Q; scanf("%d", &Q);
      |            ~~~~~^~~~~~~~~~
main.cpp:196:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  196 |       int v, w; scanf("%d %d", &v, &w); --v, --w;
      |                 ~~~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/string:43,
                 from /usr/include/c++/13/bits/locale_classes.h:40,
                 from /usr/include/c++/13/bits/ios_base.h:41,
                 from /usr/include/c++/13/ios:44,
                 from /usr/include/c++/13/ostream:40,
                 from /usr/include/c++/13/iostream:41,
                 from main.cpp:10:
/usr/include/c++/13/bits/allocator.h: In destructor ‘std::_Vector_base<HLD::node, std::allocator<HLD::node> >::_Vector_impl::~_Vector_impl()’:
/usr/include/c++/13/bits/allocator.h:184:7: error: inlining failed in call to ‘always_inline’ ‘std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = HLD::node]’: target specific option mismatch
  184 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/13/vector:66,
                 from main.cpp:12:
/usr/include/c++/13/bits/stl_vector.h:133:14: note: called from here
  133 |       struct _Vector_impl
      |              ^~~~~~~~~~~~

ソースコード

diff #

#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), heavy(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;
    }
    heavy[v] = 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; w >= 0; w = heavy[w], ++len) {
        tree[w].id = id++; tree[w].head = v;
        for (auto x : edges[w]) if (x != heavy[w] && 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;
  vector<int> heavy;
};
Edges edges;

struct SegmentTree {
  struct node {
    using result_t = i64;
    node() : sum(0), lazy(0) {}
    node(i64 s, int l) : sum(s), lazy(l) {}
    result_t result() const { return sum; }
    static node merge(const node& lhs, const node& rhs, int b) {
      i64 sum = (lhs.sum + rhs.sum) + i64(b) * (lhs.lazy + rhs.lazy);
      return node(sum, 0);
    }
    i64 sum;
    int lazy;
  };
  
  SegmentTree(int N) : buff_size(N) { buff = new node[2 * N]; }
  ~SegmentTree() { delete [] buff; }

  void propagate(int k) {
    int ks[30], ki = 0, b = 1;
    for (k >>= 1; k >= 1; k >>= 1, b <<= 1) ks[ki++] = k;
    for (; ki; b >>= 1) {
      int k = ks[--ki];
      int l = tree[k].lazy;
      if (l) {
        tree[2 * k + 0].lazy += l;
        tree[2 * k + 1].lazy += l;
        tree[k].sum += i64(l) * b;
        tree[k].lazy = 0;
      }
    }
  }

  void fix(int k, int b) {
    tree[k] = node::merge(tree[2 * k + 0], tree[2 * k + 1], b >> 1);
  }

  void update(int l, int r, int offset=0, int N=0) {
    size = (N ? N : buff_size); tree = buff + offset * 2;

    l += size; r += size;
    propagate(l); propagate(r - 1);

    int b = 1;
    bool lup = false, rup = false;
    for (; l < r; l >>= 1, r >>= 1, b <<= 1) {
      if (lup) fix(l - 1, b);
      if (rup) fix(r, b);
      if (l & 1) tree[l++].lazy += 1, lup = true;
      if (r & 1) tree[--r].lazy += 1, rup = true;
    }
    for (--l; l < r; l >>= 1, r >>= 1, b <<= 1)  {
      if (lup) fix(l, b);
      if (rup) fix(r, b);
    }
    for (; l; l >>= 1, b <<= 1) fix(l, b);
  }

  node::result_t query(int l, int r, int offset=0, int N=0) {
    size = (N ? N : buff_size); tree = buff + offset * 2;

    l += size; r += size;
    propagate(l); propagate(r - 1);

    node left, right;
    int b = 1;
    for (; l < r; l >>= 1, r >>= 1, b <<= 1) {
      if (l & 1) left = node::merge(left, tree[l++], b);
      if (r & 1) right = node::merge(tree[--r], right, b);
    }
    left = node::merge(left, right, b);
    return left.result();
  }

  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 = SegmentTree(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.update(l, r, 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.query(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