POJ 3321—— Apple Tree(树状数组)

Apple Tree

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19160   Accepted: 5831

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

————————————————————分割线——————————

题目大意:

给定一个有n个节点的苹果树,树上初始每个节点有1个苹果。然后给出n-1条边。有两种操作:

1.如果节点x有苹果,就摘掉,否则就长苹果

2.求出以x节点为根的子树上的苹果数

思路:

将树形结构转变为线性结构——————方法dfs这棵树,某个节点第一次访问的编号和最后一次访问的编号之间的节点就是以该点为根的子树的所有子节点。

那么就可以套用BIT模板的改点求段.

用链表建立双向边,节点1肯定是根,dfs。

这题好像有个BUG,题目给出的N的范围是10万,但是我原来开10万+10wa了,后来改成100万就a了。  好神奇

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define M 1000000+10
using namespace std;
struct pp
{
    int v,w,next;
}edge[M];int tot,head[M],n,vis[M],c[M],app[M],cnt;
inline void addedge(int u,int v,int w,int *h)
{
    edge[tot].v=v;
    edge[tot].w=w;
    edge[tot].next=h[u];
    h[u]=tot++;
}

struct node
{
    int s,e;
}a[M];
void dfs(int u)
{
    a[u].s=cnt;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].v;
        if(vis[v]) continue;
        cnt++;
        dfs(v);
    }
    a[u].e=cnt;
}

void update(int x,int v)
{
    for(int i=x;i<=n;i+=i&-i){
        c[i]+=v;
    }
}
int getsum(int x)
{
    int sum=0;
    for(int i=x;i>0;i-=i&-i){
        sum+=c[i];
    }
    return sum;
}
int main()
{
    scanf("%d",&n);
    if(n==0) return 0;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    tot=0;
    for(int i=0;i<n-1;++i){
        int u,v;
        scanf("%d %d",&u,&v);
        addedge(u,v,0,head);
        addedge(v,u,0,head);
    }
    cnt=1;
    dfs(1);

    for(int i=1;i<=n;++i){
        app[i]=1;
    }
    for(int i=1;i<=n;++i){
        c[i]=i&-i;
    }

    int m,x;
    scanf("%d",&m);
    while(m--){
        getchar();
        char ch;int x;
        scanf("%c %d",&ch,&x);
        if(ch=='C'){
            int v;
            if(app[x]){v=-1;app[x]=0;}
            else{app[x]=1;v=1;}
            //printf("\t%d....%d\n",a[x].s,v);
            update(a[x].s,v);
        }
        if(ch=='Q'){
            printf("%d\n",getsum(a[x].e) - getsum(a[x].s-1));
        }
    }
    return 0;
}
时间: 2024-11-08 22:46:47

POJ 3321—— Apple Tree(树状数组)的相关文章

POJ 3321 Apple Tree (树状数组)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21191   Accepted: 6436 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

E - Apple Tree(树状数组+DFS序)

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

POJ3321 Apple Tree(树状数组)

先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio>#include<iostream>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#include<map>#include<queue>#include<vector>#

poj 3321:Apple Tree(树状数组,提高题)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 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

【树状数组】POJ 3321 Apple Tree

/** * @author johnsondu * @time 2015.8.25 20:04 * @problem POJ 3321 Apple Tree * @type Binary Index Tree * @description 从根节点开始,dfs遍历树,先访问的节点 * 记为beg, 从当前结点遍历访问到的最后的 * 一个节点,记为end.然后按照树状数组的 * 方法进行求解. * @url http://poj.org/problem?id=3321 */ #include <i

POJ 3321 Apple Tree (dfs+线段树)

题目大意: 修改树上的节点,然后求子树的和. 思路分析: dfs 重新编号,烂大街了... #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 100005 #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e using namespace std

POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

POJ - 3321 Apple Tree Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status 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 muc

HDU 3333 Turing Tree 树状数组 离线查询

题意: 给你一个数列,然后有n个查询,问你给定区间中不同数字的和是多少. 思路还是比较难想的,起码对于蒟蒻我来说. 将区间按照先右端点,后左端点从小到大排序之后,对于每个查询,我只要维护每个数字出现的最后一次就可以了(这个结论稍微想一下就可以证明是正确的). 然后就是简单的点更新,区间求和问题了- #include <cstdio> #include <cstring> #include <iostream> #include <map> #include

hdu 1541/poj 2352:Stars(树状数组,经典题)

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4052    Accepted Submission(s): 1592 Problem Description Astronomers often examine star maps where stars are represented by points on a plan

POJ 2892 Tunnel Warfare (树状数组+二分)

题目大意: 三个操作 D pos  将pos位置摧毁,让它和周围不相连. Q pos 问和pos 相连的有多少个村庄. R 修复最近摧毁的村庄. 思路分析: 树状数组记录这个区间有多少个1. 如果  [s-e] 有e-s+1个1 的话.那么这个区间是相连的. 这样的话,我们就可以用二分的办法求出与某个位置最大相连的数量. 还有这里二分 while(l<=r) { if(满足) { ans=mid; l=mid+1; } else r=mid-1; } #include <cstdio>