1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| #include <bits/stdc++.h>
#define NUM 200000+10 #define o_l (o<<1) #define o_r (o_l+1)
using namespace std;
struct sgtnode{ int l,r; int swiced; int on,off; sgtnode(){swiced=0;on=0;off=0;} sgtnode(int s,int n,int f): swiced(s),on(n),off(f){} };
int n; vector<int> child[NUM];
int ettIndex2ONorOFF[2*NUM]; int ett_l[NUM]={0}; int ett_r[NUM]={0};
sgtnode sgmnttr[4*2*NUM];
void lazydown(int o){ if(sgmnttr[o].swiced == 1){ sgmnttr[o].swiced = 0; swap(sgmnttr[o].on,sgmnttr[o].off); sgmnttr[o_l].swiced ^=1; sgmnttr[o_r].swiced ^=1; } }
void segswi(int o,int l,int r){ if(sgmnttr[o].l == l and sgmnttr[o].r == r){ sgmnttr[o].swiced ^= 1; return ; } lazydown(o); int mid=(sgmnttr[o].l+sgmnttr[o].r)/2; if(l <= mid) segswi(o_l,l,min(r,mid)); if(r > mid) segswi(o_r,max(l,mid+1),r); sgmnttr[o].on = (sgmnttr[o_l].swiced?sgmnttr[o_l].off:sgmnttr[o_l].on)+ (sgmnttr[o_r].swiced?sgmnttr[o_r].off:sgmnttr[o_r].on); sgmnttr[o].off = (sgmnttr[o_l].swiced?sgmnttr[o_l].on:sgmnttr[o_l].off)+ (sgmnttr[o_r].swiced?sgmnttr[o_r].on:sgmnttr[o_r].off); }
int segqry(int o,int l,int r){ if(sgmnttr[o].l == l and sgmnttr[o].r == r) return sgmnttr[o].swiced?sgmnttr[o].off:sgmnttr[o].on; lazydown(o); int mid=(sgmnttr[o].l+sgmnttr[o].r)/2; int ret=0; if(l <= mid) ret += segqry(o_l,l,min(r,mid)); if(r > mid) ret += segqry(o_r,max(l,mid+1),r); return ret; }
void build_segtree(int o,int l,int r){ sgmnttr[o].l = l; sgmnttr[o].r = r; if(l == r){ sgmnttr[o].off = 1^(sgmnttr[o].on = ettIndex2ONorOFF[l]); return ; } int mid =(l+r)/2; build_segtree(o_l,l,mid); build_segtree(o_r,mid+1,r); sgmnttr[o].on = sgmnttr[o_l].on + sgmnttr[o_r].on; sgmnttr[o].off = sgmnttr[o_l].off + sgmnttr[o_r].off; }
void qry(int index){ printf("%d\n",segqry(1,ett_l[index],ett_r[index])/2); }
void swi(int index){ segswi(1,ett_l[index],ett_r[index]); }
void build_ett(int tree_index){ static int ett_index = 0; ett_l[tree_index] = ett_index++; for(auto each_child : child[tree_index]) build_ett(each_child); ett_r[tree_index] = ett_index++; }
int main(){ cin>>n; int i; for(i=2;i<=n;i++){ int tmp; scanf("%d",&tmp); child[tmp].push_back(i); } build_ett(1);
for(i=1;i<=n;i++){ int tmp; scanf("%d",&tmp); ettIndex2ONorOFF[ett_l[i]]=tmp; ettIndex2ONorOFF[ett_r[i]]=tmp; } build_segtree(1,0,2*n-1);
int q; cin>>q; while(q--){ char s[10]; int tmp; scanf("%s %d",s,&tmp); if(s[0]=='g'){ qry(tmp); }else{ swi(tmp); } } return 0; }
|