結果

問題 No.899 γatheree
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2019-10-05 08:05:08
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 502 ms / 2,000 ms
コード長 5,284 bytes
コンパイル時間 2,517 ms
コンパイル使用メモリ 210,332 KB
実行使用メモリ 15,812 KB
最終ジャッジ日時 2024-04-15 08:06:39
合計ジャッジ時間 12,786 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
11,204 KB
testcase_01 AC 6 ms
11,460 KB
testcase_02 AC 6 ms
10,816 KB
testcase_03 AC 6 ms
10,432 KB
testcase_04 AC 6 ms
10,308 KB
testcase_05 AC 7 ms
10,824 KB
testcase_06 AC 481 ms
15,556 KB
testcase_07 AC 502 ms
15,428 KB
testcase_08 AC 497 ms
15,684 KB
testcase_09 AC 493 ms
15,684 KB
testcase_10 AC 489 ms
15,424 KB
testcase_11 AC 486 ms
15,556 KB
testcase_12 AC 473 ms
15,684 KB
testcase_13 AC 476 ms
15,432 KB
testcase_14 AC 496 ms
15,432 KB
testcase_15 AC 492 ms
15,432 KB
testcase_16 AC 485 ms
15,428 KB
testcase_17 AC 478 ms
15,688 KB
testcase_18 AC 500 ms
15,556 KB
testcase_19 AC 469 ms
15,508 KB
testcase_20 AC 488 ms
15,428 KB
testcase_21 AC 426 ms
15,812 KB
testcase_22 AC 425 ms
15,812 KB
testcase_23 AC 418 ms
15,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
template<class V, int NV> struct LazySegTree { // [L,R)
    vector<V> dat, lazy; LazySegTree() { dat.resize(NV * 2, def); lazy.resize(NV * 2, ldef); }
    void update(int a, int b, V v, int k, int l, int r) { push(k, l, r); if (r <= a || b <= l) return;
        if (a <= l && r <= b) { setLazy(k, v); push(k, l, r); } else {
        update(a, b, v, k * 2 + 1, l, (l + r) / 2); update(a, b, v, k * 2 + 2, (l + r) / 2, r);
        dat[k] = comp(dat[k * 2 + 1], dat[k * 2 + 2]);}}
    V get(int a, int b, int k, int l, int r) { push(k, l, r); if (r <= a || b <= l) return def;
        if (a <= l && r <= b) return dat[k]; auto x = get(a, b, k * 2 + 1, l, (l + r) / 2);
        auto y = get(a, b, k * 2 + 2, (l + r) / 2, r); return comp(x, y);}
    void update(int a, int b, V v) { update(a, b, v, 0, 0, NV); }
    V get(int a, int b) { return get(a, b, 0, 0, NV); }
    // ---- Template ---------------------------------------------------------------------------------
    
    // 区間代入,区間和
    const V def = 0, ldef = -1;
    V comp(V l, V r) { return l + r; }
    void setLazy(int i, V v) { lazy[i] = v; }
    void push(int k, int l, int r) {
        if (lazy[k] != ldef) {
            // modify------------------------------
            dat[k] = lazy[k] * (r - l);
            // ------------------------------------
            if (r - l > 1) { setLazy(k * 2 + 1, lazy[k]); setLazy(k * 2 + 2, lazy[k]); }
            lazy[k] = ldef;
        }
    }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














int N;
vector<int> E[101010];
LazySegTree<ll, 1 << 17> st;
//---------------------------------------------------------------------------------------------------
int P[101010];
int L1[101010], R1[101010], L2[101010], R2[101010];
int toVID[101010];
void bfs_eulertour()
{
    rep(i, 0, N) {
        P[i] = -1;
        L1[i] = L2[i] = inf;
        R1[i] = R2[i] = -1;
    }

    queue<int> que;
    que.push(0);
    toVID[0] = 0;
    int vid = 1;

    while(!que.empty()) {
        int cu = que.front();
        que.pop();

        fore(to, E[cu]) if(P[cu] != to) {
            que.push(to);
            P[to] = cu;
            toVID[to] = vid;
            vid++;

            int vcu = toVID[cu];

            chmin(L1[vcu], toVID[to]);
            chmax(R1[vcu], toVID[to]);

            if (0 <= P[cu]) {
                int vp = toVID[P[cu]];
                chmin(L2[vp], toVID[to]);
                chmax(R2[vp], toVID[to]);
            }
        }
    }
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, N - 1) {
        int a, b; cin >> a >> b;
        E[a].push_back(b);
        E[b].push_back(a);
    }

    bfs_eulertour();

    rep(i, 0, N) {
        int a;
        cin >> a;
        st.update(toVID[i], toVID[i] + 1, a);
    }

    int Q; cin >> Q;
    rep(q, 0, Q) {
        int x;
        cin >> x;

        int vx = toVID[x];

        ll sm = 0;

        // 自分の1つ下の子供
        if (0 <= L1[vx]) {
            sm += st.get(L1[vx], R1[vx] + 1);
            st.update(L1[vx], R1[vx] + 1, 0);
        }
        // 自分の2つ下の子供
        if (0 <= L2[vx]) {
            sm += st.get(L2[vx], R2[vx] + 1);
            st.update(L2[vx], R2[vx] + 1, 0);
        }
        // 自分
        sm += st.get(vx, vx + 1);
        st.update(vx, vx + 1, 0);
        
        if (0 <= P[x]) {
            int p = P[x];
            int vp = toVID[p];

            // 親
            sm += st.get(vp, vp + 1);
            st.update(vp, vp + 1, 0);

            // 親の1つ下の子
            if (0 <= L1[vp]) {
                sm += st.get(L1[vp], R1[vp] + 1);
                st.update(L1[vp], R1[vp] + 1, 0);
            }

            if (0 <= P[p]) {
                int pp = P[p];
                int vpp = toVID[pp];
                
                // 親の親
                sm += st.get(vpp, vpp + 1);
                st.update(vpp, vpp + 1, 0);
            }
        }

        printf("%lld\n", sm);
        st.update(vx, vx + 1, sm);
    }
}



0