結果

問題 No.899 γatheree
ユーザー kcvlexkcvlex
提出日時 2019-10-10 21:00:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,419 bytes
コンパイル時間 2,441 ms
コンパイル使用メモリ 207,176 KB
実行使用メモリ 21,624 KB
最終ジャッジ日時 2024-11-21 22:06:40
合計ジャッジ時間 17,116 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define DEBUGGING
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
using ll = int64_t;
using ull = uint64_t;
using PLL = pair<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return max(low, min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { t = clamp(t, low, high); }

namespace init__ {

struct InitIO {
    InitIO() {
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

}

#ifdef DEBUGGING
// #include "../debug/debug.cpp"
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

template <typename T>
T make_v(T init) { return init; }

template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init, tail...)
    return V<decltype(rec)>(s, rec);
#undef rec
}

template <typename T, typename L>
class LazySegmentTree {
private:
    using Merge = function<T(T, T)>;
    using Apply = function<T(T, L)>;
    using Update = function<L(L, L)>;
    using CalcLazyValue = function<L(ssize_t, ssize_t, L)>;
    using Prop = function<L(L)>;

    size_t arr_size;
    T init_node;
    L init_lazy;
    vector<T> node;
    vector<L> lazy;
    vector<bool> lazy_flag;
    Merge merge_node;
    Apply apply_lazy_value;
    Update update_lazy_value;
    CalcLazyValue calc_lazy_value;
    Prop prop_lazy_value;
    
public:
    LazySegmentTree(vector<T> v, 
                    T init_node,
                    L init_lazy, 
                    Merge merge_node,
                    Apply apply_lazy_value,
                    Update update_lazy_value,
                    CalcLazyValue calc_lazy_value,
                    Prop prop_lazy_value = [](L l) { return l; })
        : init_node(init_node),
          init_lazy(init_lazy),
          merge_node(merge_node),
          apply_lazy_value(apply_lazy_value),
          update_lazy_value(update_lazy_value),
          calc_lazy_value(calc_lazy_value),
          prop_lazy_value(prop_lazy_value)
    {
        {
            arr_size = 1;
            while(arr_size < v.size()) arr_size *= 2;
        }
        node.resize(2 * arr_size - 1, init_node);
        lazy.resize(2 * arr_size - 1, init_lazy);
        lazy_flag.resize(2 * arr_size - 1, false);
        for(ll i = 0; i < v.size(); i++) node[i + arr_size - 1] = v[i];
        for(ll i = arr_size - 2; 0 <= i; i--) node[i] = merge_node(node[i * 2 + 1], node[i * 2 + 2]);
    }

    void lazy_eval(ll pos, ll left, ll right) {
        if(!lazy_flag[pos]) return;

        node[pos] = apply_lazy_value(node[pos], lazy[pos]);
        lazy_flag[pos] = false;

        if(right - left > 1) {
            for(ll idx = 2 * pos + 1; idx <= 2 * pos + 2; idx++) {
                DEBUG(make_tuple(lazy[idx], prop_lazy_value(lazy[pos]), update_lazy_value(lazy[idx], prop_lazy_value(lazy[pos]))));
                lazy[idx] = update_lazy_value(lazy[idx], prop_lazy_value(lazy[pos]));
                lazy_flag[idx] = true;
            }
        }

        lazy[pos] = init_lazy;
    }

    void update_query(ll left, ll right, L val, ll pos = 0, ll node_left = 0, ll node_right = -1) {
        if(node_right < 0) node_right = arr_size;

        lazy_eval(pos, node_left, node_right);

        if(right <= node_left || node_right <= left) return;
        
        if(left <= node_left && node_right <= right) {
            lazy[pos] = calc_lazy_value(node_left, node_right, val);
            DEBUG(make_tuple(left, right, node_left, node_right, lazy[pos]));
            lazy_flag[pos] = true;
            lazy_eval(pos, node_left, node_right);
        } else {
            ll mid = (node_left + node_right) / 2;
            update_query(left, right, val, 2 * pos + 1, node_left, mid);
            update_query(left, right, val, 2 * pos + 2, mid, node_right);
            node[pos] = merge_node(node[2 * pos + 1], node[2 * pos + 2]);
        }
    }

    T get_query(ll left, ll right, ll pos = 0, ll node_left = 0, ll node_right = -1) {
        if(node_right < 0) node_right = arr_size;
        
        lazy_eval(pos, node_left, node_right);

        if(node_right <= left || right <= node_left) return init_node;
        if(left <= node_left && node_right <= right) return node[pos];

        ll mid = (node_left + node_right) / 2;
        return merge_node(get_query(left, right, 2 * pos + 1, node_left, mid),
                          get_query(left, right, 2 * pos + 2, mid, node_right));
    }
};

// cur, par, par-par
using TLL = tuple<ll, ll, ll>;

const ll inf = 5e15;

int main() {
    ll N;
    cin >> N;
    VV<ll> edges(N);
    for(ll i = 1; i < N; i++) {
        ll u, v;
        cin >> u >> v;
        edges[u].emplace_back(v);
        edges[v].emplace_back(u);
    }
    for(auto &&v : edges) sort(ALL(v));
    V<ll> A(N);
    for(auto &&ele : A) cin >> ele;

    V<PLL> one(N, PLL(inf, -inf)), two(N, PLL(inf, -inf));
    V<ll> nodes(N), rnodes(N);
    V<PLL> parents(N, PLL(-1, -1));

    {
        auto update_pll = [&](PLL &p, ll v) {
            ll a, b;
            tie(a, b) = p;
            chmin(a, v);
            chmax(b, v);
            p = PLL(a, b);
        };

        V<bool> used(N);
        queue<TLL> que;
        que.emplace(0, -1, -1);
        used[0] = true;
        ll node_idx = 0;
        while(que.size()) {
            ll cur, par1, par2;
            tie(cur, par1, par2) = que.front();
            DEBUG(que.front());
            que.pop();
            nodes[node_idx] = cur;
            rnodes[cur] = node_idx;
            if(par1 != -1) {
                update_pll(one[par1], node_idx);
                parents[cur].first = par1;
            }
            if(par2 != -1) {
                update_pll(two[par2], node_idx);
                parents[cur].second = par2;
            }
            for(ll nxt : edges[cur]) {
                if(used[nxt]) continue;
                used[nxt] = true;
                que.emplace(nxt, cur, par1);
            }
            node_idx++;
        }
    }

    {
        V<ll> tmp = move(A);
        A.resize(N);
        for(ll i = 0; i < N; i++) A[i] = tmp[nodes[i]];
        A = move(tmp);
    }

    DEBUG(nodes, rnodes, A);

    LazySegmentTree<ll, ll> lst(A, 0, 0,
                                [&](ll a, ll b) { return a + b; },
                                [&](ll a, ll b) { return b; },
                                [&](ll a, ll b) { return b; },
                                [&](ll l, ll r, ll a) { return a; });

    ll Q;
    cin >> Q;
    while(Q--) {
        ll x;
        cin >> x;

        ll cur = lst.get_query(rnodes[x], rnodes[x] + 1);
        lst.update_query(rnodes[x], rnodes[x] + 1, 0);
        DEBUG(rnodes[x]);
        DEBUG(lst.get_query(rnodes[x], rnodes[x] + 1));
        PLL onep = one[x], twop = two[x];
        ll par1, par2;
        tie(par1, par2) = parents[x];

        auto update = [&](const PLL &range) {
            ll l, r;
            tie(l, r) = range;
            if(l < 0 || r < 0) return;
            DEBUG(range);
            l = rnodes[l];
            r = rnodes[r] + 1;
            ll sum = lst.get_query(l, r);
            DEBUG(sum);
            cur += sum;
            lst.update_query(l, r, 0);
        };

        PLL ranges[] = { onep, twop, PLL(par1, par1), PLL(par2, par2) };
        // DEBUG(make_tuple(ranges[0], ranges[1], ranges[2], ranges[3]));
        for(ll i = 0; i < 4; i++) update(ranges[i]);
        if(edges[x][0] == 0) update(one[0]);

        lst.update_query(rnodes[x], rnodes[x] + 1, cur);
        cout << cur << endl;
    }

    return 0;
}
0