Color the ball(hdu 1556)
树状数组的另一种应用领域,区间修改,单点查询,区间修改向下修改,单点查询向上查询
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 #define maxn 110000 7 int n,c[maxn]; 8 int lowbit(int x) 9 { 10 return x&(-x); 11 } 12 13 void add(int x,int d) 14 { 15 for(;x>0;x=x-lowbit(x)) 16 c[x]=c[x]+d; 17 } 18 19 int sum(int x) 20 { 21 int s=0; 22 for(;x<=n;x=x+lowbit(x)) 23 s=s+c[x]; 24 return s; 25 } 26 27 void add_seg(int l,int r,int d) 28 { 29 add(r,d); 30 add(l-1,-d); 31 } 32 int main() 33 { 34 int a,b; 35 while(scanf("%d",&n)==1&&n!=0) 36 { 37 memset(c,0,sizeof(c)); 38 for(int i=1;i<=n;i++) 39 { 40 scanf("%d%d",&a,&b); 41 add_seg(a,b,1); 42 } 43 for(int i=1;i<=n-1;i++) 44 printf("%d ",sum(i)); 45 printf("%d\n",sum(n)); 46 } 47 return 0; 48 }