/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 576.0 KiB
#2 Accepted 4ms 400.0 KiB
#3 Accepted 54ms 3.934 MiB
#4 Accepted 51ms 3.918 MiB
#5 Accepted 40ms 3.453 MiB
#6 Accepted 47ms 3.875 MiB
#7 Accepted 671ms 23.328 MiB
#8 Accepted 631ms 23.164 MiB
#9 Accepted 619ms 23.207 MiB
#10 Accepted 628ms 23.34 MiB
#11 Accepted 629ms 23.242 MiB
#12 Accepted 620ms 23.23 MiB
#13 Accepted 605ms 23.336 MiB
#14 Accepted 331ms 33.496 MiB
#15 Accepted 342ms 37.684 MiB
#16 Accepted 339ms 37.555 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
using namespace std;
using ll=long long;

int a[200005],first[200005],last[200005];
vector<int> g[200005],tour;
int ind=0;

void eulertour(int node,int par)
{
    first[node] = ind++;
    tour.push_back(a[node]);
    for(auto e:g[node])
    {
        if(e==par) continue;
        eulertour(e,node);
    }
    last[node] = ind-1;
    //tour.push_back(a[node]);
}

struct SGtreeLazy{
    ll sg[800005],lazy[800005];
    void build(int node,int start,int end)
    {
        if(start == end)
        {
            sg[node] = tour[end];
            return;
        }
        int mid = (start+end)/2;
        build(node*2,start,mid); 
        build(node*2+1,mid+1,end);
        sg[node] = sg[node*2]+sg[node*2+1];
    }

    void push_lazy(int node,int start,int end)
    {
        if(lazy[node])
        {
            //sg[node] = sg[node] + (end-start+1)*lazy[node];
            sg[node] = (end-start+1) - sg[node];
            if(start != end) 
            {
                lazy[node*2] ^= lazy[node];
                lazy[node*2+1] ^= lazy[node];
            }
            lazy[node] = 0;
        }
    }

    void rangeupdate(ll node,ll start,ll end,ll l,ll r,ll val)
    {
        push_lazy(node,start,end);
        if(l>r || l>end || start>r) return;
        if(l<=start && r>=end)
        {
            lazy[node] ^= val;
            push_lazy(node,start,end);
            return;
        }
        int mid = (start+end)/2;
        rangeupdate(node*2,start,mid,l,r,val);
        rangeupdate(node*2+1,mid+1,end,l,r,val);
        sg[node] = sg[node*2] + sg[node*2+1];
    }

    ll query(ll node,ll start,ll end,ll l,ll r)
    {
        push_lazy(node,start,end);
        if(l>end || start>r) return 0;
        if(l<=start && r>=end) return sg[node];
        ll mid = (start+end)/2;
        ll q1 = query(node*2,start,mid,l,r);
        ll q2 = query(node*2+1,mid+1,end,l,r);
        sg[node] = sg[node*2]+sg[node*2+1];
        return q1+q2;
    }
} lazy;

int main()
{
    int n; 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].push_back(v);
        g[v].push_back(u);
    }
    eulertour(1,0);
    //for(auto e:tour) cout<<e<<" "; cout<<endl;
    lazy.build(1,0,ind-1);
    int q; cin >> q;
    while(q--)
    {
        int choice,k; cin >> choice >> k;
        if(choice == 1)
        {
            lazy.rangeupdate(1,0,ind-1,first[k],last[k],1);
        }
        else
        {
            cout<<lazy.query(1,0,ind-1,first[k],last[k])<<endl;
        }
    }
}

Information

Submit By
Type
Submission
Problem
P1101 Mr. Heart and the Enchanted Lights
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-24 08:32:35
Judged At
2024-10-03 12:55:49
Judged By
Score
100
Total Time
671ms
Peak Memory
37.684 MiB