/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 9ms 11.711 MiB
#2 Wrong Answer 9ms 11.566 MiB
#3 Wrong Answer 34ms 13.109 MiB

Code

// PIPRA ||  HABIB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int        long long int
#define pb         push_back
#define all(x)     x.begin(),x.end()
#define allr(x)    x.rbegin(),x.rend()
#define ii         pair<int,int>
#define endl       "\n"

template<typename T>
ostream& operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for (const auto &x : v) os << " " << x;
        return os << '}';
}

const int N = 2e5 + 5;
int n;
vector<vector<int>> g(N);
vector<int> euler;
vector<int> startTime(N), endTime(N);
vector<int> lights(N);
int seg[4 * N], lazy[4 * N];

void dfs(int u, int par = 0){
    startTime[u] = euler.size() + 1;
    // cout << "dfs check " << u << ' ' << startTime[u] << endl;
    euler.pb(u);

    for(auto &v: g[u]){
        if(v == par)    continue;
        dfs(v, u);
    }

    endTime[u] = euler.size();
    // cout << endTime[u] << endl;
}

void build(int i, int l, int r){
    if(l == r){
        seg[i] = lights[euler[l-1]];
        return;
    }

    int li = (i >> 1);
    int ri = (i >> 1) + 1;
    int mid = (l + r) >> 1;

    build(li, l, mid);
    build(ri, mid+1, r);

    seg[i] = seg[li] + seg[ri];
}

void lazyPropagate(int i, int start, int end){
    if(lazy[i] != 0){
        seg[i] = (end - start +1) - seg[i];

        if(start != end){
            lazy[1<<i] ^= 1;
            lazy[(1<<i)+1] ^= 1;
        }
        lazy[i] = 0;
    }
}

void update_range(int i, int l, int r, int start, int end){
    lazyPropagate(i, l, r);
    if(l > end or r < start)    return;
    if(l >= start and r <= end){
        lazy[i] ^= 1;
        lazyPropagate(i, l, r);
        return;
    }

    int li = (i >> 1);
    int ri = (i >> 1) + 1;
    int mid = (l + r) >> 1;

    update_range(li, l, mid, start, end);
    update_range(ri, mid+1, end, start, end);

    seg[i] = seg[li] + seg[ri];
}

int query(int i, int l, int r, int start, int end){
    lazyPropagate(i, l, r);
    if(l > end or r < start)    
        return 0;
    if(l >= start and r <= end)
        return seg[i];

    int li = (i >> 1);
    int ri = (i >> 1) + 1;
    int mid = (l + r) >> 1;
    return  query(li, l, mid, start, end) +
            query(ri, mid+1, end, start, end);
}

void pipra(){
    cin >> n;
    for(int i=1 ; i<=n ; i++)   
        cin >> lights[i];

    for(int i=1 ; i<n ; i++){
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }

    dfs(1);
    build(1, 1, n);

    // for(int i=1 ; i<=5 ; i++){
        // cout << startTime[i] << ' ' << endTime[i] << endl;
    // }
    // cout << endl;

    int q;  cin >> q;
    while(q--){
        int p, node;
        cin >> p >> node;
        if(p == 1){
            update_range(1, 1, n, startTime[node], endTime[node]);
        }
        else{
            cout << query(1, 1, n, startTime[node], endTime[node]) << endl;
        }
    }
}

int32_t main(){
    // HABIB
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); cout.tie(NULL);

    // int t;    cin>>t;
    // while(t--) {
        pipra();
    // }
    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 09:20:43
Judged At
2024-11-11 02:37:42
Judged By
Score
5
Total Time
34ms
Peak Memory
13.109 MiB