POJ 3321

Description

There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.
The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?

input

The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or "Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

Output

For every inquiry, output the correspond answer per line.

Sample Input

3
1 2
1 3
3
Q 1
C 2
Q 1

Sample Output

3
2


问题分析

给定一棵树,一开始树上每个节点都有苹果,有两种操作:

  • Q x :查询某个节点以及子树的总苹果数。
  • C x :更改该节点,如果有就摘掉,如果没有,就加上。

注意到N ≤ 100,000,M ≤ 100,000,规模较大,考虑O(MlogN)级别的算法,分析问题可以知道,这个问题涉及到求和,可以考虑树状数组,由于这个问题的树并非标准二叉树,我们可以通过DFS重新编号并记数组low[MAX_N]high[MAX_N],分别储存各个节点的最大节点与最小节点,显然low[i]i节点的编号,而high[i]则是后代中最大的编号。

树状数组树状数组的sum(i)所求的是从1i之间的区间和,那么我们最终需要的答案自然是sum(high[i])-sum(low[i]-1)


代码

#include
#include
#include
using namespace std;
const int MAXN=100005;
struct Edge{int y,next;}w[2*MAXN];
int head[MAXN],c[2*MAXN],low[MAXN],high[MAXN];
int n,m,k,step=0,vst[MAXN],f[MAXN];
int Lowbit(int i){return i&(-i);}
int sum(int x)
{  int ret=0;
   for(int i=x;i>0;i-=Lowbit(i))ret+=c[i];
   return ret;
}
void Add(int x,int d)
{  for(int i=x;i<=n;i+=Lowbit(i))c[i]+=d;}
void AddEdge(int u,int v){w[k].y=v;w[k].next=head[u];head[u]=k++;}  //前向星建图
void DFS(int u)
{  int i;
   low[u]=++step;
   vst[u]=1;    //标记已经遍历的节点
   for(i=head[u];i!=-1;i=w[i].next)
      if(!vst[w[i].y])DFS(w[i].y);       //遍历儿子
   high[u]=step;
}
void Read()
{  int i,u,v;
   memset(head,-1,sizeof(head));
   memset(vst,0,sizeof(vst));
   memset(c,0,sizeof(c));
   scanf("%d",&n);
   k=1;
   for(i=1;i

扩展-树状数组

树状数组,以树状结构存储数组的和,更改与查找时间均为O(logN),主要操作为lowbit(),add(),sum()

原理如下

image

记目标数组为A[N],树状数组的构造如下:

C[1]=A[1]

C[2]=A[1]+A[2]

C[3]=A[3]

C[4]=A[1]+A[2]+A[3]+A[4]

C[5]=A[5]

C[6]=A[5]+[A6]

C[7]=A[7]

C[8]=A[1]+A[2]+A[3]+A[4]+A[5]+A[6]+A[7]+A[8]

转换成二进制后:

1=(0001) C[1]=A[1]

2=(0010) C[2]=A[1]+A[2]

3=(0011) C[3]=A[3]

4=(0100) C[4]=A[1]+A[2]+A[3]+A[4]

5=(0101) C[5]=A[5]

6=(0110) C[6]=A[5]+A[6]

7=(0111) C[7]=A[7]

8=(1000) C[8]=A[1]+A[2]+A[3]+A[4]+A[5]+A[6]+A[7]+A[8]

可以得出规律为 C[i]=A[i-2k+1]+A[i-2k+2]+...+A[i],其中k是i写成二进制数从低位到高位连续0的个数,可以验证i=8时k=3,符合上面的式子。


下面是树状数组的函数组成:

  1. lowbit(x)顾名思义,返回x最低位的1,也就是说lowbit(x)返回上面式子中的2k

    写成代码则是

    int lowbit(int x){
        return x&(-x);
    }
    

为什么可以通过lowbit(x)求出2x?核心操作在于按位与&,而(-x)对x进行取反加1,容易想到x二进制形式形如...10...0,那么补码则为...01...1 + 1,即...10...0,此处的1是第一个1并且在第k+1位上,高位上按位与结果全为0(取反),低位上全为0,故结果为2k

  1. 接下来是更新函数add(x,y),把A[x]的值更新y,如何实现?

    观察上图发现,更新A[1]需要向上更新C[1],C[2],C[4],C[8],写成二进制是C[(0001)],C[(0010)],C[(0100)],C[(1000)],不难发现需要更新的C[i]之间相差了lowbit(i),因此可以得出代码

    void add(int x,int y){
        for(int i=x;i<=n;i+=lowbit(i))
            tree[i]+=y;
    }
    
  2. 最后一个函数是求和函数sum(x),首先看

    S[1]=A[1]=C[1]

    S[2]=A[1]+A[2]=C[2]

    S[3]=A[1]+A[2]+A[3]=C[2]+C[3]

    S[4]=A[1]+A[2]+A[3]+A[4]=C[4]

    S[5]=A[1]+A[2]+A[3]+A[4]+A[5]=C[4]+C[5]

    S[6]=A[1]+A[2]+A[3]+A[4]+A[5]+A[6]=C[4]+C[6]

    S[7]=A[1]+A[2]+A[3]+A[4]+A[5]+A[6]+A[7]=C[4]+C[6]+C[7]

    S[8]=A[1]+A[2]+A[3]+A[4]+A[5]+A[6]+A[7]+A[8]=C[8]

    规律是什么?首先不难发现要求sum(x)必定会有一项C[x], 此外,不妨考虑树的一些特征,更新函数可以看成是叶子节点向根节点更新,那么求和则相反,可以看作从根向叶子求和,所以同样地,C[i]之间也相差了lowbit(i),因此代码如下

    int sum(int x){
        int ret=0;
        for(int i=x;i;i-=lowbit(i))
            ret+=C[i];
        return ret;
    }
    

至此,树状数组与POJ3221已经解决,后续有时间可能会写有关前向星建图的一些问题。

你可能感兴趣的:(POJ 3321)