/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 18ms 19.496 MiB
#2 Accepted 18ms 19.664 MiB
#3 Accepted 35ms 20.75 MiB
#4 Accepted 34ms 20.859 MiB
#5 Accepted 30ms 20.859 MiB
#6 Accepted 33ms 20.863 MiB
#7 Accepted 261ms 27.539 MiB
#8 Accepted 248ms 27.566 MiB
#9 Accepted 261ms 27.559 MiB
#10 Accepted 261ms 27.594 MiB
#11 Accepted 268ms 27.594 MiB
#12 Accepted 257ms 27.457 MiB
#13 Accepted 261ms 27.562 MiB
#14 Accepted 180ms 38.887 MiB
#15 Accepted 179ms 38.867 MiB
#16 Accepted 179ms 38.691 MiB

Code

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

#define ll long long
#define pb push_back
#define fast ios::sync_with_stdio(false); cin.tie(nullptr)
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<ll>

const int MAXN = 2e5 + 5;

int N, Q;
vi adj[MAXN], euler, start_time(MAXN), end_time(MAXN);
vi lights(MAXN);
vll seg(4 * MAXN), lazy(4 * MAXN);

void dfs(int node, int parent) {
    start_time[node] = euler.size();
    euler.pb(node);
    for (int child : adj[node]) {
        if (child != parent) {
            dfs(child, node);
        }
    }
    end_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];
}

ll 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);
}

int main() {
    fast;

    cin >> N;
    for (int i = 0; i < N; ++i) {
        cin >> lights[i + 1];
    }


    for (int i = 0; i < N - 1; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[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, start_time[k], end_time[k]);
        } else {
            cout << query_range(1, 0, N - 1, start_time[k], end_time[k]) << "\n";
        }
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1101 Mr. Heart and the Enchanted Lights
Contest
Brain Booster #6
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 17:57:50
Judged At
2024-11-11 02:45:18
Judged By
Score
100
Total Time
268ms
Peak Memory
38.887 MiB