hdu-1556 树状数组
题目出处 http://acm.hdu.edu.cn/showproblem.php?pid=1556
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define MAXN 100048 int c[MAXN]; int n,t; inline int Lowbit(int x)//求lowbit { return x&(-x); } void update(int i,int val)//修改i节点 增加val值 { while(i>0) { c[i]+=val; i-=Lowbit(i); } } int sum(int x)//求sum(n)的和 { int sum=0; while(x<=n) { sum+=c[x]; x+= Lowbit(x); } return sum; } int main() { int a,b; while(~scanf("%d",&n)&&n) { memset(c,0,sizeof(c)); for(int i=1;i<=n;i++) { scanf("%d%d",&a,&b);//区间[a,b] update(b,1);//先将[1,b] +1 update(a-1,-1);//再将[1,a-1] -1; } for(int i=1;i<n;i++) { cout<<sum(i)<<' '; } cout<<sum(n)<<'\n'; } return 0; }