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

                                                              Apple Tree
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 27470   Accepted: 8140

    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
    【分析】题意很简单,这里就不多说了。做法是先dfs编号,找出每个节点所包含的节点编号区间,然后就是树状数组的事了。
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 2e9
    #define met(a,b) memset(a,b,sizeof a)
    typedef long long ll;
    using namespace std;
    const int N = 2e5+5;
    const int M = 4e5+5;
    int n,m,tot=0,cnt=0;
    int head[N],x[N],y[N],r[N];
    int tree[N];
    struct EDG{
        int to,next;
    }edg[M];
    inline void addedg(int u,int v){
        edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
    }
    void add(int k,int num){
        while(k<=n){
            tree[k]+=num;
            //printf("####%lld
    ",tree[k]);
            k+=k&(-k);
        }
    }
    int Sum(int k){
        int sum=0;
        while(k>0){
            sum+=tree[k];
            k-=k&(-k);
        }
        return sum;
    }
    void dfs(int u){
        int v;
        x[u]=++cnt;
        for(int i=head[u];i!=-1;i=edg[i].next){
            v=edg[i].to;
            if(!x[v])dfs(v);
        }
        y[u]=cnt;
        return;
    }
    int main() {
        met(tree,0);met(head,-1);met(x,0);
        int apple[N];
        for(int i=1;i<N;i++)apple[i]=1;
        int u,v;
        char str[5];
        scanf("%d",&n);
        for(int i=1;i<n;i++){
            scanf("%d%d",&u,&v);
            addedg(u,v);addedg(v,u);
        }
        dfs(1);
        for(int i=1;i<=n;i++)add(i,1);
        scanf("%d",&m);
        while(m--){
            scanf("%s",str);
            scanf("%d",&u);
            if(str[0]=='C'){
                int l=x[u],rr=y[u];
                if(apple[l]){
                    apple[l]=0;
                    add(l,-1);
                }else {
                    apple[l]=1;
                    add(l,1);
                }
            }else {
                int ans=Sum(y[u])-Sum(x[u]-1);
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    第三部分 学习shell与shell scipt---第九章 vim程序编辑器
    端口映射与容器互联
    在web工程中如何查看编译后class路径
    Spring配置JDBC连接Orcale、MySql、sqlserver
    org.springframework.beans.factory.BeanNotOfRequiredTypeException错误
    windows系统中如何启动两个tomcat
    eclipse+maven搭建SSM框架
    windows下安装和配置多个版本的JDK
    java环境变量为1.7jdk为何在dos窗口中查看版本为1.8
    查看jdk、eclipse、tomcat的是32位还是64
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6090918.html
Copyright © 2011-2022 走看看