/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 9ms 20.438 MiB
#2 Accepted 10ms 20.824 MiB
#3 Accepted 47ms 22.125 MiB
#4 Accepted 50ms 22.395 MiB
#5 Accepted 69ms 22.391 MiB
#6 Accepted 43ms 22.629 MiB
#7 Accepted 594ms 30.195 MiB
#8 Accepted 542ms 30.074 MiB
#9 Accepted 493ms 30.137 MiB
#10 Accepted 519ms 30.309 MiB
#11 Accepted 531ms 30.375 MiB
#12 Accepted 507ms 30.121 MiB
#13 Accepted 520ms 30.195 MiB
#14 Accepted 333ms 37.207 MiB
#15 Accepted 331ms 37.02 MiB
#16 Accepted 323ms 37.41 MiB

Code

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

const int N = 2e5 + 9;
int start[N], en[N];
vector<ll>flat(N + 5, 0);
vector<ll>g[N], lights(N + 5);
struct ST {
    #define lc (n << 1)
    #define rc ((n << 1) | 1)
    ll t[4 * N], lazy[4 * N];
    ST() {
        for (int i = 0; i < 4 * N; i++)
            t[i] = lazy[i] = 0;
    }
    
    inline void push(int n, int st, int en) {
        if (lazy[n] == 0) return;
        if(lazy[n] % 2 != 0){
            t[n] = (en - st + 1) - t[n];
        }
        if (st != en) {
            lazy[lc] += lazy[n];
            lazy[rc] += lazy[n];
        }
        lazy[n] = 0;
    }

    inline void pull(int n) {
        t[n] = t[lc] + t[rc];
    }
    void build(int n, int st, int en, vector<ll> &arr) {
        lazy[n] = 0; // set lazy default value = 0
        if (st == en) {
            t[n] = arr[st];
            return;
        }
        int mid = (st + en) >> 1;
        build(lc, st, mid, arr);
        build(rc, mid + 1, en, arr);
        pull(n);
    }
    void update(int n, int st, int en, int l, int r, ll v) {
        push(n, st, en);  // push the value to left and right child
        if (r < st || en < l) return;
        if (l <= st && en <= r) {
            lazy[n] += v; // set lazy
            push(n, st, en);
            return;
        }
        int mid = (st + en) >> 1;
        update(lc, st, mid, l, r, v);
        update(rc, mid + 1, en, l, r, v);
        pull(n);
    }

    inline ll combine(ll a, ll b) {
        return a + b;
    }
    ll query(int n, int st, int en, int l, int r) {
        push(n, st, en);
        if (l > en || st > r) return 0; // return null
        if (l <= st && en <= r) return t[n];
        int mid = (st + en) >> 1;
        return combine(query(lc, st, mid, l, r), query(rc, mid + 1, en, l, r));
    }
} st;

int tt = 0;
void dfs(int node, int par){
    // cerr << endl;
    // cerr << node << " " << par << " " << tt <<  endl;
    // cerr << tt << endl;
    start[node] = ++tt;
    flat[tt] = lights[node];
    for(auto it : g[node]){
        if(it != par){
            dfs(it, node);
        }
    }
    // cerr << tt << endl;
    en[node] = tt;
}

int32_t main() {
    int n;
    cin >> n ;
    for (int i = 1; i <= n; i++) cin >> lights[i];
   
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    dfs(1, 1);

    st.build(1, 1, n, flat); // Building the Segment tree
    
    int q;
    cin >> q;
    while(q--){
        int type;
        cin >> type;
        if(type == 1){
            int node;
            cin >> node;
            st.update(1, 1, n, start[node], en[node], 1);
        }
        else{
            int node;
            cin >> node;
            // cerr << start[node] << ' ' << en[node] << endl;
            cout << st.query(1, 1, n, start[node], en[node]) << endl;
        }
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1101 Mr. Heart and the Enchanted Lights
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-19 18:32:53
Judged At
2024-11-21 03:45:17
Judged By
Score
100
Total Time
594ms
Peak Memory
37.41 MiB