zoj3686(线段树的区间更新)

时间:2021-12-31 06:43:06

对线段树的区间更新有了初步的了解。。。

A Simple Tree Problem


Time Limit: 3 Seconds      Memory Limit: 65536 KB

Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the labels are 0.

We define this kind of operation: given a subtree, negate all its labels.

And we want to query the numbers of 1's of a subtree.

Input

Multiple test cases.

First line, two integer N and M, denoting the numbers of nodes and numbers of operations and queries.(1<=N<=100000, 1<=M<=10000)

Then a line with N-1 integers, denoting the parent of node 2..N. Root is node 1.

Then M lines, each line are in the format "o node" or "q node", denoting we want to operate or query on the subtree with root of a certain node.

Output

For each query, output an integer in a line.

Output a blank line after each test case.

Sample Input

3 2
1 1
o 2
q 1

Sample Output

1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff
#define N 100100 int n,m;
struct node
{
int to,next;
}edge[*N]; struct node1
{
int b,d,mid;
int flag,sum;
}tnode[*N]; int cnt,pre[N];
int savex[N],savey[N];
int id; // 线段树的区间更新初步了解! void add_edge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=pre[u];
pre[u]=cnt++;
} void dfs(int s,int path)
{
int tmp;
++id;
tmp=id;
for(int p=pre[s];p!=-;p=edge[p].next)
{
int v=edge[p].to;
if(v!=path)
{
dfs(v,s);
}
}
savex[s]=tmp;
savey[s]=id;
} void built(int b,int d,int s)
{
tnode[s].b=b;
tnode[s].d=d;
tnode[s].mid=(b+d)/;
tnode[s].sum=;
tnode[s].flag=;
if(b==d) return ;
built(b,tnode[s].mid,*s);
built(tnode[s].mid+,d,*s+);
} void insert(int b,int d,int s)
{
if(tnode[s].b==b&&tnode[s].d==d)
{
tnode[s].flag^=;
tnode[s].sum=(d-b+) - tnode[s].sum;
return ;
}
if(tnode[s].flag!=)
{
tnode[s].flag=;
tnode[*s].flag^=;
tnode[*s].sum=(tnode[*s].d-tnode[*s].b+)-tnode[*s].sum; tnode[*s+].flag^=;
tnode[*s+].sum=(tnode[*s+].d-tnode[*s+].b+)-tnode[*s+].sum;
}
if(tnode[s].mid>=d)
insert(b,d,*s);
else if(tnode[s].mid+<=b)
insert(b,d,*s+);
else
{
insert(b,tnode[s].mid,*s);
insert(tnode[s].mid+,d,*s+);
}
tnode[s].sum=tnode[*s].sum+tnode[*s+].sum;//用up,和down就更好理解
} //这样就不需要放回了,更简单了... int find(int b,int d,int s)
{
if(tnode[s].b==b&&tnode[s].d==d)
{
return tnode[s].sum;
}
if(tnode[s].flag!=)
{
tnode[s].flag=;
tnode[*s].flag^=;
tnode[*s].sum=(tnode[*s].d-tnode[*s].b+)-tnode[*s].sum; tnode[*s+].flag^=;
tnode[*s+].sum=(tnode[*s+].d-tnode[*s+].b+)-tnode[*s+].sum;
}
if(tnode[s].mid>=d)
return find(b,d,s*);
else if(tnode[s].mid+<=b)
return find(b,d,*s+);
else return find(b,tnode[s].mid,s*)+find(tnode[s].mid+,d,*s+);
} int main()
{
//freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
//freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt=;
id=;
memset(pre,-,sizeof(pre));
for(int i=;i<=n;i++)
{
int tmp;
scanf("%d",&tmp);
add_edge(i,tmp);
add_edge(tmp,i);
}
dfs(,);
///////////////
built(,n,);
for(int i=;i<m;i++)
{
char sel;
int tmp;
cin>>sel;
scanf("%d",&tmp);
if(sel=='o')
{
insert(savex[tmp],savey[tmp],);
}
else
{
printf("%d\n",find(savex[tmp],savey[tmp],));
}
}
printf("\n");
}
return ;
}