/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 5.02 MiB
#2 Accepted 6ms 5.02 MiB
#3 Accepted 52ms 7.023 MiB
#4 Accepted 52ms 7.164 MiB
#5 Accepted 39ms 6.848 MiB
#6 Accepted 45ms 6.941 MiB
#7 Accepted 607ms 19.141 MiB
#8 Accepted 597ms 19.199 MiB
#9 Accepted 613ms 19.234 MiB
#10 Accepted 612ms 19.215 MiB
#11 Accepted 595ms 19.191 MiB
#12 Accepted 606ms 19.227 MiB
#13 Accepted 605ms 19.129 MiB
#14 Accepted 338ms 25.469 MiB
#15 Accepted 347ms 27.395 MiB
#16 Accepted 355ms 27.465 MiB

Code

#include <iostream>
#include <vector>
using namespace std;

const int MAXN = 200005;
int N, Q;
int A[MAXN];  // Initial state of lights
vector<int> adj[MAXN];  // Adjacency list for the tree
int in[MAXN], out[MAXN], euler[MAXN];  // Euler Tour data
int segTree[4 * MAXN], lazy[4 * MAXN];  // Segment tree and lazy array
int timer = 0;

// DFS to perform Euler Tour
void dfs(int node, int parent) {
    in[node] = ++timer;
    euler[timer] = A[node];
    for (int neighbor : adj[node]) {
        if (neighbor != parent) {
            dfs(neighbor, node);
        }
    }
    out[node] = timer;
}

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

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

// Update range with lazy propagation
void update(int node, int start, int end, int l, int r) {
    propagate(node, start, end);
    if (start > r || end < l)
        return;
    if (start >= l && end <= r) {
        lazy[node] ^= 1;
        propagate(node, start, end);
        return;
    }
    int mid = (start + end) / 2;
    update(2 * node, start, mid, l, r);
    update(2 * node + 1, mid + 1, end, l, r);
    segTree[node] = segTree[2 * node] + segTree[2 * node + 1];
}

// Query range with lazy propagation
int query(int node, int start, int end, int l, int r) {
    propagate(node, start, end);
    if (start > r || end < l)
        return 0;
    if (start >= l && end <= r)
        return segTree[node];
    int mid = (start + end) / 2;
    return query(2 * node, start, mid, l, r) + query(2 * node + 1, mid + 1, end, l, r);
}

int main() {
    // Input reading
    cin >> N;
    for (int i = 1; i <= N; ++i) {
        cin >> A[i];
    }

    for (int i = 0; i < N - 1; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    // DFS to flatten the tree
    dfs(1, -1);

    // Build the segment tree
    build(1, 1, N);

    cin >> Q;
    while (Q--) {
        int type, K;
        cin >> type >> K;
        if (type == 1) {
            // Toggle the lights in the subtree of K
            update(1, 1, N, in[K], out[K]);
        } else if (type == 2) {
            // Count the lights on in the subtree of K
            cout << query(1, 1, N, in[K], out[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-03 19:16:15
Judged At
2024-11-11 02:44:50
Judged By
Score
100
Total Time
613ms
Peak Memory
27.465 MiB