POJ 3321 Apple Tree(DFS+树状数组)
来源:互联网 发布:如何通过淘宝客赚钱 编辑:程序博客网 时间:2024/04/29 03:27
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove
题目:给出一棵树,初始每个结点一个苹果,有修改操作,改变某个结点的状态,有查询操作,某棵子树的苹果数量
http://poj.org/problem?id=3321
首先DFS一次,给每个节点重新标号,并且给每个结点一个区间,表示他的子结点的标号区间。这样查询的话,就成了区间和了。
初始每个结点有苹果,所以树状数组的值便是能表示长度,即lowbit(i)
修改的时候,如果初始有苹果,则点更新,减1,否则点更新,加1
查询的话就是一个区间和。
一直以为这题只能树DP
#include<iostream> #include<cstdio> #include<map> #include<cstring> #include<cmath> #include<vector> #include<algorithm> #include<set> #include<string> #include<queue> #define inf 1600005 #define M 40 #define N 100005#define maxn 300005 #define eps 1e-7#define zero(a) fabs(a)<eps #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) #define pb(a) push_back(a) #define mp(a,b) make_pair(a,b) #define mem(a,b) memset(a,b,sizeof(a)) #define LL long long #define MOD 2012 #define lson step<<1#define rson step<<1|1#define sqr(a) ((a)*(a)) #define Key_value ch[ch[root][1]][0] #define test puts("OK"); #define pi acos(-1.0)#define lowbit(x) ((x)&(-(x)))#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; struct Edge{ int v,next;}e[N*2];int tot,start[N];void add(int u,int v){ e[tot].v=v;e[tot].next=start[u]; start[u]=tot++; e[tot].v=u;e[tot].next=start[v]; start[v]=tot++;}int l[N],r[N],id[N];int n;int idx;int s[N];int app[N];void dfs(int u,int pre){ id[u]=++idx; l[u]=idx; for(int i=start[u];i!=-1;i=e[i].next){ int v=e[i].v; if(v==pre) continue; dfs(v,u); } r[u]=idx;}void Update(int x,int val){ for(int i=x;i<=n;i+=lowbit(i)) s[i]+=val;}int sum(int x){ int ret=0; for(int i=x;i>0;i-=lowbit(i)) ret+=s[i]; return ret;}int main(){ freopen("in.txt","r",stdin); while(scanf("%d",&n)!=EOF){ tot=0;mem(start,-1); for(int i=1;i<n;i++){ int u,v; scanf("%d%d",&u,&v); add(u,v); } idx=0; dfs(1,0); mem(s,0); for(int i=1;i<=n;i++) s[i]=lowbit(i),app[i]=1; int q; scanf("%d",&q); while(q--){ char str[5]; int k; scanf("%s%d",str,&k); if(str[0]=='C') app[k]^=1,Update(id[k],app[k]?1:-1); else printf("%d\n",sum(r[k])-sum(l[k]-1)); } } return 0;}
- POJ 3321 Apple Tree(DFS+树状数组)
- POJ 3321 Apple Tree(DFS+树状数组)
- POJ 3321 Apple Tree (dfs + 树状数组)
- POJ 3321 Apple Tree (DFS + 树状数组)
- POJ 3321 Apple Tree DFS + 树状数组
- POJ 3321 Apple Tree(dfs+树状数组)
- poj 3321 Apple Tree (树状数组+DFS)
- POJ 3321 Apple Tree(树状数组+dfs)
- POJ Apple Tree (3321)(树状数组+dfs)
- poj 3321 Apple Tree (dfs+树状数组)
- POJ 3321 APPLE TREE(树状数组+DFS)
- poj - 3321 - Apple Tree(dfs+树状数组)
- poj 3321 Apple Tree DFS时间戳预处理+树状数组
- poj 3321 Apple Tree(dfs + 树状数组)
- POJ 3321 Apple Tree(树状数组+DFS)
- poj 3321 Apple Tree(树状数组+dfs)中等难度
- POJ 3321 Apple Tree(dfs序 + 树状数组)
- POJ 3321 Apple Tree 【树状数组 dfs记录时间戳】
- dom对象类型和js对像的类型
- Oracle 分页
- hibernate.jdbc.fetch_size 和 hibernate.jdbc.batch_size
- 洋奶粉也出问题
- [记录]oracle操作技巧记录
- POJ 3321 Apple Tree(DFS+树状数组)
- Windows 的多字节与宽字节的转换
- PHP操作Mongdb(3)----获取一个表集
- Android开发六:栈 ActivityGroup
- 编程书写规则
- 【Web Trick 3】常用的js字符串操作函数
- ORACLE视图添加备注
- [OpenWrt] WR703N修砖记 --- 刷机有风险, 折腾需谨慎
- 自定义一个Toast(技巧)