結果

問題 No.899 γatheree
ユーザー koi_kotyakoi_kotya
提出日時 2019-10-06 16:16:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 861 ms / 2,000 ms
コード長 4,094 bytes
コンパイル時間 2,575 ms
コンパイル使用メモリ 186,384 KB
実行使用メモリ 16,092 KB
最終ジャッジ日時 2024-04-18 11:50:36
合計ジャッジ時間 17,778 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 756 ms
15,832 KB
testcase_07 AC 742 ms
15,712 KB
testcase_08 AC 861 ms
15,708 KB
testcase_09 AC 734 ms
15,840 KB
testcase_10 AC 741 ms
15,712 KB
testcase_11 AC 750 ms
15,712 KB
testcase_12 AC 722 ms
15,712 KB
testcase_13 AC 735 ms
15,708 KB
testcase_14 AC 753 ms
15,708 KB
testcase_15 AC 727 ms
15,712 KB
testcase_16 AC 758 ms
15,708 KB
testcase_17 AC 734 ms
15,836 KB
testcase_18 AC 733 ms
15,708 KB
testcase_19 AC 726 ms
15,836 KB
testcase_20 AC 702 ms
15,708 KB
testcase_21 AC 745 ms
16,088 KB
testcase_22 AC 747 ms
16,092 KB
testcase_23 AC 737 ms
15,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

struct LazySegmentTree {
private:
    int n;
    vector<ll> node,lazy;
    vector<bool> upd;
public:
    LazySegmentTree(vector<ll> v) {
        int sz = v.size();
        n = 1;
        while (n < sz) n *= 2;
        node.resize(2*n-1);
        lazy.resize(2*n-1,0);
        upd.resize(2*n-1,false);
        for (int i = 0;i < sz;++i) node[i+n-1] = v[i];
        for (int i = n-2;i >= 0;--i) node[i] = node[i*2+1]+node[i*2+2];
    }

    void eval(int k,int l,int r) {
        if (upd[k]) {
            node[k] = lazy[k]*(r-l);
            if (r-l > 1) {
                lazy[2*k+1] = lazy[2*k+2] = lazy[k];
                upd[2*k+1] = upd[2*k+2] = true;
            }
            lazy[k] = 0;
            upd[k] = false;
        }
    }

    void update(int a,int b,ll x,int k = 0,int l = 0,int r = -1) {
        if (r < 0) r = n;
        eval(k,l,r);
        if (b <= l || r <= a) return;
        if (a <= l && r <= b) {
            lazy[k] = x;
            upd[k] = true;
            eval(k,l,r);
        } else {
            update(a,b,x,2*k+1,l,(l+r)/2);
            update(a,b,x,2*k+2,(l+r)/2,r);
            node[k] = node[2*k+1]+node[2*k+2];
        }
    }

    ll getsum(int a,int b,int k = 0,int l = 0,int r = -1) {
        if (r < 0) r = n;
        if (b <= l || r <= a) return 0;
        eval(k,l,r);
        if (a <= l && r <= b) return node[k];
        ll vl = getsum(a,b,k*2+1,l,(l+r)/2);
        ll vr = getsum(a,b,2*k+2,(l+r)/2,r);
        return vl+vr;
    }
};

int main() {
    int n;
    cin >> n;
    vector<vector<int>> edges(n);
    vector<int> a(n),idx(n),par(n);
    deque<int> que;
    vector<bool> used(n,false);
    vector<P> child(n,P(-1,-1)),child2(n,P(-1,-1));;
    LazySegmentTree seg(vector<ll>(n,0));
    for (int i = 0;i < n-1;++i) {
        int u,v;
        cin >> u >> v;
        edges[u].push_back(v);
        edges[v].push_back(u);
    }
    for (int i = 0;i < n;++i) cin >> a[i];
    que.push_back(0);
    idx[0] = 0;
    par[0] = -1;
    int c = 0;
    while (!que.empty()) {
        int t = que.front();
        que.pop_front();
        used[t] = true;
        int s = idx[t];
        for (int& i : edges[t]) if (!used[i]) {
            c++;
            idx[i] = c;
            par[c] = s;
            if (child[s].first < 0) child[s] = P(c,c);
            child[s].second = c;
            if (par[s] >= 0) {
                if (child2[par[s]].first < 0) child2[par[s]] = P(c,c);
                child2[par[s]].second = c;
            }   
            que.push_back(i);
        }
    }
    for (int i = 0;i < n;++i) seg.update(idx[i],idx[i]+1,a[i]);
    int q;
    cin >> q;
    for (int z = 0;z < q;++z) {
        int x;
        cin >> x;
        int y = idx[x];
        ll sum = seg.getsum(y,y+1);
        seg.update(y,y+1,0);
        if (par[y] >= 0) {
            sum += seg.getsum(par[y],par[y]+1);
            sum += seg.getsum(child[par[y]].first,child[par[y]].second+1);
            seg.update(par[y],par[y]+1,0);
            seg.update(child[par[y]].first,child[par[y]].second+1,0);
            if (par[par[y]] >= 0) {
                int k = par[par[y]];
                sum += seg.getsum(k,k+1);
                seg.update(k,k+1,0);
            }
        }
        if (child[y].first >= 0) {
            sum += seg.getsum(child[y].first,child[y].second+1);
            seg.update(child[y].first,child[y].second+1,0);
            if (child2[y].first >= 0) {
                sum += seg.getsum(child2[y].first,child2[y].second+1);
                seg.update(child2[y].first,child2[y].second+1,0);
            }
        }
        seg.update(y,y+1,sum);
        cout << sum << endl;
    }
}
0