CF 877 E
题意 给你一棵树
get操作为求这个树子树内开着灯个数
pow操作为使这个子树内开灯状态取反
我们只要用dfs序建立一颗线段树
然后对get操作 区间求和
对于pow操作 区间取反即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 200025;
int p[MAX_N],eid,s[MAX_N<<2],col[MAX_N<<2],low[MAX_N],high[MAX_N],dep,arr[MAX_N],Rank[MAX_N];
struct
{
int v,next;
}e[MAX_N<<1];
void init()
{
memset(p,-1,sizeof(p));
eid = 0;
}
void add(int u,int v)
{
e[eid].v = v;
e[eid].next = p[u];
p[u] = eid++;
}
void dfs(int u,int fa)
{
low[u] = ++dep;
Rank[dep] = u;
for(int i = p[u];~i;i=e[i].next)
if(e[i].v!=fa)
dfs(e[i].v,u);
high[u] = dep;
}
void up(int rt)
{
s[rt] = s[rt<<1] + s[rt<<1|1];
}
void build(int rt,int l,int r)
{
col[rt] = 0;
if(l==r)
{
s[rt] = arr[Rank[l]];
return ;
}
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
up(rt);
}
void down(int rt,int l,int r)
{
if(col[rt])
{
col[rt<<1] ^= 1;
col[rt<<1|1] ^= 1;
s[rt<<1] = mid-l+1-s[rt<<1];
s[rt<<1|1] = r-mid - s[rt<<1|1];
col[rt] = 0;
}
}
void update(int rt,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
col[rt] ^= 1;
s[rt] = r-l+1-s[rt];
return ;
}
down(rt,l,r);
if(x<=mid) update(rt<<1,l,mid,x,y);
if(mid<y) update(rt<<1|1,mid+1,r,x,y);
up(rt);
}
int query(int rt,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
return s[rt];
}
int res = 0;
down(rt,l,r);
if(x<=mid) res+=query(rt<<1,l,mid,x,y);
if(mid<y) res+=query(rt<<1|1,mid+1,r,x,y);
return res;
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int Q,n,a,x;
char str[5];
init();
scanf("%d",&n);
for(int i = 2;i<=n;++i)
{
scanf("%d",&a);
add(a,i),add(i,a);
}
for(int i= 1;i<=n;++i) scanf("%d",&arr[i]);
dfs(1,0);
build(1,1,n);
scanf("%d",&Q);
while(Q--)
{
scanf("%s",str);
if(str[0]=='g')
{
scanf("%d",&x);
printf("%d\n",query(1,1,n,low[x],high[x]));
}
else
{
scanf("%d",&x);
update(1,1,n,low[x],high[x]);
}
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}