結果

問題 No.899 γatheree
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-10-04 22:44:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 748 ms / 2,000 ms
コード長 5,680 bytes
コンパイル時間 1,419 ms
コンパイル使用メモリ 120,412 KB
実行使用メモリ 25,856 KB
最終ジャッジ日時 2024-10-03 08:15:24
合計ジャッジ時間 16,328 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,448 KB
testcase_01 AC 6 ms
8,320 KB
testcase_02 AC 7 ms
8,448 KB
testcase_03 AC 6 ms
8,320 KB
testcase_04 AC 6 ms
8,320 KB
testcase_05 AC 6 ms
8,320 KB
testcase_06 AC 738 ms
25,728 KB
testcase_07 AC 748 ms
25,728 KB
testcase_08 AC 727 ms
25,856 KB
testcase_09 AC 719 ms
25,728 KB
testcase_10 AC 727 ms
25,856 KB
testcase_11 AC 727 ms
25,856 KB
testcase_12 AC 726 ms
25,856 KB
testcase_13 AC 716 ms
25,856 KB
testcase_14 AC 721 ms
25,728 KB
testcase_15 AC 732 ms
25,728 KB
testcase_16 AC 722 ms
25,856 KB
testcase_17 AC 723 ms
25,856 KB
testcase_18 AC 721 ms
25,856 KB
testcase_19 AC 733 ms
25,728 KB
testcase_20 AC 715 ms
25,856 KB
testcase_21 AC 733 ms
25,728 KB
testcase_22 AC 722 ms
25,728 KB
testcase_23 AC 715 ms
25,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC optimize ("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target ("avx")

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> void chmin(T &t, const T &f) { if (t > f) t = f; }
template <class T> void chmax(T &t, const T &f) { if (t < f) t = f; }

template <class T, class S> struct SegmentTree {
  using OpTT = std::function<T(const T &, const T &)>;
  using OpSS = std::function<S(const S &, const S &)>;
  using OpST = std::function<T(const S &, const T &, int)>;
  const OpTT opTT;
  const OpSS opSS;
  const OpST opST;
  const T idT;
  const S idS;

  int n;
  vector<T> ts;
  vector<S> ss;
  SegmentTree(int n_, const OpTT opTT, const OpSS opSS, const OpST opST, const T &idT, const S &idS)
      : opTT(opTT), opSS(opSS), opST(opST), idT(idT), idS(idS) {
    for (n = 1; n < n_; n <<= 1) {}
    ts.assign(n << 1, idT);
    ss.assign(n << 1, idS);
  }
  T &at(int a) {
    return ts[n + a];
  }
  void build() {
    for (int a = n; --a; ) ts[a] = opTT(ts[a << 1], ts[a << 1 | 1]);
  }
  T query(int a, int b, const S &s) {
    return query(1, 0, n, a, b, s);
  }

 private:
  T query(int u, int l, int r, int a, int b, const S &s) {
    if (a < l) a = l;
    if (b > r) b = r;
    if (a >= b) return idT;
    if (a == l && b == r) {
      ts[u] = opST(s, ts[u], r - l);
      ss[u] = opSS(s, ss[u]);
      return ts[u];
    }
    const int uL = u << 1, uR = u << 1 | 1;
    const int mid = (l + r) >> 1;
    {
      ts[uL] = opST(ss[u], ts[uL], mid - l);
      ts[uR] = opST(ss[u], ts[uR], r - mid);
      ss[uL] = opSS(ss[u], ss[uL]);
      ss[uR] = opSS(ss[u], ss[uR]);
      ss[u] = idS;
    }
    const T resL = query(uL, l, mid, a, b, s);
    const T resR = query(uR, mid, r, a, b, s);
    ts[u] = opTT(ts[uL], ts[uR]);
    return opTT(resL, resR);
  }
};

struct S {
  bool f;
  Int x;
};
S opSS(const S &sa, const S &sb) {
  return sa.f ? sa : sb;
}
Int opST(const S &s, const Int &t, int sz) {
  return s.f ? (s.x * sz) : t;
}
constexpr S IdS{false, 0};


constexpr int MAX = 200010;

int N;
int U[MAX], V[MAX];
Int A[MAX];
int Q;
int X[MAX];

vector<int> G[MAX];
int par[MAX], dep[MAX];
int zeit, dis[MAX], fin[MAX];

pair<pair<int, int>, int> ps[MAX];
int ord[MAX];
pair<int, int> ls[MAX], rs[MAX];
pair<int, int> lls[MAX], rrs[MAX];

void dfs(int u, int p) {
  par[u] = p;
  dep[u] = (p == -1) ? 0 : (dep[p] + 1);
  dis[u] = zeit++;
  for (const int i : G[u]) {
    const int v = U[i] ^ V[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
  fin[u] = zeit++;
}

int main() {
  for (; ~scanf("%d", &N); ) {
    for (int i = 0; i < N - 1; ++i) {
      scanf("%d%d", &U[i], &V[i]);
    }
    for (int u = 0; u < N; ++u) {
      scanf("%lld", &A[u]);
    }
    scanf("%d", &Q);
    for (int q = 0; q < Q; ++q) {
      scanf("%d", &X[q]);
    }
    
    for (int u = 0; u < N; ++u) {
      G[u].clear();
    }
    for (int i = 0; i < N - 1; ++i) {
      G[U[i]].push_back(i);
      G[V[i]].push_back(i);
    }
    zeit = 0;
    dfs(0, -1);
// cerr<<"dis = ";pv(dis,dis+N);
// cerr<<"fin = ";pv(fin,fin+N);
    for (int u = 0; u < N; ++u) {
      ps[u] = make_pair(make_pair(dep[u], dis[u]), u);
    }
    sort(ps, ps + N);
    for (int j = 0; j < N; ++j) {
      ord[ps[j].second] = j;
    }
    fill(ls, ls + N, make_pair(N, -1));
    fill(rs, rs + N, make_pair(-1, -1));
    fill(lls, lls + N, make_pair(N, -1));
    fill(rrs, rrs + N, make_pair(-1, -1));
    for (int u = 0; u < N; ++u) {
      if (par[u] != -1) {
        chmin(ls[par[u]], make_pair(ord[u], u));
        chmax(rs[par[u]], make_pair(ord[u], u));
        if (par[par[u]] != -1) {
          chmin(lls[par[par[u]]], make_pair(ord[u], u));
          chmax(rrs[par[par[u]]], make_pair(ord[u], u));
        }
      }
    }
// cerr<<"ps = ";pv(ps,ps+N);
// cerr<<"ls = ";pv(ls,ls+N);
// cerr<<"rs = ";pv(rs,rs+N);
// cerr<<"lls = ";pv(lls,lls+N);
// cerr<<"rrs = ";pv(rrs,rrs+N);
    
    SegmentTree<Int, S> seg(N, plus<Int>(), opSS, opST, 0, IdS);
    for (int u = 0; u < N; ++u) {
      seg.at(ord[u]) = A[u];
    }
    seg.build();
    for (int q = 0; q < Q; ++q) {
      const int u = X[q];
      vector<pair<int, int>> intervals;
      if (par[u] != -1) {
        if (par[par[u]] != -1) {
          intervals.emplace_back(ord[par[par[u]]], ord[par[par[u]]]);
        }
        intervals.emplace_back(ord[par[u]], ord[par[u]]);
        intervals.emplace_back(ls[par[u]].first, rs[par[u]].first);
      } else {
        intervals.emplace_back(ord[u], ord[u]);
      }
      if (ls[u].second != -1) {
        intervals.emplace_back(ls[u].first, rs[u].first);
        if (lls[u].second != -1) {
          intervals.emplace_back(lls[u].first, rrs[u].first);
        }
      }
      Int ans = 0;
      for (const auto &interval : intervals) {
        ans += seg.query(interval.first, interval.second + 1, IdS);
        seg.query(interval.first, interval.second + 1, S{true, 0});
      }
      seg.query(ord[u], ord[u] + 1, S{true, ans});
      printf("%lld\n", ans);
    }
  }
  return 0;
}
0