877E - Danil and a Part-time Job - CodeForces Solution


bitmasks data structures trees *2000

Please click on ads to support us..

Python Code:

import bisect
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def euler_tour():
    s = []
    s.append(1)
    now = [0] * (n + 1)
    t = 1
    t1, t2 = [-1] * (n + 1), [-1] * (n + 1)
    v = []
    while s:
        i = s[-1]
        if t1[i] == -1:
            v.append(i)
            t1[i] = t
            t += 1
        if now[i] == len(G[i]):
            t2[i] = t
            t += 1
            s.pop()
        else:
            s.append(G[i][now[i]])
            now[i] += 1
    return t1, t2, v

def f(i):
    if not lazy[i]:
        return
    tree[i], lazy[i] = cnt[i] - tree[i], 0
    if i < l1:
        lazy[i << 1] ^= 1
        lazy[i << 1 ^ 1] ^= 1
    return

def update(l, r, s):
    q, ll, rr, i = [1], [0], [l1 - 1], 0
    while len(q) ^ i:
        j = q[i]
        l0, r0 = ll[i], rr[i]
        if l <= l0 and r0 <= r:
            lazy[j] ^= s
            f(j)
            i += 1
            continue
        f(j)
        m0 = (l0 + r0) >> 1
        if l <= m0 and l0 <= r:
            q.append(j << 1)
            ll.append(l0)
            rr.append(m0)
        if l <= r0 and m0 + 1 <= r:
            q.append(j << 1 ^ 1)
            ll.append(m0 + 1)
            rr.append(r0)
        i += 1
    for i in q[::-1]:
        if i < l1:
            j, k = i << 1, i << 1 ^ 1
            f(j)
            f(k)
            tree[i] = tree[j] + tree[k]
    return

def get_sum(s, t):
    update(s, t, 0)
    s += l1
    t += l1
    ans = 0
    while s <= t:
        if s % 2:
            ans += tree[s]
            s += 1
        s >>= 1
        if not t % 2:
            ans += tree[t]
            t -= 1
        t >>= 1
    return ans

n = int(input())
p = [0] * 2 + list(map(int, input().split()))
t = [0] + list(map(int, input().split()))
G = [[] for _ in range(n + 1)]
for i in range(2, n + 1):
    G[p[i]].append(i)
t1, t2, v = euler_tour()
u = [t1[i] for i in v]
l1 = pow(2, n.bit_length())
l2 = 2 * l1
tree, lazy = [0] * l2, [0] * l2
for i in range(n):
    tree[i + l1] = t[v[i]]
for i in range(l1 - 1, 0, -1):
    tree[i] = tree[2 * i] + tree[2 * i + 1]
cnt = [0] * l2
for i in range(n):
    cnt[i + l1] = 1
for i in range(l1 - 1, 0, -1):
    cnt[i] = cnt[2 * i] + cnt[2 * i + 1]
q = int(input())
ans = []
for _ in range(q):
    c, x = input().rstrip().decode().split()
    x = int(x)
    l = bisect.bisect_left(u, t1[x])
    r = bisect.bisect_left(u, t2[x]) - 1
    if c == "pow":
        update(l, r, 1)
    else:
        ans0 = get_sum(l, r)
        ans.append(ans0)
sys.stdout.write("\n".join(map(str, ans)))

C++ Code:

/*
                                                  IN THE NAME OF GOD
	...........................................................................................................................
	...........................................................................................................................
	.......@..........@....@@@@@@@@@............@..............@@@@@@@@@@........@@@@@@@@@@@.......@@@@@@@@@@@@@@@@@...........
	.......@........@..........@...............@.@.............@........@........@................................@............
	.......@......@............@..............@...@............@........@........@..............................@..............
	.......@....@..............@.............@.....@...........@........@........@............................@................
	.......@..@................@............@.......@..........@@@@@@@@@@........@..........................@..................
	.......@.@.................@...........@@@@@@@@@@@.........@...@.............@@@@@@@@@@@..............@....................
	.......@....@..............@..........@...........@........@....@............@......................@......................
	.......@.......@...........@.........@.............@.......@.....@...........@....................@........................
	.......@.........@.........@........@...............@......@......@..........@..................@..........................
	.......@...........@...@@@@@@@@@...@.................@.....@.......@.........@@@@@@@@@@@@......@@@@@@@@@@@@@@@.............
	...........................................................................................................................
	...........................................................................................................................
*/

