/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 14ms 21.859 MiB
#2 Accepted 14ms 21.945 MiB
#3 Accepted 59ms 23.508 MiB
#4 Accepted 44ms 23.453 MiB
#5 Accepted 37ms 23.473 MiB
#6 Accepted 38ms 23.523 MiB
#7 Accepted 447ms 33.023 MiB
#8 Accepted 423ms 33.156 MiB
#9 Accepted 407ms 33.074 MiB
#10 Accepted 389ms 33.086 MiB
#11 Accepted 402ms 33.227 MiB
#12 Accepted 411ms 33.141 MiB
#13 Accepted 393ms 33.047 MiB
#14 Accepted 279ms 42.383 MiB
#15 Accepted 301ms 42.301 MiB
#16 Accepted 298ms 42.426 MiB

Code

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

#define int        long long int
#define pb         push_back
#define all(x)     x.begin(),x.end()
#define allr(x)    x.rbegin(),x.rend()
#define ii         pair<int,int>
#define endl       "\n"

const int MAXN = 2e5 + 5;

int N, Q;
vector<int> g[MAXN], euler, in_time(MAXN), out_time(MAXN);
vector<int> lights(MAXN);
vector<int> seg(4 * MAXN), lazy(4 * MAXN);

void dfs(int node, int parent) {
    in_time[node] = euler.size();
    euler.pb(node);
    for (int child : g[node]) {
        if (child != parent) {
            dfs(child, node);
        }
    }
    out_time[node] = euler.size() - 1;
}

void build(int node, int start, int end) {
    if (start == end) {
        seg[node] = lights[euler[start]];
    } else {
        int mid = (start + end) / 2;
        build(2 * node, start, mid);
        build(2 * node + 1, mid + 1, end);
        seg[node] = seg[2 * node] + seg[2 * node + 1];
    }
}

void propagate(int node, int start, int end) {
    if (lazy[node] != 0) {
        seg[node] = (end - start + 1) - seg[node];
        if (start != end) {
            lazy[2 * node] ^= lazy[node];
            lazy[2 * node + 1] ^= lazy[node];
        }
        lazy[node] = 0;
    }
}

void update_range(int node, int start, int end, int l, int r) {
    propagate(node, start, end);
    if (start > end || start > r || end < l) return;
    if (start >= l && end <= r) {
        lazy[node] ^= 1;
        propagate(node, start, end);
        return;
    }
    int mid = (start + end) / 2;
    update_range(2 * node, start, mid, l, r);
    update_range(2 * node + 1, mid + 1, end, l, r);
    seg[node] = seg[2 * node] + seg[2 * node + 1];
}

int query_range(int node, int start, int end, int l, int r) {
    propagate(node, start, end);
    if (start > end || start > r || end < l) return 0;
    if (start >= l && end <= r) return seg[node];
    int mid = (start + end) / 2;
    return query_range(2 * node, start, mid, l, r) + query_range(2 * node + 1, mid + 1, end, l, r);
}

void pipra(){
    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].pb(v);
        g[v].pb(u);
    }

    dfs(1, -1);

    build(1, 0, N - 1);

    cin >> Q;
    while (Q--) {
        int type, k;
        cin >> type >> k;
        if (type == 1) {
            update_range(1, 0, N - 1, in_time[k], out_time[k]);
        } else {
            cout << query_range(1, 0, N - 1, in_time[k], out_time[k]) << "\n";
        }
    }
}

int32_t main(){
    // HABIB
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); cout.tie(NULL);

    // int t;    cin>>t;
    // while(t--) {
        pipra();
    // }
    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-10-12 12:18:37
Judged At
2024-11-11 02:37:32
Judged By
Score
100
Total Time
447ms
Peak Memory
42.426 MiB