标签:des style blog http color io os ar for
大意:
Input
Output
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 100005; 7 int num[maxn]; 8 9 int main() { 10 int n; 11 int a, b; 12 while(scanf("%d",&n) && n) { 13 memset(num, 0, sizeof(num)); 14 for(int i = 1; i <= n; i++) { 15 scanf("%d %d",&a, &b); 16 num[a]++; num[b + 1]--; 17 } 18 int c = 0; 19 int sum = 0; 20 for(int i = 1; i <= n; i++) { 21 sum += num[i]; 22 printf(c++?" %d" : "%d", sum); 23 } 24 puts(""); 25 } 26 return 0; 27 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 100005; 7 8 int c[maxn]; 9 10 int lowbit(int x) { 11 return x & ( - x ); 12 } 13 14 int n; 15 void Add(int i, int num) { 16 while(i <= n) { 17 c[i] += num; 18 i += lowbit(i); 19 } 20 } 21 22 int Sum(int i) { 23 int sum = 0; 24 while(i > 0) { 25 sum += c[i]; 26 i -= lowbit(i); 27 } 28 return sum; 29 } 30 31 int main() { 32 int a, b; 33 while(scanf("%d",&n) && n) { 34 memset(c, 0, sizeof(c)); 35 for(int i = 1; i <= n; i++) { 36 scanf("%d %d",&a, &b); 37 Add(a, 1); 38 Add(b + 1, -1); 39 } 40 for(int i = 1; i <= n; i++) { 41 printf(i == 1 ? "%d" : " %d", Sum(i)); 42 } 43 puts(""); 44 } 45 return 0; 46 }
HDU1556Color the ball【标号法||树状数组】
标签:des style blog http color io os ar for
原文地址:http://www.cnblogs.com/zhanzhao/p/3982098.html