POJ 3321- Apple Tree(标号+BIT)

题意:

给你一棵树,初始各节点有一个苹果,给出两种操作,x 表示若x节点有苹果拿掉,无苹果就长一个。

x查询以x为根的子树中有多少个苹果。

分析:

开始这个题无从下手,祖先由孩子的标号不能确定,就想能不能重新编号

,对与一棵树我们以先根序进行编号这就保证了一个子树在一个连续的区间内,然后就是BIT了。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 100010
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
struct edge{
    int v,next;
}e[N*2];
int used[N],bit[N],l[N],r[N],id,f[N],n,head[N*2];
int len;
void add_edge(int u,int v){
    e[len].v=v;
    e[len].next=head[u];
    head[u]=len++;
}
void add(int x,int d){
    while(x<=n){
        bit[x]+=d;
        x+=x&(-x);
    }
}
int sum(int x){
    int num=0;
    while(x>0){
        num+=bit[x];
        x-=x&(-x);
    }
    return num;
}
void dfs(int i){
    l[i]=++id;//根节点编号就是子树左边界
    used[i]=1;
    for(int j=head[i];j!=-1;j=e[j].next){
        if(!used[e[j].v]){
            dfs(e[j].v);
        }
    }
    r[i]=id;//最后一个子孙的编号右边界
}
int main()
{
    while(~scanf("%d",&n)){
        memset(bit,0,sizeof(bit));
        memset(f,0,sizeof(f));

        memset(used,0,sizeof(used));
        memset(head,-1,sizeof(head));
            int u,v;
            len=0;
        for(int i=0;i<n-1;++i){
            scanf("%d%d",&u,&v);
           add_edge(u,v);
           add_edge(v,u);
        }
        id=0;
        dfs(1);
        for(int i=1;i<=n;++i)
            add(i,1);
        int m,fork;
        char op[3];
        scanf("%d",&m);
        while(m--){
            scanf("%s%d",op,&fork);
            if(op[0]==‘C‘){
                if(f[fork]){
                    add(l[fork],1);
                    f[fork]=0;
                }
                else{
                    add(l[fork],-1);
                    f[fork]=1;
                }
            }
            else if(op[0]==‘Q‘){
                printf("%d\n",sum(r[fork])-sum(l[fork]-1));
            }
        }
    }
return 0;
}
时间: 2024-10-13 17:52:25

POJ 3321- Apple Tree(标号+BIT)的相关文章

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 (线段树 + 建树 + 思维转换)

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

【树状数组】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 【树状数组+建树】

题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34812 Accepted: 10469 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka like

POJ 3321 Apple Tree 【树形结构转变为线性结构+线段树OR树状数组】

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

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

POJ 3321 Apple Tree

题目链接:http://poj.org/problem?id=3321 解题思路:dfs加时间戳然后简单树状数组单点更新区间查询即可. 代码: 1 const int maxn = 1e5 + 5; 2 struct Edge{ 3 int to, next; 4 }; 5 Edge edges[maxn]; 6 int head[maxn], tot; 7 int st[maxn], ed[maxn], cnt; 8 int bit[maxn], status[maxn], n, m; 9 1

#5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25232   Accepted: 7503 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,树链剖分+树状数组。

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.