/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 3.52 MiB
#2 Accepted 4ms 3.52 MiB
#3 Accepted 28ms 6.48 MiB
#4 Accepted 29ms 6.719 MiB
#5 Accepted 23ms 6.711 MiB
#6 Accepted 26ms 6.488 MiB
#7 Accepted 361ms 23.012 MiB
#8 Accepted 361ms 23.012 MiB
#9 Accepted 350ms 23.012 MiB
#10 Accepted 343ms 22.816 MiB
#11 Accepted 382ms 22.957 MiB
#12 Accepted 377ms 23.012 MiB
#13 Accepted 367ms 22.781 MiB
#14 Accepted 175ms 34.133 MiB
#15 Accepted 191ms 34.309 MiB
#16 Accepted 190ms 34.312 MiB

Code

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

#define sz(a) ((int)(a.size()))
#define all(a) a.begin(), a.end()
#define rep(i, a, n) for(int i = a; i < n; i++)

const uint32_t N = 2e5 + 1;
int arr[N], timer = 0; 
vector<int> tin(N), tout(N), who(2 * N + 1);

void dfs(int v, int par, vector<int> G[]) {
    tin[v] = ++timer;  
    who[tin[v]] = v;

    for(int u : G[v]) {
        if(u != par) dfs(u, v, G);
    }

    tout[v] = ++timer;
}

class segment_tree {
    vector<int> sgtree, lazy; int n;
public:

    void build(int l, int r, int node, vector<int> &a) {
        if(l == r) {
            sgtree[node] = arr[who[a[l]]]; return void();
        }

        int mid = (l + r) / 2;
        build(l, mid, 2 * node + 1, a);
        build(mid+1, r, 2 * node + 2, a);

        sgtree[node] = sgtree[2 * node + 1] + sgtree[2 * node + 2];
    }

    int query(int i, int j, int l, int r, int node) {
        if(r < i or l > j) return 0;
        if(i >= l and j <= r) return sgtree[node];

        int mid = (i + j) / 2;

        if(lazy[node]%2) {
            update(i, mid, i, mid, node * 2 + 1, lazy[node]);
            update(mid+1, j, mid + 1, j, node * 2 + 2, lazy[node]);
            lazy[node] = 0;
        }
        

        int a = query(i, mid, l, r, 2 * node + 1);
        int b = query(mid+1, j, l, r, 2 * node + 2);

        return a + b;
    }

    void update(int i, int j, int l, int r, int node, int val) {
        if(j < l or i > r) return void();

        int mid = (i + j) / 2;

        if(i >= l and j <= r) {
            sgtree[node] = j - i + 1 - sgtree[node];
            lazy[node] += val;
            return void();
        }

        if(lazy[node]%2) {
            update(i, mid, i, mid, node * 2 + 1, lazy[node]);
            update(mid+1, j, mid + 1, j, node * 2 + 2, lazy[node]);
            lazy[node] = 0;
        }

        update(i, mid, l, r, node * 2 + 1, val);
        update(mid+1, j, l, r, node * 2 + 2, val);

        sgtree[node] = sgtree[2 * node + 1] + sgtree[2 * node + 2];
    }

    void build (vector<int> &a) {
        this->n = (int)(a.size());
        sgtree.resize(4*n); lazy.resize(4*n);
        build(0, n-1, 0, a);
    }

    void update (int l, int r, int val) {
        update(0, n-1, l, r, 0, val);
    }

    int query (int l, int r) {
        return query(0, n-1, l, r, 0);
    }
};

int get_id(int val, vector<int> &T) {
    int ans = sz(T) - 1, l = 0, r = ans;

    while (l <= r) {
        int m = (l + r) / 2;
        
        if (T[m] <= val) {
            ans = m; l = m + 1;
        } else {
            r = m - 1;
        }
    }

    return ans;
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    int n; cin >> n; 
    rep(i, 1, n + 1) cin >> arr[i];
    vector<int> G[n+1];
    
    for(int i = 0; i < n-1; i++) {
        int u, v; cin >> u >> v;
        G[u].push_back(v); G[v].push_back(u);
    }

    dfs(1, 0, G);

    vector<int> stin;
    rep(i, 1, n + 1) stin.push_back(tin[i]);
    sort(all(stin));

    segment_tree st; st.build(stin);

    int q; cin >> q;

    while(q--) {
        int type, v; cin >> type >> v;
        if(type == 1) {
            st.update(get_id(tin[v], stin), get_id(tout[v], stin), 1);
        } else {
            cout << st.query(get_id(tin[v], stin), get_id(tout[v], stin)) << '\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-04 12:18:04
Judged At
2024-11-11 02:42:57
Judged By
Score
100
Total Time
382ms
Peak Memory
34.312 MiB