HDU 1556 Color the ball

简介: Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18404    Accepted Submission(s...

Color the ball

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18404    Accepted Submission(s): 9166


Problem Description
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
 

 

Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
 

 

Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
1 1 1
3 2 1
 
Author
8600
 
Source
典型的树状数组,更新区间查找点!
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN=100005;
 4 int tree[MAXN];
 5 int n;
 6 void add(int k,int num)//后面的所有的值得更新,不包括自身
 7 {
 8     while(k<=n)
 9     {
10         tree[k]+=num;// 增加某个元素的大小
11         k+=k&-k;
12     }
13 }
14 int read(int k)
15 {
16     int sum=0;
17     while(k)
18     {
19         sum+=tree[k];//统计k以前的总数,即求该点的染色次数
20         k-=k&-k;
21     }
22     return sum;
23 }
24 int main()
25 {
26     int x,y,i;
27     while(scanf("%d",&n)&&n)
28     {
29         memset(tree,0,sizeof(tree));
30         for(i=0;i<n;i++)
31         {
32             scanf("%d%d",&x,&y);
33             add(x,1);//插线段的话就在线段左端点加1
34             add(y+1,-1);//在右端点后面减1
35         }
36         for(i=1;i<n;i++)
37             printf("%d ",read(i));
38         printf("%d\n",read(n));
39     }
40     return 0;
41 }

 

目录
相关文章
|
10月前
|
Java
hdu-1312-Red and Black
hdu-1312-Red and Black
44 0
hdu 1312 Red and Black(BFS)
hdu 1312 Red and Black(BFS)
156 0
hdu 1312 Red and Black
一个人从@点出发,求他所能到达的'.'的数目,'#'不可走,@本身算1个点。 思路:搜索入门题。
169 0
|
机器学习/深度学习
HDOJ/HDU 1556 Color the ball(树状数组)
HDOJ/HDU 1556 Color the ball(树状数组)
112 0
HDOJ 1312 (POJ 1979) Red and Black
HDOJ 1312 (POJ 1979) Red and Black
123 0
|
测试技术
poj-1046-color me less
Description A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform jus...
967 0
[LeetCode] Smallest Rectangle Enclosing Black Pixels
This post shares very detailed explanation of how to use binary search to find the boundaries of the smallest rectangle that encloses the black pixels.
1297 0
|
人工智能 网络协议
HDOJ 1312 (POJ 1979) Red and Black
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black.
1124 0