/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 6ms 6.625 MiB
#2 Wrong Answer 7ms 6.453 MiB
#3 Wrong Answer 31ms 11.16 MiB

Code

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

template<typename T3> void write(T3 zZ) { cout << zZ << endl; }
template<typename T, typename... T2> void write(T zZ, T2... more) { cout << zZ << ' '; write(more...); }

const int64_t mod = 1e9 + 7;
#define int long long
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define vi vector<int>
#define sz(a) ((int)(a.size()))
#define all(a) a.begin(), a.end()
#define read(a) for(auto &i : a) cin >> i
#define rep(i, a, n) for(int i = a; i < n; i++)
#define print(a) for(auto x : a) cout << x << " "
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 {
public:
    vector<int> sgtree, lazy; int n;

    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(lazy[node]%2) {

            sgtree[node] = j - i + 1 - sgtree[node];
            if(i != j) {
                lazy[2 * node + 1] += lazy[node];
                lazy[2 * node + 2] += lazy[node];
            }
            lazy[node] = 0;
        }

        if(i >= l and j <= r) return sgtree[node];

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

        return sgtree[node] = a + b;
    }

    void update(int i, int j, int l, int r, int node, int val) {

        if(lazy[node]%2) {

            sgtree[node] = j - i + 1 - sgtree[node]; 
            if(i != j) {
                lazy[2 * node + 1] += lazy[node];
                lazy[2 * node + 2] += lazy[node];
            }
            lazy[node] = 0;
        }

        if(j < l or i > r) return void();

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

            if(i != j) {
                lazy[2 * node + 1] += val;
                lazy[2 * node + 2] += val;
            }
            return void();
        }

        int mid = (i + j) / 2;
        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 11:26:55
Judged At
2024-11-11 02:42:59
Judged By
Score
5
Total Time
31ms
Peak Memory
11.16 MiB