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 }

 

目录
相关文章
|
9月前
HDU - 1312 Red and Black(DFS)
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles. Write a program to count the number of black
57 0
|
机器学习/深度学习 Windows
Codeforces Round #748 (Div. 3) F - Red-Black Number (记忆化搜索)
Codeforces Round #748 (Div. 3) F - Red-Black Number (记忆化搜索)
76 0
CodeForces - 1469B Red and Blue (前缀和)
CodeForces - 1469B Red and Blue (前缀和)
73 0
hdu 1312 Red and Black(BFS)
hdu 1312 Red and Black(BFS)
118 0
hdu 1312 Red and Black
一个人从@点出发,求他所能到达的'.'的数目,'#'不可走,@本身算1个点。 思路:搜索入门题。
122 0
|
机器学习/深度学习
HDOJ/HDU 1556 Color the ball(树状数组)
HDOJ/HDU 1556 Color the ball(树状数组)
82 0
ZOJ1067 Color Me Less
复制代码#include <iostream> #include <cmath> #include <limits> using namespace std; const int MAXSIZE = 100; int pos[100];//记录对应的最小值所在位置 struct RGB {//颜.
1432 0