标签:.com == problem ane print www date sample arch
1 /* 2 引入一个小的技巧,就是a的位置+1,b+1的位置-1,最后一个for循环,每一个数都要加上前面那个数的值即可。 3 4 */ 5 /* 6 #include <iostream> 7 #include <cstdio> 8 #include <cmath> 9 #include <cstring> 10 11 using namespace std; 12 13 int num[100000+10]; 14 15 int main (){ 16 int T,a,b,Max; 17 while (~scanf("%d",&T)){ 18 memset(num,0,sizeof(num)); 19 Max=0; 20 if (T==0) 21 break; 22 for (int i=1; i<=T; i++){ 23 scanf("%d%d",&a,&b); 24 num[a]++; 25 num[++b]--; 26 27 } 28 for (int i=1; i<=T; i++){ 29 num[i]+=num[i-1]; 30 if(i==1) 31 printf ("%d",num[i]); 32 else 33 printf(" %d",num[i]); 34 } 35 printf ("\n"); 36 } 37 return 0; 38 } 39 40 */ 41 #include <iostream> 42 #include <stdio.h> 43 #include <string.h> 44 #define lowbit(x) x&(-x) 45 using namespace std; 46 47 #define N 100000 48 int c[N]; 49 void update(int x,int y){ 50 for(int i=x;i<=N;i+=lowbit(i)) 51 c[i]+=y; 52 } 53 int sum(int x){ 54 int s=0; 55 for(int i=x;i>0;i-=lowbit(i)) 56 s+=c[i]; 57 return s; 58 } 59 60 int main(){ 61 int x,y,n; 62 while(~scanf("%d",&n)){; 63 memset(c,0,sizeof(c)); 64 for(int i=1;i<=n;i++){ 65 scanf("%d%d",&x,&y); 66 update(x,1); 67 update(y+1,-1); 68 } 69 for(int i=1;i<=n;i++){ 70 if(i==1) printf("%d",sum[i]); 71 else printf(" %d",sum(i)); 72 } 73 printf("\n"); 74 } 75 return 0; 76 }
hdoj 1556 Color the ball(树状数组)
标签:.com == problem ane print www date sample arch
原文地址:http://www.cnblogs.com/z-712/p/7300862.html