Danil and a Part-time Job codeforces dfs序,线段树

来源:互联网 发布:廖雪峰java 编辑:程序博客网 时间:2024/05/17 06:13
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 usingWorkforces personal messages.

There are two types of tasks:

  1. pow v describes a task to switch lights in the subtree of vertexv.
  2. get v describes a task to count the number of rooms in the subtree ofv, in which the light is turned on. Danil should send the answer to his boss usingWorkforces messages.

A subtree of vertex v is a set of vertices for which the shortest path from them to the root passes throughv. In particular, the vertexv 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 integersp2, p3, ..., pn (1 ≤ pi < i), wherepi is the ancestor of vertexi.

The third line contains n space-separated integerst1, t2, ..., tn (0 ≤ ti ≤ 1), whereti is1, if the light is turned on in vertexi 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 ofv, in which the light is turned on.

Example
Input
41 1 11 0 0 19get 1get 2get 3get 4pow 1get 1get 2get 3get 4
Output
20012110
读完这道题的时候第一感觉就是不能暴力更新,于是大约想到了用线段树,但是无奈实在想不出来如何转化,看了题解之后发现是dfs序,之前没有做过这种题,所以收获很大。这道题我大概用了4个小时才A掉,因为很久都不接触线段树的题目了,所有有些生疏。
解法:
类似tarjan算法,你需要标记子树根节点的入序列与出序列,我就不描述了,看代码就比较清楚了,然后把树上的点都映射到区间上,然后建线段树,这样的话就可以A了
我TM就是让lazy卡了我半天,还是没有深刻理解线段树啊,每次更新的节点我们都可以保证当前节点的值是正确的并且他的祖先节点的值也是正确的,所以说lazy标记向下传递的时候,只有奇数次的lazy可以让下面的值翻转,偶数次的则不行。就这个点卡了很久很久
#include<bits/stdc++.h>using namespace std;const int M=2e5+10;int tree[4*M],lazy[4*M],root=1,pre[4*M],cnt,tot,in[4*M],out[4*M],idx[4*M],node[4*M];struct Edge{    int u,v,next;} edge[2*M];void pushdown(int now,int l,int r){    int mid=(l+r)/2;    int lch=now*2,rch=now*2+1;    lazy[lch]+=lazy[now];    lazy[rch]+=lazy[now];    if(lazy[now]%2)    {        lazy[now]=0;        tree[lch]=(mid-l+1)-tree[lch];        tree[rch]=(r-mid)-tree[rch];    }}void update(int now,int l,int r,int al,int ar){    if(al>r||ar<l)        return;    if(l>=al&&r<=ar)    {        lazy[now]++;        tree[now]=(r-l+1)-tree[now];        return ;    }    pushdown(now,l,r);    int mid=(l+r)/2;    update(now*2,l,mid,al,ar);    update(now*2+1,mid+1,r,al,ar);    tree[now]=tree[now*2]+tree[now*2+1];}int query(int now,int l,int r,int al,int ar){    if(al>r||ar<l)        return 0;    if(l>=al&&r<=ar)    {        return tree[now];    }    pushdown(now,l,r);    int mid=(l+r)/2;    return query(now*2,l,mid,al,ar)+query(now*2+1,mid+1,r,al,ar);}void creat(int now,int l,int r,int x[]){    lazy[now]=0;    if(l==r)    {        tree[now]=node[x[l]];        return ;    }    int mid=(l+r)/2;    creat(now*2,l,mid,x);    creat(now*2+1,mid+1,r,x);    tree[now]=tree[now*2]+tree[now*2+1];}void add(int u,int v){    edge[cnt].u=u;    edge[cnt].v=v;    edge[cnt].next=pre[u];    pre[u]=cnt++;}void dfs(int now,int fa){    in[now]=++tot;    idx[tot]=now;    for(int i=pre[now]; i!=-1; i=edge[i].next)    {        int v=edge[i].v;        if(v!=fa)            dfs(v,now);    }    out[now]=tot;}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        cnt=0;        tot=0;        memset(pre,-1,sizeof(pre));        for(int i=2; i<=n; i++)        {            int tep;            scanf("%d",&tep);            add(tep,i);            add(i,tep);        }        for(int i=1; i<=n; i++)            scanf("%d",&node[i]);        dfs(1,1);        creat(1,1,n,idx);        int q;        scanf("%d",&q);        while(q--)        {            char op[10];            int nod;            scanf("%s%d",op,&nod);            if(op[0]=='g')            {                int l=in[nod];                int r=out[nod];                int ans=query(1,1,n,l,r);                printf("%d\n",ans);            }            else            {                int l=in[nod];                int r=out[nod];                update(1,1,n,l,r);            }        }    }}


阅读全文
0 0
原创粉丝点击