Color the ball HDU - 1556 线段树区间更新点查询
2018-01-24 10:52
411 查看
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
Sample Output
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=100020;
struct node
{
int l,r,n;
}tree[M<<2];
int n;
int ans[M];
void build(int l,int r,int k)
{
tree[k].l=l;tree[k].r=r;
tree[k].n=0;
if(l==r)
return ;
int mid=(l+r)>>1;
build(l,mid,k<<1);
build(mid+1,r,k<<1|1);
}
void update(int l,int r,int k)
{
if(tree[k].l>=l&&tree[k].r<=r)
{
tree[k].n++;
return ;
}
int mid=(tree[k].l+tree[k].r)>>1;
if(l<=mid)
update(l,r,k<<1);
if(r>mid)
update(l,r,k<<1|1);
}
void add(int k)
{
if(tree[k].n)
for(int i=tree[k].l;i<=tree[k].r;i++)
ans[i]+=tree[k].n;
if(tree[k].r==tree[k].l)
return ;
add(k<<1|1);
add(k<<1);
}
int main()
{
int a,b;
while(scanf("%d",&n),n)
{
build(1,n,1);
memset(ans,0,sizeof(ans));
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
update(a,b,1);
}
add(1);
for(int i=1;i<=n;i++)
{
printf("%d%c",ans[i],i==n?'\n':' ');
}
}
return 0;
}
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
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=100020;
struct node
{
int l,r,n;
}tree[M<<2];
int n;
int ans[M];
void build(int l,int r,int k)
{
tree[k].l=l;tree[k].r=r;
tree[k].n=0;
if(l==r)
return ;
int mid=(l+r)>>1;
build(l,mid,k<<1);
build(mid+1,r,k<<1|1);
}
void update(int l,int r,int k)
{
if(tree[k].l>=l&&tree[k].r<=r)
{
tree[k].n++;
return ;
}
int mid=(tree[k].l+tree[k].r)>>1;
if(l<=mid)
update(l,r,k<<1);
if(r>mid)
update(l,r,k<<1|1);
}
void add(int k)
{
if(tree[k].n)
for(int i=tree[k].l;i<=tree[k].r;i++)
ans[i]+=tree[k].n;
if(tree[k].r==tree[k].l)
return ;
add(k<<1|1);
add(k<<1);
}
int main()
{
int a,b;
while(scanf("%d",&n),n)
{
build(1,n,1);
memset(ans,0,sizeof(ans));
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
update(a,b,1);
}
add(1);
for(int i=1;i<=n;i++)
{
printf("%d%c",ans[i],i==n?'\n':' ');
}
}
return 0;
}
相关文章推荐
- hdu 1556 Color the ball(线段树区间更新单点查询+懒惰标记)
- HDU-1556-Color the ball-线段树+区间更新+单点查询
- HDU 1556 Color the ball(线段树区间更新区间查询模板)
- HDU 1556 Color the ball【线段树区间更新,一次查询+数组模拟】
- hdu 1556 Color the ball(线段树 区间更新单点查询)
- HDU 1556 Color the ball [区间更新+单点查询]
- 【线段树II:区间修改+点查询】hdu 1556 Color the ball
- HDU 1556 Color the ball (一维树状数组,区间更新,单点查询)
- HDU 1556 Color the ball(线段树区间更新)
- HDU 1556 Color the ball(线段树区间更新)
- hdu 1556 Color the ball 线段树 区间更新
- HDU 1556 Color the ball 线段树 区间更新
- HDU 1556-Color the ball(线段树区间更新)
- hdu 1556 Color the ball(区间更新,查询点)
- HDU 1556 Color the ball 线段树更新区间查点
- HDu 1556 Color the ball【线段树&&树状数组】区间更新,单点查询
- HDU 1556 Color the ball(树状数组区间更新点查询)
- HDU 1556 Color the ball(线段树:区间更新)
- HDU - 1556 Color the ball —— 区间更新 单点查询
- HDU - 1556 Color the ball (线段树 + 区间更新)