/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 15ms 17.328 MiB
#2 Accepted 16ms 17.27 MiB
#3 Accepted 36ms 19.109 MiB
#4 Accepted 40ms 19.309 MiB
#5 Accepted 35ms 19.18 MiB
#6 Accepted 37ms 19.32 MiB
#7 Accepted 297ms 30.121 MiB
#8 Accepted 296ms 30.227 MiB
#9 Accepted 299ms 30.242 MiB
#10 Accepted 301ms 30.316 MiB
#11 Accepted 306ms 30.133 MiB
#12 Accepted 300ms 30.297 MiB
#13 Accepted 296ms 30.156 MiB
#14 Accepted 189ms 37.0 MiB
#15 Accepted 194ms 36.996 MiB
#16 Accepted 200ms 37.223 MiB

Code

#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif

using namespace std;
#define int long long
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';

const int N = 2e5 + 5;
vector<int> tree(4 * N), lazy(4 * N), adj[N];
int n, q, arr[N], tin[N], tout[N], timer = 0;

void propagate(int node, int l, int r) {
    if (lazy[node] == 0) return;
    tree[node] = r - l + 1 - tree[node];
    if (l != r) {
        lazy[node * 2] = (lazy[node * 2] + lazy[node]) % 2;
        lazy[node * 2 + 1] = (lazy[node * 2 + 1] + lazy[node]) % 2;
    }
    lazy[node] = 0;
}

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] = (lazy[node] + 1) % 2;
        propagate(node, start, end);
        return;
    }
    int mid = (start + end) / 2;
    update(node * 2, start, mid, l, r);
    update(node * 2 + 1, mid + 1, end, l, r);
    tree[node] = tree[node * 2] + tree[node * 2 + 1];
}

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 tree[node];
    int mid = (start + end) / 2;
    int q1 = query(node * 2, start, mid, l, r);
    int q2 = query(node * 2 + 1, mid + 1, end, l, r);
    return q1 + q2;
}

void euler_tour(int now, int par) {
    tin[now] = ++timer;
    for (auto &it: adj[now]) {
        if (it == par) continue;
        euler_tour(it, now);
    }
    tout[now] = timer;
}

void shelby() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> arr[i];
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    euler_tour(1, -1);
    for (int i = 1; i <= n; ++i) {
        if (arr[i]) update(1, 1, n, tin[i], tin[i]);
    }
    int q;
    cin >> q;
    while (q--) {
        int t, k;
        cin >> t >> k;
        if (t == 1) update(1, 1, n, tin[k], tout[k]);
        else cout << query(1, 1, n, tin[k], tout[k]) << '\n';
    }
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        // cout << "Case " << _ << ": ";
        shelby();
    }
}

Information

Submit By
Type
Submission
Problem
P1101 Mr. Heart and the Enchanted Lights
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-04 18:17:50
Judged At
2024-11-11 02:42:23
Judged By
Score
100
Total Time
306ms
Peak Memory
37.223 MiB