結果

問題 No.899 γatheree
ユーザー ぷらぷら
提出日時 2022-05-08 11:57:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 866 ms / 2,000 ms
コード長 3,849 bytes
コンパイル時間 2,855 ms
コンパイル使用メモリ 220,880 KB
実行使用メモリ 20,576 KB
最終ジャッジ日時 2023-09-22 06:53:40
合計ジャッジ時間 20,107 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 865 ms
20,096 KB
testcase_07 AC 851 ms
20,140 KB
testcase_08 AC 840 ms
20,216 KB
testcase_09 AC 848 ms
20,096 KB
testcase_10 AC 866 ms
20,100 KB
testcase_11 AC 848 ms
20,220 KB
testcase_12 AC 858 ms
20,196 KB
testcase_13 AC 850 ms
20,096 KB
testcase_14 AC 854 ms
20,272 KB
testcase_15 AC 846 ms
20,152 KB
testcase_16 AC 854 ms
20,204 KB
testcase_17 AC 856 ms
20,164 KB
testcase_18 AC 857 ms
20,212 KB
testcase_19 AC 845 ms
20,272 KB
testcase_20 AC 846 ms
20,104 KB
testcase_21 AC 736 ms
20,488 KB
testcase_22 AC 749 ms
20,532 KB
testcase_23 AC 749 ms
20,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
struct LazySegmentTree {
    int n;
    vector<T> dat;
    vector<T>lazy;
    LazySegmentTree() {};
    LazySegmentTree(int N) {
        n = 1;
        while(n < N) {
            n *= 2;
        }
        dat.resize(n*2,0);
        lazy.resize(n*2,-1);
    }
    void eval(int k,int l,int r) {
        if(lazy[k] == -1) return;
        if(k < n) {
            lazy[k*2] = lazy[k];
            lazy[k*2+1] = lazy[k];
        }
        dat[k] = lazy[k]*(r-l);
        lazy[k] = -1;
    }
    void update(int a, int b, T x, int k, int l, int r) {
        eval(k,l,r);
        if(a <= l && r <= b) {
            lazy[k] = x;
            eval(k,l,r);
        }
        else if(a < r && l < b) {
            update(a, b, x, k*2, l, (l+r)/2);
            update(a, b, x, k*2+1, (l+r)/2, r);
            dat[k] = dat[k*2]+dat[k*2+1];
        }
    }
    void update(int a, int b, T x) {//[a,b)
        update(a, b, x, 1, 0, n);
    }
    T query_sub(int a, int b, int k, int l, int r) {
        eval(k,l,r);
        if (r <= a || b <= l) {
            return 0;
        }
        else if (a <= l && r <= b) {
            return dat[k];
        }
        else {
            T vl = query_sub(a, b, k*2, l, (l+r)/2);
            T vr = query_sub(a, b, k*2+1, (l+r)/2, r);
            return vl+vr;
        }
    }
    T query(int a, int b) {//[a,b)
        return query_sub(a, b, 1, 0, n);
    }
};

int main() {
    int N;
    cin >> N;
    vector<vector<int>>ki(N);
    for(int i = 0; i < N-1; i++) {
        int u,v;
        cin >> u >> v;
        ki[u].push_back(v);
        ki[v].push_back(u);
    }
    vector<int>dist(N);
    vector<vector<int>>c(N);
    vector<int>id,id2(N);
    vector<int>p(N,-1);
    queue<int>que;
    que.push(0);
    id.push_back(0);
    while (!que.empty()) {
        int x = que.front();
        que.pop();
        for(int i:ki[x]) {
            if(i && !dist[i]) {
                dist[i] = dist[x]+1;
                id2[i] = id.size();
                id.push_back(i);
                p[i] = x;
                c[x].push_back(i);
                que.push(i);
            }
        }
    }
    vector<int>A(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    LazySegmentTree<long long>Seg(N);
    vector<int>l1(N,1001001001),r1(N),l2(N,1001001001),r2(N);
    for(int i = N-1; i >= 0; i--) {
        for(int j:c[id[i]]) {
            l1[id[i]] = min(l1[id[i]],id2[j]);
            r1[id[i]] = max(r1[id[i]],id2[j]);
            l2[id[i]] = min(l2[id[i]],l1[j]);
            r2[id[i]] = max(r2[id[i]],r1[j]);
        }
        Seg.update(i,i+1,A[id[i]]);
    }
    int Q;
    cin >> Q;
    while (Q--) {
        int x;
        cin >> x;
        if(l2[x] != 1001001001) {
            long long s = Seg.query(l2[x],r2[x]+1);
            Seg.update(l2[x],r2[x]+1,0);
            Seg.update(id2[x],id2[x]+1,Seg.query(id2[x],id2[x]+1)+s);
        }
        if(l1[x] != 1001001001) {
            long long s = Seg.query(l1[x],r1[x]+1);
            Seg.update(l1[x],r1[x]+1,0);
            Seg.update(id2[x],id2[x]+1,Seg.query(id2[x],id2[x]+1)+s);
        }
        if(p[x] != -1) {
            long long s = Seg.query(l1[p[x]],r1[p[x]]+1);
            Seg.update(l1[p[x]],r1[p[x]]+1,0);
            Seg.update(id2[x],id2[x]+1,Seg.query(id2[x],id2[x]+1)+s);
            
            s = Seg.query(id2[p[x]],id2[p[x]]+1);
            Seg.update(id2[p[x]],id2[p[x]]+1,0);
            Seg.update(id2[x],id2[x]+1,Seg.query(id2[x],id2[x]+1)+s);
            if(p[p[x]] != -1) {
                s = Seg.query(id2[p[p[x]]],id2[p[p[x]]]+1);
                Seg.update(id2[p[p[x]]],id2[p[p[x]]]+1,0);
                Seg.update(id2[x],id2[x]+1,Seg.query(id2[x],id2[x]+1)+s);
            }
        }
        cout << Seg.query(id2[x],id2[x]+1) << endl;
    }
}
0