记住树状数组的基本操作:http://hi.baidu.com/wuxyy/blog/item/a4ad808b59be8bd2fd1f109d.html
http://writeblog.csdn.net/PostEdit.aspx?entryId=6376639
int lowbit(int x) { return x&(x^(x–1)); } 利用机器补码的特点,这个函数可以改得更方便 int lowbit(int i) { return i&(-i); } 如果要把a[n]增加m,可以通过调用如下函数实现 void add(int i,int v) { while (i<=n) { a[i]+=v; i+=lowbit(i); } } 如果要统计a[1]到a[n]之间的和,可以通过调用如下函数实现 int sum(int i) { int s=0; while (i>0) { s+=a[i]; i-=lowbit(i); } return s; }
此题思路:将每个点定一个时间戳。比如题目中给的树dfs()之后,点1为1,6,点2为2,3,点3为4,5.这样,查询1时,统计1--6之间的apple,再将结果除以2即可。
#include<iostream> #include<cstdio> #include<string> using namespace std; #define maxn 1000005 int n,m; class map { public: int v,next; }; map g[maxn]; int head[maxn],cnt,c[maxn*2]; bool have[maxn]; class tree { public: int st,ed; }; tree st[maxn]; void dfs(int v) { st[v].st =++cnt; int i; for(i=head[v];i;i=g[i].next ) { dfs(g[i].v ); } st[v].ed =++cnt; } int lowbit(int x) { return x&(-x); } void add(int t,int v) { while(t<maxn) { c[t]+=v; t+=lowbit(t); } } int getsum(int t) { int s=0; while(t>0) { s+=c[t]; t-=lowbit(t); } return s; } int main() { int i,a,b,lsum,rsum; char ch; cnt=0; scanf("%d",&n); for(i=1;i<n;i++) { scanf("%d%d",&a,&b); g[++cnt].v =b; g[cnt].next =head[a]; head[a]=cnt; } cnt=0; dfs(1); memset(have,true,sizeof(have)); for(i=1;i<=n;i++) { add(st[i].st,1); add(st[i].ed,1); } scanf("%d",&m); getchar(); while(m--) { scanf("%c %d",&ch,&a); getchar(); if(ch=='Q') { lsum=getsum(st[a].st-1 ); rsum=getsum(st[a].ed ); printf("%d\n",(rsum-lsum)/2); } else if(ch=='C') { if(have[a]) { have[a]=false; add(st[a].st ,-1); add(st[a].ed ,-1); } else { have[a]=true; add(st[a].st ,1); add(st[a].ed ,1); } } } return 0; }
您还没有登录,请您登录后再发表评论
poj 2488——dfs深度优先遍历 //给行数列数,求问能否遍历,给出字典序的一种遍历
NULL 博文链接:https://128kj.iteye.com/blog/1745671
c表示有多少种珍珠 ai 表示第i种珍珠所需的数量 pi 表示第i种珍珠的价钱 每买一种珍珠都需要付额外的10 * pi的钱,便宜的珍珠可以用贵的珍珠来代替,求最少的钱的总数。
POJ 是“北京大学程序在线评测系统”(Peking University Online Judge)的缩写,是个提供编程题目的网站,兼容Pascal、C、C++、Java、Fortran、Python等多种语言。可以按照分类,在POJ上做题。
NULL 博文链接:https://128kj.iteye.com/blog/1744222
POJ 1006 源代码——中国剩余定理分析POJ 1006 源代码——中国剩余定理分析POJ 1006 源代码——中国剩余定理分析
NULL 博文链接:https://128kj.iteye.com/blog/1744555
NULL 博文链接:https://128kj.iteye.com/blog/1747400
这是一道很不错的题目,即可以用线段树做也可以用树状数组,可谓经典。不过当然了线段树是比较难搞,而树状数组是极其简洁的,构造很简单,下面就分别来介绍一下两种方法...
POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度
poj 2820 古代密码 http://poj.grids.cn/problem?id=2820 可直接运行
网上整理的一些poj刷题指南。 poj地址:http://poj.org
北大POJ2255-Tree Recovery 解题报告+AC代码
POJ中级图算法所有题目【解题报告+AC代码】 我的所有POJ解题报告 http://blog.csdn.net/lyy289065406/article/details/6642573
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem....
poj 2201 Cartesian Tree.md
ACM/icpc的练习题目分类,非常全面的关于poj题目的分类
NULL 博文链接:https://128kj.iteye.com/blog/1746750
北大POJ2525-Text Formalization【TrieTree】 解题报告+AC代码+测试数据 http://hi.csdn.net/!s/XW4FQ3
原理为:以数链思想,移动数组中的内容 使数组在没有扩充情况下,达成移动的效果 当然,有更简单的,大牛不要笑哦
相关推荐
poj 2488——dfs深度优先遍历 //给行数列数,求问能否遍历,给出字典序的一种遍历
NULL 博文链接:https://128kj.iteye.com/blog/1745671
c表示有多少种珍珠 ai 表示第i种珍珠所需的数量 pi 表示第i种珍珠的价钱 每买一种珍珠都需要付额外的10 * pi的钱,便宜的珍珠可以用贵的珍珠来代替,求最少的钱的总数。
POJ 是“北京大学程序在线评测系统”(Peking University Online Judge)的缩写,是个提供编程题目的网站,兼容Pascal、C、C++、Java、Fortran、Python等多种语言。可以按照分类,在POJ上做题。
NULL 博文链接:https://128kj.iteye.com/blog/1744222
POJ 1006 源代码——中国剩余定理分析POJ 1006 源代码——中国剩余定理分析POJ 1006 源代码——中国剩余定理分析
NULL 博文链接:https://128kj.iteye.com/blog/1744555
NULL 博文链接:https://128kj.iteye.com/blog/1747400
这是一道很不错的题目,即可以用线段树做也可以用树状数组,可谓经典。不过当然了线段树是比较难搞,而树状数组是极其简洁的,构造很简单,下面就分别来介绍一下两种方法...
POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度POj 1001源代码——高精度乘单精度
poj 2820 古代密码 http://poj.grids.cn/problem?id=2820 可直接运行
网上整理的一些poj刷题指南。 poj地址:http://poj.org
北大POJ2255-Tree Recovery 解题报告+AC代码
POJ中级图算法所有题目【解题报告+AC代码】 我的所有POJ解题报告 http://blog.csdn.net/lyy289065406/article/details/6642573
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem....
poj 2201 Cartesian Tree.md
ACM/icpc的练习题目分类,非常全面的关于poj题目的分类
NULL 博文链接:https://128kj.iteye.com/blog/1746750
北大POJ2525-Text Formalization【TrieTree】 解题报告+AC代码+测试数据 http://hi.csdn.net/!s/XW4FQ3
原理为:以数链思想,移动数组中的内容 使数组在没有扩充情况下,达成移动的效果 当然,有更简单的,大牛不要笑哦