結果

問題 No.899 γatheree
ユーザー t98slidert98slider
提出日時 2023-05-02 03:24:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 433 ms / 2,000 ms
コード長 2,137 bytes
コンパイル時間 4,414 ms
コンパイル使用メモリ 237,024 KB
実行使用メモリ 14,228 KB
最終ジャッジ日時 2023-08-13 04:52:40
合計ジャッジ時間 13,979 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 422 ms
13,832 KB
testcase_07 AC 433 ms
13,832 KB
testcase_08 AC 432 ms
13,872 KB
testcase_09 AC 417 ms
13,892 KB
testcase_10 AC 413 ms
13,836 KB
testcase_11 AC 423 ms
13,832 KB
testcase_12 AC 419 ms
13,888 KB
testcase_13 AC 410 ms
13,872 KB
testcase_14 AC 418 ms
13,984 KB
testcase_15 AC 413 ms
13,904 KB
testcase_16 AC 429 ms
13,924 KB
testcase_17 AC 425 ms
13,832 KB
testcase_18 AC 425 ms
13,848 KB
testcase_19 AC 414 ms
13,844 KB
testcase_20 AC 418 ms
13,852 KB
testcase_21 AC 408 ms
14,120 KB
testcase_22 AC 404 ms
14,160 KB
testcase_23 AC 407 ms
14,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;

using S = ll;
using F = bool;
S e(){return 0;}
S op(S l, S r){return l + r;}
S mapping(F f, S x){return f ? 0 : x;}
F composition(F f, F g){return f | g;}
F id(){return false;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, u, v, Q;
    cin >> n;
    vector<vector<int>> g(n);
    vector<int> ent(n, -1), par(n, -1), mn(n, n), mx(n, -1), mn2(n, n), mx2(n, -1);
    vector<ll> tmp(n);
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    queue<int> nxt;
    nxt.emplace(0);
    for(int timer = 0; timer < n; timer++){
        v = nxt.front();
        nxt.pop();
        ent[v] = timer;
        int p = par[v];
        if(p != -1){
            mn[p] = min(mn[p], timer);
            mx[p] = timer;
            p = par[p];
            if(p != -1){
                mn2[p] = min(mn2[p], timer);
                mx2[p] = timer;
            }
        }
        for(auto &&u : g[v]){
            if(par[v] == u) continue;
            par[u] = v;
            nxt.emplace(u);
        }
    }
    for(int i = 0; i < n; i++){
        cin >> v;
        tmp[ent[i]] = v;
    }
    atcoder::lazy_segtree<S, op, e, F, mapping, composition, id> seg(tmp);
    auto update = [&](int l, int r){
        if(l == n) return 0ll;
        ll res = seg.prod(l, r);
        seg.apply(l, r, true);
        return res;
    };
    auto f = [&](int v){
        ll ans = 0;
        if(v){
            int p = par[v];
            ans += update(mn[p], mx[p] + 1);
            ans += seg.get(ent[p]);
            seg.set(ent[p], 0);
            if(par[p] != -1){
                ans += seg.get(ent[par[p]]);
                seg.set(ent[par[p]], 0);
            }
        }else{
            ans += seg.get(ent[0]);
            seg.set(ent[0], 0);
        }
        ans += update(mn[v], mx[v] + 1);
        ans += update(mn2[v], mx2[v] + 1);
        seg.set(ent[v], ans);
        return ans;
    };
    cin >> Q;
    while(Q--){
        cin >> v;
        cout << f(v) << '\n';
    }
}
0