#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

using namespace std;

// this line is private
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef pair<pii, int> piii;
typedef pair<ll, pll> lpll;
typedef pair<pll, ll> plll;
typedef pair<pii, pii> piipii;
typedef pair<pll, pll> pllpll;
typedef long double ld;

#define SQ									   350
#define endl                                   '\n'
#define F                                      first
#define S                                      second
#define Mp                                     make_pair
#define pb                                     push_back
#define pf                                     push_front
#define PQ                                     priority_queue
#define size(x)                                ((ll)x.size())
#define DASH                                   "---------"
#define UNDERLINE                              "_________"
#define all(x)                                 (x).begin(),(x).end()
#define FORD(i, s, e)                          for(int i=s; i>=e; --i)
#define FOR(i, s, e)                           for(int i=s; i<=e; ++i)
#define kill(x)		                           cout << x << '\n', exit(0);
#define set_dec(x)	                           cout << fixed << setprecision(x);
#define ios				                       ios_base::sync_with_stdio(false), cin.tie(0)
#define fuck(x)                                cout << "(" << #x << " , " << x << ")" << endl
#define file_io(x,y)	                       freopen(x, "r", stdin); freopen(y, "w", stdout);

const int N = 532323, LG=262144;
const ll MOD = 1e9+7;

ll getmod(ll a, ll mod=MOD)                    {return (a+3*mod)%mod;}
ll max(ll a, ll b)                             {return (a>b ? a : b);}
ll min(ll a, ll b)                             {return (a<b ? a : b);}
ll abso(ll a)                                  {return (a<0?-a:a);}

int n, m, A[N], lazy[N], mark[N], typ, st[N], fin[N], par[N], t;
vector<int> adj[N];

void dfs(int v) {
	st[v]=++t;
	for(auto it : adj[v]) {dfs(it);}
	fin[v]=t+1;
}

void push(int ind, int sz) {
	if(mark[ind]==0 || ind>LG-1) {return;}
	if(lazy[ind]==0) {mark[ind]=0; return;}

	lazy[2*ind]^=lazy[ind];
	lazy[2*ind+1]^=lazy[ind];
	A[2*ind]=sz-A[2*ind];
	A[2*ind+1]=sz-A[2*ind+1];
	mark[2*ind]=mark[2*ind+1]=1;
	mark[ind]=lazy[ind]=0;
}

void update(int l, int r, int lc=1, int rc=LG+1, int ind=1) {
	push(ind, (rc-lc)/2);
	if(rc<=l || lc>=r|| ind>2*LG-1) {return;}
	if(rc<=r && lc>=l) {
		A[ind]=(rc-lc)-A[ind];
		mark[ind]=1;
		lazy[ind]^=1;
		return;
	}
	ll mid=(rc+lc)/2;
	update(l, r, lc, mid, 2*ind);
	update(l, r, mid, rc, 2*ind+1);
	A[ind]=A[2*ind]+A[2*ind+1];
}

int query(int l, int r, int lc=1, int rc=LG+1, int ind=1) {
	push(ind, (rc-lc)/2);
	if(rc<=l || lc>=r || ind>2*LG-1) {return 0;}
	if(rc<=r && lc>=l) {return A[ind];}
	ll mid=(rc+lc)/2;
	return (query(l, r, lc, mid, 2*ind)+query(l, r, mid, rc, 2*ind+1));
}

int main () {
	ios;

	cin>>n;
	FOR(i,2,n) {int p;cin>>p;adj[p].pb(i);par[i]=p;}dfs(1);
	FOR(i,1,n) {ll x;cin>>x;if(x) {update(st[i], fin[i]);for(auto it : adj[i]) {update(st[it], fin[it]);}}}

	cin>>m;
	FOR(i,1,m) {
		string s;ll v;
		cin>>s>>v;
		if(s=="get") {
			cout<<query(st[v],fin[v])<<endl;
		} else {
			update(st[v], fin[v]);
		}
	}

	return 0;
}


Comments

Submit
0 Comments
More Questions

448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock