Danil and a Part-time Job

Danil and a Part-time Job

time limit per test2 seconds
memory limit per test256 megabytes
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 1 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:

p o w v pow \quad v powv describes a task to switch lights in the subtree of vertex v.
g e t v get \quad v getv describes a task to count the number of rooms in the subtree of v v v in which the light is turned on. Danil should send the answer to his boss using Workforces messages.
A subtree of vertex v v v is a set of vertices for which the shortest path from them to the root passes through v v v In particular, the vertex v v v is in the subtree of v v 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 ) n (1 ≤ n ≤ 200 000) n(1n200000) — the number of vertices in the tree.

The second line contains n - 1 space-separated integers p 2 ,   p 3 ,   . . . ,   p n ( 1   ≤   p i   <   i ) p_2, p_3, ..., p_n (1 ≤ p_i < i) p2,p3,...,pn(1pi<i), where p i p_i pi is the ancestor of vertex i.

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

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

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

Output

For each task get v v v print the number of rooms in the subtree of v v 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
/***  Amber  ***/
#pragma GCC optimize(3,"Ofast","inline")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define ls (rt<<1)
#define rs (rt<<1|1)
typedef long long ll;
typedef pair<int,int> pii;
template <typename T>
inline void read(T &x) {
     
    x = 0;
    static int p;
    p = 1;
    static char c;
    c = getchar();
    while (!isdigit(c)) {
     
        if (c == '-')p = -1;
        c = getchar();
    }
    while (isdigit(c)) {
     
        x = (x << 1) + (x << 3) + (c - 48);
        c = getchar();
    }
    x *= p;
}
template <typename T>
inline void print(T x) {
     
    if (x<0) {
     
        x = -x;
        putchar('-');
    }
    static int cnt;
    static int a[50];
    cnt = 0;
    do {
     
        a[++cnt] = x % 10;
        x /= 10;
    } while (x);
    for (int i = cnt; i >= 1; i--)putchar(a[i] + '0');
    puts("");
}
const double Pi=acos(-1);
const double eps=1e-6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const ll Inf = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5+10;

int n;
struct edge{
     
    int v,nxt;
}e[maxn << 1];
int t,head[maxn];
inline void add(int u,int v) {
     
    t++;
    e[t].v = v;
    e[t].nxt = head[u];
    head[u] = t;
}
int a[maxn];
int tot,dfn[maxn],in[maxn],out[maxn],rt,tim;
void dfs(int u,int fa) {
     
    in[u] = ++tim;
    dfn[tim] = u;
    for (int i = head[u]; i; i = e[i].nxt) {
     
        int v = e[i].v;
        if (v == fa) continue;
        dfs(v, u);
    }
    out[u] = tim;
}
int sum[maxn << 2];
inline void pushup(int rt) {
     
    sum[rt] = sum[ls] + sum[rs];
}
int L[maxn << 2],R[maxn << 2];
void build(int rt,int l,int r) {
     
    L[rt]=l;
    R[rt]=r;
    if (l == r) {
     
        sum[rt] = a[dfn[l]];
        return;
    }
    int mid = l + r >> 1;
    build(ls, l, mid);
    build(rs, mid + 1, r);
    pushup(rt);
}
int lazy[maxn << 2];
inline void pushdown(int rt) {
     
    if (lazy[rt]) {
     
        lazy[ls] ^= 1;
        lazy[rs] ^= 1;
        sum[ls] = (R[ls] - L[ls] + 1) - sum[ls];
        sum[rs] = (R[rs] - L[rs] + 1) - sum[rs];
        lazy[rt] = 0;
    }
}
void update(int rt,int l,int r,int ql,int qr) {
     
    if (ql <= l && r <= qr) {
     
        sum[rt] = (r - l + 1) - sum[rt];
        lazy[rt] ^= 1;
        return;
    }
    pushdown(rt);
    int mid = l + r >> 1;
    if (ql <= mid) update(ls, l, mid, ql, qr);
    if (qr > mid) update(rs, mid + 1, r, ql, qr);
    pushup(rt);
}
int query(int rt,int l,int r,int ql,int qr) {
     
    if (ql <= l && r <= qr) {
     
        return sum[rt];
    }
    pushdown(rt);
    int res = 0;
    int mid = l + r >> 1;
    if (ql <= mid) res += query(ls, l, mid, ql, qr);
    if (qr > mid) res += query(rs, mid + 1, r, ql, qr);
    pushup(rt);
    return res;
}
inline void work() {
     
    scanf("%d",&n);
    for (int i = 2; i <= n; i++) {
     
        int fa;
        scanf("%d",&fa);
        add(fa, i);
        add(i, fa);
    }
    for (int i = 1; i <= n; i++) {
     
        scanf("%d",&a[i]);
    }
    dfs(1, -1);
    build(1, 1, n);
    int q;
    scanf("%d",&q);
    while (q--) {
     
        char op[9];
        int v;
        scanf("%s %d", op, &v);
        if (op[0] == 'p') update(1, 1, n, in[v], out[v]);
        else print(query(1, 1, n, in[v], out[v]));
    }
}
int main() {
     
    //freopen("1.txt","r",stdin);
    int T = 1;
    //read(T);
    while (T--) {
     
        work();
    }
    return 0;
}

你可能感兴趣的:(Codeforces,数据结构,acm竞赛)