CodeForces - 877E Danil and a Part-time Job(线段树+dfs序)

E. Danil and a Part-time Job
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. pow v describes a task to switch lights in the subtree of vertex v.
  2. 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.

Example
input
Copy
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
Copy
2
0
0
1
2
1
1
0

题意:有n个点,1是根节点,给出2~n每个节点的父节点,以及初始时每个点是否被点亮(点亮为1,否则为0)

有两种操作

1:get x 询问x及x的子树里有多少个节点被点亮

2:pow x 对x及x的子树(如果点亮则熄灭,若熄灭则点亮)


分析:查询次数很多,很容易想到线段树,由于更新每个点时还要对子树记性更新,于是需要dfs序,

操作2处理一个区间时,只需要将这个节点更新为 该区间的长度-该区间亮灯个数,lazy向下更新时也只用考虑奇数的情况(操作两次和不操作效果一样)

#include<bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
typedef long long ll;
const int N=2e5+200;
int t[N<<2],lazy[N<<2],tot,L[N],R[N],a[N],b[N];
vector<int>e[N];
void build(int l,int r,int rt)
{
    if(l==r)
    {
        t[rt]=b[l];
        return;
    }
    int m=(l+r)/2;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    t[rt]=t[rt<<1]+t[rt<<1|1];
}
void pushdown(int rt,int m)
{
    if(lazy[rt])
    {
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        if(lazy[rt]&1)
        {
            t[rt<<1]=(m-(m>>1))-t[rt<<1];
            t[rt<<1|1]=(m>>1)-t[rt<<1|1];
        }
        lazy[rt]=0;
    }
}
void update(int ql,int qr,int l,int r,int rt)
{
    if(ql==l&&qr==r)
    {
        t[rt]=r-l+1-t[rt];
        lazy[rt]++;
        return;
    }
    pushdown(rt,r-l+1);
    int m=(l+r)/2;
    if(qr<=m)update(ql,qr,l,m,rt<<1);
    else if(ql>m)update(ql,qr,m+1,r,rt<<1|1);
    else
    {
        update(ql,m,l,m,rt<<1);
        update(m+1,qr,m+1,r,rt<<1|1);
    }
    t[rt]=t[rt<<1]+t[rt<<1|1];
}
int query(int ql,int qr,int l,int r,int rt)
{
    if(ql==l&&qr==r)
        return t[rt];
    pushdown(rt,r-l+1);
    int m=(l+r)/2;
    if(qr<=m)return query(ql,qr,l,m,rt<<1);
    else if(ql>m)return query(ql,qr,m+1,r,rt<<1|1);
    else
        return query(ql,m,l,m,rt<<1)+query(m+1,qr,m+1,r,rt<<1|1);
    t[rt]=t[rt<<1]+t[rt<<1|1];
}
void dfs(int u)
{
    L[u]=++tot;
    b[tot]=a[u];
    for(auto v:e[u])
        dfs(v);
    R[u]=tot;
}
int main()
{
    int n,x,q;
    char s[10];
    scanf("%d",&n);
    for(int i=2; i<=n; i++)
    {
        scanf("%d",&x);
        e[x].push_back(i);
    }
    for(int i=1; i<=n; i++)scanf("%d",&a[i]);
    dfs(1);
    build(1,n,1);
    scanf("%d",&q);
    while(q--)
    {
        scanf("%s%d",s,&x);
        if(s[0]=='p')
            update(L[x],R[x],1,n,1);
        else
            printf("%d\n",query(L[x],R[x],1,n,1));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37134257/article/details/80013642