HDU 1556 线段树 lazy

Color the ball

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

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

code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+5;
int ans[maxn];
struct node
{
int l,r,addlazy;
}tree[maxn<<2];

void build (int l, int r, int id)
{
tree[id].l=l;
tree[id].r=r;
if(l==r)
{
tree[id].addlazy=0;
return ;
}
int mid;
mid=(l+r)/2;
build(l,mid,id<<1);
build(mid+1,r,id<<1|1);
}
void add(int i)
{
int j;
if(tree[i].addlazy)
{
for(j=tree[i].l;j<=tree[i].r;j++)
ans[j]+=tree[i].addlazy;
tree[i].addlazy=0;
}
if(tree[i].l==tree[i].r) return ;
add(i*2);
add(i*2+1);

}

void update( int l,int r,int id)
{
if(tree[id].l==l&&tree[id].r==r)
{
tree[id].addlazy++;
return ;
}
int mid;
mid=(tree[id].r+tree[id].l)/2;
if(l>=mid+1) update(l,r,id*2+1);
else if(r<=mid) update(l,r,id*2);
else
{update(l,mid,id*2); update(mid+1,r,id*2+1);}
}
int main( )
{
int t,a,b;
while(~scanf("%d",&t))
{
memset(ans,0,sizeof(ans));
if(!t) break;
build(1,t,1);
for(int i=1;i<=t;i++)
{
scanf("%d%d",&a,&b);
update(a,b,1);
}
add(1);
for(int i=1;i<=t;i++)
{
printf("%d",ans[i]);
if(i!=t) printf(" ");
}

printf("\n");
}
return 0;
}

线段树入门题。lazy存下区间更新,等到访问时再把lazy存值传递出来。

时间: 2024-10-16 04:51:24

HDU 1556 线段树 lazy的相关文章

HDU 1556 线段树或树状数组,插段求点

1.HDU 1556  Color the ball   区间更新,单点查询 2.题意:n个气球,每次给(a,b)区间的气球涂一次色,问最后每个气球各涂了几次. (1)树状数组 总结:树状数组是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有元素之和,但是每次只能修改一个元素的值. 这里改下思路可以用树状数组.在更新(a,b)时,向上更新,将a~n加1,b+1~n减1.查询点时,向下求和即可. #include<iostream> #include<cstr

hdu 1556 线段树

这个题其实有O(n)的算法,不过还是用线段树做了一遍,还写了个自认为不错的pushalldown函数,哈哈. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 100001; 7 int ans[N]; 8 9 struct Node 10 { 11 int l, r, add; 12 } node[N <&

hdu 1556(线段树之扫描线)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11361    Accepted Submission(s): 5659 Problem Description N个气球排成一排,从左到右依次编号为1,2

HDU 4902 线段树||暴力

给定一个序列,两种操作 1:把一段变成x. 2:把一段每个数字,如果他大于x,就变成他和x的gcd,求变换完后,最后的序列. 线段树解法:用lazy标记下即可,优化方法还是很巧妙的, Accepted 4902 515MS 3308K 1941 B C++ #include "stdio.h" #include "string.h" struct node { int l,r,x;// 在叶子节点代表值,树节点代表成端更新的lazy操作. }data[400010]

hdu 3974 线段树 将树弄到区间上

Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1647    Accepted Submission(s): 753 Problem Description There is a company that has N employees(numbered from 1 to N),every emplo

HDU 4893 线段树裸题

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2512    Accepted Submission(s): 751 Problem Description Recently, Doge got a funny birthday present from his new friend, Pro

HDU 4902 线段树(区间更新)

Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 353    Accepted Submission(s): 169 Problem Description There is an old country and the king fell in love with a devil. The devil alw

hdu 4893 线段树 --- 也是两个变 类似双标记

http://acm.hdu.edu.cn/showproblem.php?pid=4893 开始的时候,我按双标记,WA了一下午,搞不定,我是用的两个标记add--表示当前结点中有值发生变化,flag,斐波那契的懒惰标记,但是估计是我自己处理的有问题,一直不对 参考了别人的代码,写法还是很不错的,Add变量维护的是,完全变成Fibonacci的时候的和,---回头我再重新写一遍 #include <cstdio> #include <cstring> #include <a

线段树lazy标记??Hdu4902

Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 335    Accepted Submission(s): 159 Problem Description There is an old country and the king fell in love with a devil. The devil al