結果

問題 No.899 γatheree
ユーザー 👑 jupirojupiro
提出日時 2020-03-06 14:53:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 971 ms / 2,000 ms
コード長 7,273 bytes
コンパイル時間 1,550 ms
コンパイル使用メモリ 131,216 KB
実行使用メモリ 18,048 KB
最終ジャッジ日時 2024-04-22 03:54:47
合計ジャッジ時間 17,987 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,144 KB
testcase_01 AC 4 ms
6,272 KB
testcase_02 AC 4 ms
6,144 KB
testcase_03 AC 4 ms
6,144 KB
testcase_04 AC 4 ms
6,144 KB
testcase_05 AC 4 ms
6,144 KB
testcase_06 AC 738 ms
17,480 KB
testcase_07 AC 762 ms
17,664 KB
testcase_08 AC 722 ms
17,664 KB
testcase_09 AC 703 ms
17,664 KB
testcase_10 AC 727 ms
17,664 KB
testcase_11 AC 719 ms
17,536 KB
testcase_12 AC 723 ms
17,500 KB
testcase_13 AC 736 ms
17,664 KB
testcase_14 AC 733 ms
17,448 KB
testcase_15 AC 721 ms
17,544 KB
testcase_16 AC 756 ms
17,640 KB
testcase_17 AC 782 ms
17,652 KB
testcase_18 AC 774 ms
17,664 KB
testcase_19 AC 851 ms
17,632 KB
testcase_20 AC 853 ms
17,536 KB
testcase_21 AC 948 ms
17,792 KB
testcase_22 AC 900 ms
17,920 KB
testcase_23 AC 971 ms
18,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;



template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree {
    using F = function< Monoid(Monoid, Monoid) >;
    using G = function< Monoid(Monoid, OperatorMonoid) >;
    using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;

    int sz, height;
    vector< Monoid > data;
    vector< OperatorMonoid > lazy;
    const F f;
    const G g;
    const H h;
    const Monoid M1;
    const OperatorMonoid OM0;


    LazySegmentTree(int n, const F f, const G g, const H h,
        const Monoid& M1, const OperatorMonoid OM0)
        : f(f), g(g), h(h), M1(M1), OM0(OM0) {
        sz = 1;
        height = 0;
        while (sz < n) sz <<= 1, height++;
        data.assign(2 * sz, M1);
        lazy.assign(2 * sz, OM0);
    }

    void set(int k, const Monoid& x) {
        data[k + sz] = x;
    }

    void build() {
        for (int k = sz - 1; k > 0; k--) {
            data[k] = f(data[2 * k + 0], data[2 * k + 1]);
        }
    }

    inline void propagate(int k) {
        if (lazy[k] != OM0) {
            lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
            lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
            data[k] = reflect(k);
            lazy[k] = OM0;
        }
    }

    inline Monoid reflect(int k) {
        return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
    }

    inline void recalc(int k) {
        while (k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
    }

    inline void thrust(int k) {
        for (int i = height; i > 0; i--) propagate(k >> i);
    }

    void update(int a, int b, const OperatorMonoid& x) {
        thrust(a += sz);
        thrust(b += sz - 1);
        for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) lazy[l] = h(lazy[l], x), ++l;
            if (r & 1) --r, lazy[r] = h(lazy[r], x);
        }
        recalc(a);
        recalc(b);
    }

    Monoid query(int a, int b) {
        thrust(a += sz);
        thrust(b += sz - 1);
        Monoid L = M1, R = M1;
        for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) L = f(L, reflect(l++));
            if (r & 1) R = f(reflect(--r), R);
        }
        return f(L, R);
    }

    Monoid operator[](const int& k) {
        return query(k, k + 1);
    }

    template< typename C >
    int find_subtree(int a, const C& check, Monoid& M, bool type) {
        while (a < sz) {
            propagate(a);
            Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
            if (check(nxt)) a = 2 * a + type;
            else M = nxt, a = 2 * a + 1 - type;
        }
        return a - sz;
    }

    template< typename C >
    int find_first(int a, const C& check) {
        Monoid L = M1;
        if (a <= 0) {
            if (check(f(L, reflect(1)))) return find_subtree(1, check, L, false);
            return -1;
        }
        thrust(a + sz);
        int b = sz;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1) {
                Monoid nxt = f(L, reflect(a));
                if (check(nxt)) return find_subtree(a, check, L, false);
                L = nxt;
                ++a;
            }
        }
        return -1;
    }


    template< typename C >
    int find_last(int b, const C& check) {
        Monoid R = M1;
        if (b >= sz) {
            if (check(f(reflect(1), R))) return find_subtree(1, check, R, true);
            return -1;
        }
        thrust(b + sz - 1);
        int a = sz;
        for (b += sz; a < b; a >>= 1, b >>= 1) {
            if (b & 1) {
                Monoid nxt = f(reflect(--b), R);
                if (check(nxt)) return find_subtree(b, check, R, true);
                R = nxt;
            }
        }
        return -1;
    }
};



