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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章