codeforces 877e


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 0otherwise.

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
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點 輸入每個點的值0或1 輸入每個點的父節點 m次操作 pow操作改變q節點后面的全部節點包括q節點 get操作是問q節點的子節點包括q一共有多少個1

這個題要用線段樹+dfs,意思就是為每個點分配一個區間,區間大的包含區間小的

這個題要注意的是

每個點可能變換多次,你不需要全都變換,查詢時候在一次性的變完就行,否則可能會超時,反正我鏈表這樣做超時了,數組我好像runtime error了   最后也懶得改了,又敲了一遍過的,然后就沒什么注意的了,這題就是這樣

 

 

丑陋的代碼

#include <iostream>

#include <vector>

using namespace std;

void pushdown(int i);

const int N = 2e5 + 10;

int a[N],in[N],out[N],pre[N],cnt = 0;

vector<int> p[N];

struct node

{

    int l = 0,r = 0;

    int sum = 0;

    int lazy = 0;

}pp[N<<2];

void dfs(int n)

{

    for(int i = 0; i < p[n].size(); ++i)

    {

        int d = p[n][i];

        cnt ++;

        in[d] = cnt;

        pre[cnt] = d;

        dfs(d);

        out[d] = cnt;

    }

}

void build(int i)

{

    int mid = (pp[i].l+pp[i].r)>>1;

    int l = pp[i].l;

    int r = pp[i].r;

    

    if(pp[i].l == pp[i].r)

    {

        pp[i].sum = a[pre[l]];

        return ;

    }

    pp[(i<<1)].l = l;

    pp[(i<<1)].r = mid;

    

    pp[(i<<1)+1].l = mid+1;

    pp[(i<<1)+1].r = r;

    

    

    build((i<<1));

    build((i<<1)+1);

    pp[i].sum += pp[i<<1].sum + pp[(i<<1)+1].sum;

}

void update(int l,int r,int i)

{

    int mid = (pp[i].l+pp[i].r) >> 1;

    if(pp[i].l == l && pp[i].r == r)

    {

        pp[i].sum = pp[i].r - pp[i].l + 1 - pp[i].sum;

        pp[i].lazy ^= 1;

        pushdown(i);

      //這個地方要注意pushdowm的操作位置     

        return ;

    }

    if(r <= mid)

    {

        update(l,r, i<<1);

    }

    else if(l >= mid + 1)

    {

        update(l,r,(i<<1)+1);

    }

    else

    {

        update(l, mid, i<<1);

        update(mid+1, r, (i<<1)+1);

    }

    pp[i].sum = pp[i<<1].sum + pp[(i<<1)+1].sum;

}

void pushdown(int i)

{

    if(pp[i].lazy)

    {

        pp[i].sum = pp[i].r - pp[i].l + 1 - pp[i].sum;

        pp[i<<1].lazy ^= 1;

        pp[(i<<1)+1].lazy ^= 1;

        pp[i].lazy ^= 1;

    }

}

int query(int l,int r,int i)

{

    int mid = (pp[i].l + pp[i].r)>>1;

    pushdown(i);

    if(pp[i].l == l && pp[i].r == r)

        return pp[i].sum;

    if(r <= mid)

    {

        return query(l, r, i<<1);

    }

    else if(l >= mid + 1)

    {

        return query(l, r, (i<<1)+1);

    }

    else

    {

        return query(l, mid, i<<1) + query(mid+1, r, (i<<1)+1);

    }

}

int main()

{

    int n,i,j,d,x,ppp;

    char str[20];

    scanf("%d",&n);

    for(i = 2; i <= n; ++i)

    {

        scanf("%d",&d);

        p[d].push_back(i);

    }

    for(i = 1; i <= n; ++i)

        scanf("%d",&a[i]);

    pre[1] = 1;

    cnt = 1;

    in[1] = 1;

    out[1] = n;

    pp[1].l = 1;

    pp[1].r = n;

    dfs(1);

    build(1);

    scanf("%d",&ppp);

    while( ppp-- )

    {

        scanf("%s",str);

        if(str[0] == 'g')

        {

            scanf("%d",&x);

            printf("%d\n",query(in[x], out[x], 1));

        }

        else

        {

            scanf("%d",&x);

            update(in[x], out[x], 1);

        }

    }

    return 0;

}

 

 

 

注意    update的時候假如(pp[i].l == l && pp[i].r == r)這個時候一定要更新當前節點     后面的節點可以不更新,但當前節點必須要更新

具體原因數據在這里     你畫一下圖就明白了

10
1 2 3 3 5 5 7 7 8
0 0 0 0 1 1 1 1 0 0
10
pow 3
get 1
pow 9
get 1
get 1
get 8
pow 8
pow 4
get 10
pow 2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM