• poj 3321 Apple Tree dfs序+线段树


    Apple Tree
    Time Limit: 2000MS   Memory Limit: 65536K
         

    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
    "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
    "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
    

    Source

    POJ Monthly--2007.08.05, Huang, Jinsong
    题意:给你一颗苹果树,以1为根,问你这棵子树上的苹果有多少个,q询问,c修改(没有苹果挂上,有就摘下);
    思路:dfs序基础题;
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    const int N=1e5+10,M=1e6+10,inf=1e9+10;
    const ll INF=1e18+10,mod=2147493647;
    int tree[N<<2];
    void build(int l,int r,int pos)
    {
        if(l==r)
        {
            tree[pos]=1;
            return;
        }
        int mid=(l+r)>>1;
        build(l,mid,pos<<1);
        build(mid+1,r,pos<<1|1);
        tree[pos]=tree[pos<<1]+tree[pos<<1|1];
    }
    void update(int l,int r,int pos,int p,int c)
    {
        if(l==r&&l==p)
        {
            tree[pos]=c;
            return;
        }
        int mid=(l+r)>>1;
        if(p<=mid)
        update(l,mid,pos<<1,p,c);
        if(p>mid)
        update(mid+1,r,pos<<1|1,p,c);
        tree[pos]=tree[pos<<1]+tree[pos<<1|1];
    }
    int query(int L,int R,int l,int r,int pos)
    {
        if(L<=l&&r<=R)
        {
            return tree[pos];
        }
        int mid=(l+r)>>1;
        int ans=0;
        if(L<=mid)
            ans+=query(L,R,l,mid,pos<<1);
        if(R>mid)
            ans+=query(L,R,mid+1,r,pos<<1|1);
        return ans;
    }
    struct is
    {
        int v,nex;
    }edge[N<<1];
    int head[N<<1],edg;
    int in[N],out[N],tot;
    int n,p;
    void init()
    {
        memset(tree,0,sizeof(tree));
        memset(head,-1,sizeof(head));
        edg=0;
        tot=0;
    }
    void add(int u,int v)
    {
        edg++;
        edge[edg].v=v;
        edge[edg].nex=head[u];
        head[u]=edg;
    }
    void dfs(int u,int fa)
    {
        in[u]=++tot;
        for(int i=head[u];i!=-1;i=edge[i].nex)
        {
            int v=edge[i].v;
            if(v==fa)continue;
            dfs(v,u);
        }
        out[u]=tot;
    }
    char ch[10];
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            init();
            for(int i=1;i<n;i++)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                add(u,v);
                add(v,u);
            }
            dfs(1,-1);
            build(1,n,1);
            int q;
            scanf("%d",&q);
            while(q--)
            {
                int p;
                scanf("%s%d",ch,&p);
                if(ch[0]=='Q')
                {
                    printf("%d
    ",query(in[p],out[p],1,n,1));
                }
                else
                {
                    int x=query(in[p],in[p],1,n,1);
                    if(x)
                        update(1,n,1,in[p],0);
                    else
                        update(1,n,1,in[p],1);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    IE8兼容
    游标
    WARN No appenders could be found for logger (org.springframework.orm.hibernate3.support.OpenSessionInViewFilter)
    sql server 备份数据
    JS页面打印
    jQuery Mobile 入门教程
    jquerymobile入门(文件引用+多页面)
    定位和可见性
    二月份总结
    mailto用法
  • 原文地址:https://www.cnblogs.com/jhz033/p/6135098.html
Copyright © 2020-2023  润新知