您的位置:首页 > 其它

HDU 1556 Color the ball

2016-08-12 19:50 225 查看
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

HDU 2006-12 Programming Contest

给n个气球 编号 1~n 然后进行n次染色

每次给 a b两个值 表示染从a到b之间的全部气球

最后输出每个气球的染色次数

 

 树状数组和线段树都可以做 

树状数组:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const int maxn = 1e5 + 5;

int a[maxn], n;
int lowbit(int x)
{
return x & (-x);
}
void update(int x, int k)
{
while (x <= n){
a[x] += k;
x += lowbit(x);
}
}
int sum(int x)
{
int res = 0;
while (x){
res += a[x];
x -= lowbit(x);
}
return res;
}
int main(void)
{
//	freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
int i, x, y;
while (cin >> n && n)
{
memset(a, 0, sizeof(a));
for (i = 1; i <= n; i++){
cin >> x >> y;
update(x, 1);  //  这里的+1会把第x到n个气球都加一次 会加多
update(y+1, -1); //  	因此这里的-1是把上面加多的减掉
}
for (i = 1; i < n; i++)	// 为什么sum(i)就是答案 ??
printf("%d ", sum(i)); // 因为在update时  把更新区间后面的点-1了 现在往前加加回来
printf("%d\n", sum(n));
}

return 0;
}


线段树:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int, int> P;
const int maxn = 1e5 + 5;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1

int add[maxn<<2];  // 只要更新add[]就行了
int ball[maxn], dex;
void Build(int l, int r, int rt)
{
add[rt] = 0;
if (l == r)
return;
int mid = (l + r) >> 1;
Build(lson);
Build(rson);
}
void pushdown(int rt)
{
if (add[rt])
{
add[rt<<1] += add[rt];
add[rt<<1|1] += add[rt];
add[rt] = 0;
}
}
void update(int L, int R, int l, int r, int rt)
{
if (L<=l && r<=R){
add[rt] += 1;
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
if (L <= mid) update(L, R, lson);
if (R > mid)  update(L, R, rson);
}
void PushAll(int l, int r, int rt)
{
if (l == r){
ball[++dex] = add[rt];
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
PushAll(lson);
PushAll(rson);
}
int main(void)
{
//	freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
int n, i, j, a, b;
while (cin >> n && n)
{
Build(1, n, 1);
for (i = 1; i <= n; i++){
scanf("%d %d", &a, &b);
update(a, b, 1, n, 1);
}
dex = 0;
PushAll(1, n, 1);
printf("%d", ball[1]);
for (i = 2; i <= n; i++)
printf(" %d", ball[i]);
printf("\n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: