zoukankan      html  css  js  c++  java
  • POJ3321 Apple Tree (树状数组)

    Apple Tree
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 16180   Accepted: 4836

    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
    题目大意级是说,给你一颗树,最初每个节点上都有一个苹果,有两种操作:修改(即修改某一个节点,修改时这一个节点苹果从有到无,或从无到有)和查询(查询某一个节点他的子树上有多少个苹果)。
    由于此题数据比较大(N<=10^5),而且不是标准的二叉树,所以这里我们队每一个节点重新编号,另外为每一个节点赋一个左值和一个右值,表示这个节点的管辖范围。


    上图也就是DFS搜索的时候做标记的过程,这样新的编号为1~6的节点所管辖的范围分别就是[1,6] [2,4] [3,3] [4,4] [5,6] [6,6],其中左边的是左值,右边的是右值,节点1的区间是[1,6],正好这棵子树有6个节点,其他也一样
    那我们吧新的节点放进树状数组时


    那我们求出每一个节点从1~左值的和 和 1~右值的和 他们的差就是这个节点的子树的所有的和(即这棵子树苹果数目)

    可以百度下看看树状数组的实现

    最后每输入一组数据就进行依次操作就可以了

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <vector>
     4 #define MAXN 100005
     5 #define mem(a) memset(a, 0, sizeof(a))
     6 using namespace std;
     7 
     8 int TreeArray[MAXN], Left[MAXN], Right[MAXN], Fork[MAXN];
     9 typedef vector<int> Ve;
    10 vector<Ve>Edge(MAXN);
    11 int N,M;
    12 int key;
    13 
    14 void init()//初始化数组和
    15 {
    16     mem(Left);  mem(Right);
    17     mem(Fork);  mem(TreeArray);
    18     for(int i=0;i<MAXN;i++)Edge[i].clear();
    19 }
    20 
    21 void DFS(int node)//为每一个node添加一个左值和右值,表示这个节点所
    22 {
    23     Left[node] = key;
    24     for(int i=0;i<Edge[node].size();i++)
    25     {
    26         key+=1;
    27         DFS(Edge[node][i]);
    28     }
    29     Right[node] = key;
    30 }
    31 
    32 int LowBit(int x)//返回的是2^k
    33 {
    34     return x & (x ^ (x-1));
    35 }
    36 
    37 void Edit(int k, int num)//修改节点k,如果是添加一个,代入1,删除一个代入-1
    38 {
    39     while(k <= N)
    40     {
    41         TreeArray[k] += num;
    42         k += LowBit(k);
    43     }
    44 }
    45 
    46 int GetSum(int k)//得到1...k的和
    47 {
    48     int sum = 0;
    49     while(k>=1)
    50     {
    51         sum += TreeArray[k];
    52         k -= LowBit(k);
    53     }
    54     return sum;
    55 }
    56 
    57 void ReadDataAndDo()
    58 {
    59     int a,b;
    60     char ch;
    61     for(int i=1;i<N;i++)//输入a,b把边存放在容器里面
    62     {
    63         scanf("%d%d", &a, &b);
    64         Edge[a].push_back(b);
    65     }
    66     key = 1;    DFS(1);//为每一个节点对应一个左边界和右边界,他自己就存放在左边界里面,而它的管辖范围就是左边界到右边界
    67     for(int i=1;i<=N;i++)
    68     {
    69         Fork[i] = 1;//最初每个Fork上都有一个苹果
    70         Edit(i,1);//同时更新树状数组的值
    71     }
    72     scanf("%d%*c", &M);
    73     for(int i=0;i<M;i++)
    74     {
    75         scanf("%c %d%*c", &ch, &b);
    76         if(ch == 'Q')//b的子树就是[Left[b], right[b]]
    77         {
    78             printf("%d
    ", GetSum(Right[b]) - GetSum(Left[b]-1));
    79         }
    80         else
    81         {
    82             if(Fork[b]) Edit(Left[b],-1);//由于每个节点的编号就是它的左值,所以直接修改左节点
    83             else Edit(Left[b],1);
    84             Fork[b] = !Fork[b];//变为相反的状态
    85         }
    86     }
    87 }
    88 
    89 int main()
    90 {
    91     while(~scanf("%d", &N))
    92     {
    93         init();
    94         ReadDataAndDo();
    95     }
    96     return 0;
    97 }


  • 相关阅读:
    Leetcode#104 Maximum Depth of Binary Tree
    Leetcode#102 Binary Tree Level Order Traversal
    js 实时显示字数
    js获取链接参数
    DIV+CSS左右列高度自适应问题
    css 背景透明,文字不透明
    css position的值
    从头搭建vue项目
    vuejs怎么在服务器部署?
    windows下nginx安装、配置与使用
  • 原文地址:https://www.cnblogs.com/gj-Acit/p/3236843.html
Copyright © 2011-2022 走看看