摘要: 如果给定一个数组,要你求里面所有数的和,一般都会想到累加。但是当那个数组很大的时候,累加就显得太耗时了,时间复杂度为O(n),并且采用累加的方法还有一个局限,那就是,当修改掉数组中的元素后,仍然要你求数组中某段元素的和,就显得麻烦了。所以我们就要用到树状数组,他的时间复杂度为O(lgn),相比之下就快得多。下面就讲一下什么是树状数组:一般讲到树状数组都会少不了下面这个图:下面来分析一下上面那个图看能得出什么规律:据图可知:c1=a1,c2=a1+a2,c3=a3,c4=a1+a2+a3+a4,c5=a5,c6=a5+a6,c7=a7,c8=a1+a2+a3+a4+a5+a6+a7+a8,c9= 阅读全文
posted @ 2012-11-22 16:39 紫忆 阅读(371) 评论(0) 推荐(0) 编辑
摘要: http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=15025#include<iostream>#include<algorithm>using namespace std;struct node{ __int64 f,v;}str[500000];__int64 c[500000],n,t[500000];int cmp(const node &a,const node &b){ if(a.f<b.f) return 1; else return 0;}__int64 阅读全文
posted @ 2012-11-22 16:31 紫忆 阅读(302) 评论(0) 推荐(0) 编辑
摘要: http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=10588思路:先对E进行降序排序,在E相等的情况下,对S进行升序排序。然后用树状数组统计即可#include<iostream>#include<algorithm>using namespace std;struct node { int s,e,k;}t[100005];int c[100005];int cmp(const node &a,const node &b){ if(b.e<a.e) return 1; 阅读全文
posted @ 2012-11-22 16:26 紫忆 阅读(167) 评论(0) 推荐(0) 编辑
摘要: http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=11104此题比较坑爹,离散化中,相等的数进行离散化后也需要相等对于相等数据的离散化,可以采用赋值相同的值来处理对于相等数据的离散化,可以采用赋值相同的值来处理#include<iostream>#include<algorithm>using namespace std;__int64 c[70000],n,t[70000];struct node{ __int64 v,f;}s[70000];int cmp(const node & 阅读全文
posted @ 2012-11-22 16:20 紫忆 阅读(364) 评论(0) 推荐(0) 编辑
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=2688#include<algorithm> using namespace std;const int maxn=10005;const int maxm=3000005;int c[maxn],a[maxm],n;int sum(int x){ int s=0; while(x>=1) { s+=c[x]; x-=x&(-x); } return s;}void updata(int i){ while(i<=maxn-1) { c[i]++; i+=i&(-i); 阅读全文
posted @ 2012-11-22 16:12 紫忆 阅读(223) 评论(0) 推荐(0) 编辑
摘要: 1 #include<iostream> 2 using namespace std; 3 int a[1010][1010],c[1010][1010],n=1005; 4 int lowbit(int x) 5 { 6 return x&(-x); 7 } 8 int sum(int x,int y) 9 { 10 int i,j,sum=0; 11 for(i=x;i>0;i-=lowbit(i)) 12 { 13 for(j=y;j>0;j-=lowbit(j)) 14 { 15 ... 阅读全文
posted @ 2012-11-22 16:04 紫忆 阅读(194) 评论(0) 推荐(0) 编辑
摘要: 二维树状数组:同样不要忘记c的初始化,modify的功能是改变元素(x,y),sum的功能则是求从元素(1,1)开始到(x,y)的总和,同样,可以求出任意一个子矩阵内的所有元素之和,即sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1)intlowbit(intx){returnx&(-x);}voidmodify(intx,inty,intdelta){inti,j;for(i=x;i<=N;i+=lowbit(i)){for(j=y;j<=N;j+=lowbit(j)){c[i][j]+=delta;}}}intsum( 阅读全文
posted @ 2012-11-22 16:01 紫忆 阅读(158) 评论(0) 推荐(0) 编辑
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1541题意:给定一些点,求在这点左下方的点的个数;思路:此题可以用二维树状数组做,但仔细看题,可以得到给出的y是从小到大的,这样,只需对x进行操作,进而转换为在x左边的点有多少个代码:#include<iostream>using namespace std;int a[32005],c[32005],n=32005;int lowbit(int x){ return x&(-x);}int sum(int x){ int sum=0; while(x>0) { sum+=c[x]; 阅读全文
posted @ 2012-11-22 15:59 紫忆 阅读(389) 评论(0) 推荐(0) 编辑
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1394题意:求最小逆序数#include<iostream>using namespace std;int c[5005],n,a[5005];int lowbit(int x){ return x&(-x);}int sum(int x){ int sum=0; while(x>0) { sum+=c[x]; x-=lowbit(x); } return sum;}void inster(int x,int i){ while(x<=n) { c[x]+=i; x+=lowb 阅读全文
posted @ 2012-11-22 15:52 紫忆 阅读(254) 评论(0) 推荐(0) 编辑
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=2838思路:对于数据3,4,1,2先按顺序建树,updata(a[i],a[i]),s+=a[i];当i=3时,1前面有3,4比1大,这时,先求出比1等于或小的元素和,在用s-sum(1),结果为,1前面比1大的元素和。代码:View Code 1 #include<iostream> 2 using namespace std; 3 __int64 a[100005],c[100005],d[100005],n; 4 int lowbit(int x) 5 { 6 return x&(- 阅读全文
posted @ 2012-11-22 15:48 紫忆 阅读(238) 评论(0) 推荐(0) 编辑
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=2689代码:#include<iostream>using namespace std;int c[1010],a[1010],n;int lowbit(int x){ return x&(-x);}int sum(int x){ int sum=0; while(x>0) { sum=sum+c[x]; x-=lowbit(x); } return sum;}void inster(int x,int i){ while(x<=n) { c[x]+=i; x+=lowbit( 阅读全文
posted @ 2012-11-22 15:32 紫忆 阅读(250) 评论(0) 推荐(0) 编辑
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1556代码:#include<iostream>int n,c[100003];int lowbit(int x){ return x&(-x);}int sum(int x){ int sum=0; while(x>0) { sum+=c[x]; x-=lowbit(x); } return sum;}void inster(int x,int i){ while(x<=n) { c[x]+=i; x+=lowbit(x); }}int main(){ int a,b,i; 阅读全文
posted @ 2012-11-22 15:27 紫忆 阅读(869) 评论(0) 推荐(0) 编辑
摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1166代码:#include<iostream>using namespace std;int n,a[50005],c[50005];int lowbit(int x){ return x&(-x);}int sum(int x){ int sum=0; while(x>0) { sum+=c[x]; x=x-lowbit(x); } return sum;}void inster(int i,int j){ while(i<=n) ... 阅读全文
posted @ 2012-11-22 15:23 紫忆 阅读(170) 评论(0) 推荐(0) 编辑
摘要: Ping pongTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2242Accepted Submission(s): 823Problem DescriptionN(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill r 阅读全文
posted @ 2012-11-22 15:18 紫忆 阅读(534) 评论(0) 推荐(0) 编辑