CodeForces - 877E Danil and a Part-time Job(线段树区间异或,lazy标记,dfs序)

本文介绍了一种在树形结构上使用线段树进行高效查询与更新的算法。该算法通过预处理节点遍历顺序,实现对树的子树进行快速的异或操作与计数操作。

描述

Danil decided to earn some money, so he had found a part-time job. The
interview have went well, so now he is a light switcher.

Danil works in a rooted tree (undirected connected acyclic graph) with
n vertices, vertex 1 is the root of the tree. There is a room in each
vertex, light can be switched on or off in each room. Danil’s duties
include switching light in all rooms of the subtree of the vertex. It
means that if light is switched on in some room of the subtree, he
should switch it off. Otherwise, he should switch it on.

Unfortunately (or fortunately), Danil is very lazy. He knows that his
boss is not going to personally check the work. Instead, he will send
Danil tasks using Workforces personal messages.

There are two types of tasks:

pow v describes a task to switch lights in the subtree of vertex v.
get v describes a task to count the number of rooms in the subtree of
v, in which the light is turned on. Danil should send the answer to
his boss using Workforces messages. A subtree of vertex v is a set of
vertices for which the shortest path from them to the root passes
through v. In particular, the vertex v is in the subtree of v.

Danil is not going to perform his duties. He asks you to write a
program, which answers the boss instead of him.

Input

The first line contains a single integer n (1 ≤ n ≤ 200 000) — the
number of vertices in the tree.

The second line contains n - 1 space-separated integers
p2, p3, …, pn (1 ≤ pi < i), where pi is the ancestor of vertex i.

The third line contains n space-separated integers t1, t2, …, tn
(0 ≤ ti ≤ 1), where ti is 1, if the light is turned on in vertex i and
0 otherwise.

The fourth line contains a single integer q (1 ≤ q ≤ 200 000) — the
number of tasks.

The next q lines are get v or pow v (1 ≤ v ≤ n) — the tasks described
above.

Output

For each task get v print the number of rooms in the subtree of v, in
which the light is turned on.

input

4
1 1 1
1 0 0 1
9
get 1
get 2
get 3
get 4
pow 1
get 1
get 2
get 3
get 4

output

2
0
0
1
2
1
1
0

思路

首先说明题意,给你了一个有n个节点的树,给出方式是给出从[2,n]的父亲,树中的每个节点有一个灯,有亮的也有灭的,题目在第二行给出,用1表示亮灯,用0表示灭灯,然后有两种操作:

  • pow x:表示把x以及x的子树的等变为相反状态,其实也就是异或操作
  • get x:表示求出x及x的子树节点中亮灯的个数

首先我们可以用dfs先预处理出每个树中的每个节点到达时的编号in[i]和返回这个节点的编号out[i],然后可以利用这个编号来建立一棵线段树,线段树维护的是区间亮灯的个数。

当对一个区间进行异或操作的时候,我们知道这个区间内的总点数,那么每个值变成自己的异或值之后,区间的和就变成了区间的数的个数减去区间原本为1的个数。

因为是区间问题,所以要用到lazy标记,当要对每一个区间进行异或的时候,先给这个区间的lazy进行异或操作,等到需要更新子区间的时候再下放lazy.

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
const int N=200000+20;
int in[N],out[N],a[N],times,x;
int sum[N<<2],lazy[N<<2];
vector<int>e[N];
void dfs(int rt)
{
    in[rt]=times;
    for(auto i:e[rt])
    {
        times++;
        dfs(i);
    }
    out[rt]=times;
}
void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt,int m)
{
    if(lazy[rt])
    {
        lazy[rt<<1]^=lazy[rt];
        lazy[rt<<1|1]^=lazy[rt];
        sum[rt<<1]=(m-(m>>1))-sum[rt<<1];
        sum[rt<<1|1]=(m>>1)-sum[rt<<1|1];
        lazy[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    lazy[rt]=0;
    if(l==r)
    {
        sum[rt]=a[l];
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        lazy[rt]^=1;
        sum[rt]=(r-l+1)-sum[rt];
        return;
    }
    pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,lson);
    if(R>m) update(L,R,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return sum[rt];
    pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    int ans=0;
    if(L<=m) ans+=query(L,R,lson);
    if(R>m) ans+=query(L,R,rson);
    return ans;
}
int main()
{
    char s[5];
    int n,q;
    times=1;
    scanf("%d",&n);
    for(int i=2; i<=n; i++)
    {
        scanf("%d",&x);
        e[x].push_back(i);
    }
    dfs(1);
    for(int i=1; i<=n; i++)scanf("%d",&a[in[i]]);
    build(1,n,1);
    scanf("%d",&q);
    while(q--)
    {
        scanf("%s%d",s,&x);
        if(s[0]=='g') printf("%d\n",query(in[x],out[x],1,n,1));
        else update(in[x],out[x],1,n,1);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值