您的位置:首页 > 其它

hdu 1556 Color the ball(线段树+懒惰标记)

2013-03-31 16:01 369 查看


Color the ball

Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4961 Accepted Submission(s): 2624



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

Recommend

LL

思路:就是一道比较水的线段树,非常明显,我用上了懒惰节点。现在做就是为了重温一下线段树,一切都是自己重新码的。

#include<iostream>
#include<cstring>
#include<cstdio>
#define lson t<<1
#define rson t<<1|1
#define midl (l+r)/2
#define midr (l+r)/2+1
using namespace std;
const int mm=1e5+9;
class node
{
public:int l,r,lazy,ci;
}rt[mm*4];
int n;
void build(int t,int l,int r)
{ rt[t].l=l;rt[t].r=r;rt[t].lazy=rt[t].ci=0;
if(l==r)return;
build(lson,l,midl);build(rson,midr,r);
}
void update(int t,int l,int r,int num)
{
if(rt[t].l==l&&rt[t].r==r){rt[t].ci+=num;rt[t].lazy+=num;return;}
rt[lson].lazy+=rt[t].lazy;rt[rson].lazy+=rt[t].lazy;
rt[lson].ci+=rt[t].lazy;rt[rson].ci+=rt[t].lazy;
rt[t].lazy=0;
if(rt[lson].r>=r)update(lson,l,r,rt[t].lazy+num);
else if(rt[rson].l<=l)update(rson,l,r,rt[t].lazy+num);
else update(lson,l,rt[lson].r,rt[t].lazy+num),update(rson,rt[rson].l,r,rt[t].lazy+num);

}
int query(int t,int id)
{ int ret;
if(rt[t].l==rt[t].r&&rt[t].l==id)return rt[t].ci;
rt[lson].lazy+=rt[t].lazy;
rt[lson].ci+=rt[t].lazy;
rt[rson].ci+=rt[t].lazy;
rt[rson].lazy+=rt[t].lazy;
rt[t].lazy=0;
if(rt[lson].r>=id)ret=query(lson,id);
else ret=query(rson,id);
return ret;
}
int main()
{ int a,b;
while(scanf("%d",&n)&&n)
{ build(1,1,n);
for(int i=0;i<n;++i)
{
scanf("%d%d",&a,&b);
update(1,a,b,1);
}
printf("%d",query(1,1));
for(int i=2;i<=n;++i)
printf(" %d",query(1,i));
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: