真的菜的不行,自己敲一个模板,到处都是问题。哎
1 #include2 using namespace std; 3 const int maxn = 2e5+5; 4 #define lson (q<<1) 5 #define rson ((q<<1)|1) 6 struct node 7 { 8 int l,r,mid; 9 int v,lazy; 10 }tree[maxn*4]; 11 int L[maxn],R[maxn],index; 12 int vis[maxn] = { 0}; 13 int a[maxn]; 14 vector g[maxn]; 15 void dfs(int x) 16 { 17 L[x] = index; 18 for(int i=0;i =l&&tree[q].r<=r) 62 { 63 tree[q].lazy ^= 1; 64 tree[q].v = (tree[q].r-tree[q].l+1)-tree[q].v; 65 return; 66 } 67 push_down(q); 68 if(l<=tree[q].mid) update(l,r,lson); 69 if(r>=tree[q].mid+1) update(l,r,rson); 70 push_up(q); ///v值由下往上更新 71 } 72 int query(int l,int r,int q) ///由上及下 73 { 74 if(tree[q].l>=l&&tree[q].r<=r) 75 { 76 // printf("%d %d %d %d\n",tree[q].l,tree[q].r,tree[q].v,tree[q].lazy); 77 return tree[q].v; 78 } 79 push_down(q); 80 int sum = 0; 81 if(l<=tree[q].mid) sum += query(l,r,lson); 82 if(r>=tree[q].mid+1) sum += query(l,r,rson); 83 return sum; 84 } 85 int main() 86 { 87 int n;scanf("%d",&n); 88 for(int i=2;i<=n;i++) 89 { 90 int x;scanf("%d",&x); 91 g[x].push_back(i); 92 g[i].push_back(x); 93 } 94 index = 1;vis[1] = 1; 95 dfs(1); 96 for(int i=1;i<=n;i++) 97 { 98 int cc; 99 scanf("%d",&cc);100 a[L[i]] = cc; ///按dfs序来赋值101 }102 build(1,n,1);103 int q;scanf("%d",&q);104 while(q--)105 {106 char s[5];107 int v;108 scanf("%s %d",s,&v);109 if(s[0]=='g')110 {111 printf("%d\n",query(L[v],R[v],1));112 }113 else114 {115 update(L[v],R[v],1);116 }117 }118 return 0;119 }120 /*121 10122 1 2 3 3 5 5 7 7 8123 0 0 0 0 1 1 1 1 0 0124 10125 pow 3126 get 3127 */