/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 18ms 19.52 MiB
#2 Accepted 18ms 19.512 MiB
#3 Accepted 35ms 20.902 MiB
#4 Accepted 36ms 20.887 MiB
#5 Accepted 33ms 20.832 MiB
#6 Accepted 34ms 20.793 MiB
#7 Accepted 276ms 27.578 MiB
#8 Accepted 260ms 27.531 MiB
#9 Accepted 270ms 27.605 MiB
#10 Accepted 263ms 27.543 MiB
#11 Accepted 285ms 27.594 MiB
#12 Accepted 275ms 27.57 MiB
#13 Accepted 241ms 27.477 MiB
#14 Accepted 181ms 38.703 MiB
#15 Accepted 180ms 38.68 MiB
#16 Accepted 179ms 38.77 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();
    // cout << "dfs check " << node << ' ' << start_time[node] << endl;
    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);

    // for(int i=0 ; i<=12 ; i++){
        // cout << seg[i] << ' ';
    // }
    // cout << endl;

    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
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-12 12:04:58
Judged At
2024-11-11 02:37:32
Judged By
Score
100
Total Time
285ms
Peak Memory
38.77 MiB