/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 8ms 10.723 MiB
#2 Wrong Answer 7ms 10.27 MiB
#3 Wrong Answer 39ms 13.125 MiB

Code

#include <bits/stdc++.h>
#define ll long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int mod = 1e9 + 7, N = 2e5 + 10;

int timee = 1;

struct node
{
    int odd, even;
};

node t[4 * N];
int lazy[4 * N];
int a[N];

void push(int n, int b, int e)
{ // update specific node by lazy
    if (lazy[n] == 0)
    {
        return;
    }
    if (lazy[n] % 2 == 1)
    {
        swap(t[n].even, t[n].odd);
    }
    if (b != e)
    {
        int mid = (b + e) / 2, l = 2 * n, r = 2 * n + 1;
        lazy[l] = (lazy[l] + lazy[n]);
        lazy[r] = (lazy[r] + lazy[n]);
    }
    lazy[n] = 0;
}

node merge(node l, node r)
{ // what question need ??
    if (l.odd == -1)
        return r;
    if (r.odd == -1)
        return l;
    node ans;
    ans.even = l.even + r.even;
    ans.odd = l.odd + r.odd;
    return ans;
}

void build(int n, int b, int e)
{
    lazy[n] = 0;
    if (b == e)
    {
        t[n].odd = t[n].even = 0;

        if (a[b] % 2)
        {
            t[n].odd = 1;
        }
        else
            t[n].even = 1;


        return;
    }
    int mid = (b + e) / 2, l = 2 * n, r = 2 * n + 1;
    build(l, b, mid);
    build(r, mid + 1, e);
    t[n] = merge(t[l], t[r]);
}

void upd(int n, int b, int e, int i, int j, int v)
{
    push(n, b, e);
    if (e < i or j < b)
        return;
    if (b >= i and e <= j)
    {
        lazy[n] += v; // set lazy
        push(n, b, e);
        return;
    }
    int mid = (b + e) / 2, l = 2 * n, r = 2 * n + 1;
    upd(l, b, mid, i, j, v);
    upd(r, mid + 1, e, i, j, v);
    t[n] = merge(t[l], t[r]);
}

node query(int n, int b, int e, int i, int j)
{
    node tmp;
    tmp.odd = -1;
    push(n, b, e);
    if (e < i or j < b)
        return {tmp}; // resutl
    if (b >= i and e <= j)
    {
        return t[n]; // result
    }
    int mid = (b + e) / 2, l = 2 * n, r = 2 * n + 1;
    return merge(query(l, b, mid, i, j), query(r, mid + 1, e, i, j)); // result
}

int n, ar[N], start[N], endd[N];
vector<int> g[N];

void dfs(int u, int par = 1)
{
    start[u] = timee++;

    for (auto v : g[u])
    {
        if (v != par)
        {
            dfs(v, u);
        }
    }
    endd[u] = timee - 1;
}
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[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);
    // cout << start[1] << " " << endd[1] << endll;
    int q;
    cin >> q;
    while (q--)
    {
        int tp;
        cin >> tp;
        if (tp == 1)
        {
            int nd;
            cin >> nd;
            upd(1, 1, n, start[nd], endd[nd], 1);
        }
        else
        {
            int nd;
            cin >> nd;
            node x = query(1, 1, n, start[nd], endd[nd]);
            cout << x.odd << endll;
        }
    }
}

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    //   cin >> t;
    while (t--)
    {
        solve();
    }
    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 16:19:08
Judged At
2024-11-11 02:48:43
Judged By
Score
5
Total Time
39ms
Peak Memory
13.125 MiB