結果

問題 No.899 γatheree
ユーザー ぷらぷら
提出日時 2022-05-08 11:57:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,825 bytes
コンパイル時間 2,801 ms
コンパイル使用メモリ 219,776 KB
実行使用メモリ 18,800 KB
最終ジャッジ日時 2023-09-22 06:52:23
合計ジャッジ時間 19,832 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 830 ms
18,004 KB
testcase_07 AC 837 ms
18,036 KB
testcase_08 AC 836 ms
17,992 KB
testcase_09 AC 841 ms
18,028 KB
testcase_10 AC 848 ms
18,064 KB
testcase_11 AC 835 ms
18,080 KB
testcase_12 AC 834 ms
18,148 KB
testcase_13 AC 852 ms
18,168 KB
testcase_14 AC 844 ms
18,068 KB
testcase_15 AC 834 ms
18,048 KB
testcase_16 AC 839 ms
18,004 KB
testcase_17 AC 834 ms
18,052 KB
testcase_18 AC 837 ms
18,060 KB
testcase_19 AC 836 ms
18,164 KB
testcase_20 AC 831 ms
18,076 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

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<int>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) {
            int 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) {
            int 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) {
            int 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