/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 7ms 11.066 MiB
#2 Wrong Answer 8ms 12.566 MiB
#3 Time Exceeded ≥1024ms ≥12.695 MiB

Code

/**
 *  written by Binoy Barman .
**/

#include<bits/stdc++.h>
using namespace std;
#define nl '\n'
#define all(v) v.begin(), v.end()
#define Too_Many_Jobs int tts, tc = 1; cin >> tts; hell: while(tts--)
#define Dark_Lord_Binoy ios_base::sync_with_stdio(false); cin.tie(NULL);

#ifdef LOCAL
#include "debug/whereisit.hpp"
#else
#define dbg(...) 42
#endif
#define int long long

const int N = 2e5 + 9;

int n, preCleared = 1;
int a[N], on[N], off[N], lazy[N], lvl[N], par[N];
vector<int> g[N];

inline void push(int k) {
    if(lazy[k] == 0) {
        return;
    }
    if(lazy[k] & 1) swap(on[k], off[k]);
    for(auto u : g[k]) {
        lazy[u] += lazy[k];
    }
    lazy[k] = 0;
}
pair<int, int> dfs(int v, int p = -1) {
    lazy[v] = 0;
    par[v] = p;
    for(auto u : g[v]) {
        if(u == p) continue;
        auto tmp = dfs(u, v);
        on[v] += tmp.first;
        off[v] += tmp.second;
        lvl[u] = lvl[v] + 1;
    }
    on[v] += a[v] == 1;
    off[v] += a[v] == 0;
    return {on[v], off[v]};
}
void toggle(int k, int v, int p = -1) {
    push(v);
    for(auto u : g[k]) {
        if(u == k) {
            push(k);
            return;
        }
        if(u == p) continue;
        dfs(u, v);
    }
}

int32_t main() {
Dark_Lord_Binoy
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        on[i] = off[i] = 0;
    }
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1);
    // for (int i = 1; i <= n; i++) {
    //     dbg(on[i], off[i]);
    // }
    int q;
    cin >> q;
    while(q--) {
        int t, k;
        cin >> t >> k;
        if(t == 1) {
            lazy[k]++;
            if(lvl[k] < lvl[preCleared]) toggle(k, k, par[k]);
            else toggle(k, preCleared, par[preCleared]);
            preCleared = k;
        } else {
            if(lvl[k] < lvl[preCleared]) toggle(k, k, par[k]);
            else toggle(k, preCleared, par[preCleared]);
            preCleared = k;
            cout << on[k] << nl;
        }
    }
    // for (int i = 1; i <= n; i++) {
    //     dbg(on[i], off[i]);
    // }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1101 Mr. Heart and the Enchanted Lights
Contest
Brain Booster #6
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 17:55:03
Judged At
2024-11-11 02:45:24
Judged By
Score
5
Total Time
≥1024ms
Peak Memory
≥12.695 MiB