HDU 1556 Color the ball
来源:互联网 发布:狸窝视频剪辑软件 编辑:程序博客网 时间:2024/06/11 01:27
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,输入结束。
当N = 0,输入结束。
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input
31 12 23 331 11 21 30
Sample Output
1 1 13 2 1
思路:用线段树的模板做的,其中查询气球被染了多少次,另外用一个数组,记录每个位置的气球被染了多少次,用递归去查每一层的结点被染的次数,每一层结点都记录着从left到right被染的次数,因为染色的过程中只要left和right符合就不会往下面递归了。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct note
{
int left,right,sum;
int mid()
{
return (left+right)/2;
}
}segtree[100000*3];
int ans[100000*3];
#include<cstdio>
#include<cstring>
using namespace std;
struct note
{
int left,right,sum;
int mid()
{
return (left+right)/2;
}
}segtree[100000*3];
int ans[100000*3];
void build(int left,int right,int cur)
{
segtree[cur].sum=0;
segtree[cur].left=left;
segtree[cur].right=right;
if(left==right)
{
return ;
}
else
{
int mid=segtree[cur].mid();
build(left,mid,cur*2);
build(mid+1,right,cur*2+1);
}
}
void Find(int left,int right,int cur)
{
if(segtree[cur].left==left&&segtree[cur].right==right)
{
segtree[cur].sum++;
return ;
}
int mid=segtree[cur].mid();
if(right<=mid)
Find(left,right,cur*2);
else if(left>=mid+1)
Find(left,right,cur*2+1);
else
{
Find(left,mid,cur*2);
Find(mid+1,right,cur*2+1);
}
}
void add(int x)
{
int i;
for(i=segtree[x].left;i<=segtree[x].right;i++)
{
ans[i]+=segtree[x].sum;
}
if(segtree[x].left==segtree[x].right)
return ;
add(x*2);
add(2*x+1);
}
int main()
{
int n,a,b,i;
;
while(scanf("%d",&n) && n)
{
build(1,n,1);
for(i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
Find(a,b,1);
}
memset(ans,0,sizeof(ans));
add(1);
printf("%d",ans[1]);
for(i=2;i<=n;i++)
{
printf(" %d",ans[i]);
}
printf("\n");
}
}
0 0
- hdu 1556 color the ball
- hdu 1556Color 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
- HDU 1556 Color the ball
- HDU 1556 Color the ball
- HDU 1556 Color the ball
- __wakeup()函数失效引发漏洞(CVE-2016-7124)
- [leetcode]70. Climbing Stairs
- GPU高性能编程CUDA实战中的book.h
- Picasso和Glide的图片加载与缓存
- tenforflow学习笔记(七):cnn
- HDU 1556 Color the ball
- 剑指offer-链表倒数第k个节点
- Hibernate中对象的三种状态及相互转化
- mac 下安装mysql后启动问题
- 自定义圆形图片
- java的继承 inheritance
- Android 6.0 运行时权限处理
- 杭电2175
- ViewPager和SlidingPaneLayout的滑动冲突事件