/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Accepted 20ms 21.812 MiB
#2 Runtime Error 19ms 22.02 MiB
#3 Runtime Error 33ms 23.512 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"

const int N = 2e5 + 5;

vector<vector<int>> g(N);
vector<int> euler, in_time(N), out_time(N), lights(N);
vector<int> seg(4 * N);
vector<int> lazy(4 * N);

void dfs(int u, int par = -1){
    in_time[u] = euler.size();
    euler.pb(u);

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

    out_time[u] = euler.size();
}

void build(int ind, int start, int end){
    if(start == end){
        seg[ind] = lights[euler[start]];
        return;
    }

    int mid = (start + end) / 2;
    int li = (2 * ind);
    int ri = (2 * ind) + 1;

    build(li, start, mid);
    build(ri, mid+1, end);
    seg[ind] = seg[li] + seg[ri];
}

void propagate(int ind, int start, int end){
    if(lazy[ind] != 0){
        seg[ind] = (end - start + 1) - seg[ind];
        if(start != end){
            lazy[2*ind] ^= lazy[ind];
            lazy[2*ind+1] ^= lazy[ind];
        }
        lazy[ind] = 0;
    }
}

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

    int mid = (start + end) / 2;
    int li = (2 * ind);
    int ri = (2 * ind) + 1;

    update_range(li, start, mid, l, r);    
    update_range(ri, mid+1, end, l, r);
    seg[ind] = seg[li] + seg[ri];   
}

int query_range(int ind, int start, int end, int l, int r){
    propagate(ind, start, end);
    if(start > r or end < l or start > end)        return 0;
    if(start >= l and end <= r)     return seg[ind];

    int mid = (start + end) / 1;
    int li = (2 * ind);
    int ri = (2 * ind) + 1;

    return  query_range(li, start, mid, l, r) + 
            query_range(ri, mid+1, end, l, r);
}

void pipra(){
    int n;  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, 0, n-1);

    int q;  cin >> q;
    while(q--){
        int type, node;
        cin >> type >> node;

        if(type == 1){
            update_range(1, 0, n-1, in_time[node], out_time[node]);
        }
        else{
            cout << query_range(1, 0, n-1, in_time[node], out_time[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-13 06:06:20
Judged At
2024-11-11 02:37:22
Judged By
Score
5
Total Time
33ms
Peak Memory
23.512 MiB