pair<ll, ll> f1(pair<ll, ll> a, pair<ll, ll> b) {
    return { a.first + b.first, a.second + b.second };
}

pair<ll, ll> f2(pair<ll, ll> a, ll b) {
    if (b == INF) return a;
    else return { b * a.second, a.second };
}
ll f3(ll a, ll b) {
    if (b == INF) return a;
    else return b;
}
vector<int> g[100005];
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
    int n; scanf("%d", &n);
    for (int i = 0; i < n - 1; i++) {
        int u, v; scanf("%d %d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<int> idx(n, -1), par(n, -1);
    vector<int> L1(n, -1), R1(n, -1), L2(n, -1), R2(n, -1);
    queue<ll> q; q.push(0);
    idx[0] = 0;
    int now = 1;
    while (!q.empty()) {
        int cur = q.front();
        q.pop();
        for (auto next : g[cur]) {
            if (idx[next] != -1) continue;
            idx[next] = now++;
            par[next] = cur;
            if (L1[cur] == -1) L1[cur] = idx[next];
            R1[cur] = idx[next];
            int p = par[cur];
            if (p != -1) {
                if (L2[p] == -1) L2[p] = idx[next];
                R2[p] = idx[next];
            }
            q.push(next);
        }
    }

    LazySegmentTree<pair<ll, ll>, ll> lsg(n, f1, f2, f3, { 0,0 }, INF);
    for (int i = 0; i < n; i++) {
        int a; scanf("%d", &a);
        lsg.set(idx[i], { a,1 });
    }
    lsg.build();

    int Q; scanf("%d", &Q);
    while (Q--) {
        int x; scanf("%d", &x);
        ll res = 0;
        res += lsg.query(idx[x], idx[x] + 1).first;
        lsg.update(idx[x], idx[x] + 1, 0);
        if (L1[x] != -1) {
            res += lsg.query(L1[x], R1[x] + 1).first;
            lsg.update(L1[x], R1[x] + 1, 0);
        }
        if (L2[x] != -1) {
            res += lsg.query(L2[x], R2[x] + 1).first;
            lsg.update(L2[x], R2[x] + 1, 0);
        }
        
        if (par[x] != -1) {
            int p = idx[par[x]];
            res += lsg.query(p, p + 1).first;
            lsg.update(p, p + 1, 0);
            if (par[par[x]] != -1) {
                int pp = idx[par[par[x]]];
                res += lsg.query(pp, pp + 1).first;
                lsg.update(pp, pp + 1, 0);
            }
            if (L1[par[x]] != -1) {
                res += lsg.query(L1[par[x]], R1[par[x]] + 1).first;
                lsg.update(L1[par[x]], R1[par[x]] + 1, 0);
            }
        }
        
        printf("%lld\n", res);
        lsg.update(idx[x], idx[x] + 1, res);
    }
	return 0;
	/*
		おまじないを使ったらscanfとprintf関連注意!!!!!!!!!!!!
	*/
}
0