Color the ball
来源:互联网 发布:编程语言薪资排行 编辑:程序博客网 时间:2024/05/16 07:28
Color the ball
TimeLimit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K(Java/Others)Total Submission(s): 3062 Accepted Submission(s):1735
Problem Description
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数ab(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
3 1 1 2 2 33 3 1 1 1 2 1 3 0
Sample Output
1 1 1 3 21
代码:
#include<stdio.h>
#include<math.h>
struct node
{
intl,r,color;
};
node a[500000];
int ans;
void creattree(int i,int x,int y)
{
intmid;
a[i].l=x;
a[i].r=y;
a[i].color=0;
if(x==y)return;
else{
mid=(x+y)/2;
creattree(i*2,x,mid);
creattree(i*2+1,mid+1,y);
}
}
void update(int i,int x,int y)
{
intmid;
if(a[i].l==x&& a[i].r==y)
{
a[i].color++;
return ;
}
mid=(a[i].l+a[i].r)/2;
if(y<=mid)update(i*2,x,y);
elseif(x>mid)update(i*2+1,x,y);
else
{
update(i*2,x,mid);
update(i*2+1,mid+1,y);
}
}
void count(int i,int ii) // 由于更新的函数 只记录 a到b区间被color 一次
{ // 因此对于每个气球被涂抹了多少次需要 从线段树的第一个一直搜索到具体的i
intmid; //把所经过的线段记录的color值相加即为所求
if(a[i].l==ii &&a[i].r==ii){ans+=a[i].color;return;}
mid=(a[i].l+a[i].r)/2;
if(ii<=mid){ans+=a[i].color;count(i*2,ii);}
else{ans+=a[i].color;count(i*2+1,ii);}
}
int main()
{
intn,x,y,i;
while(1)
{
scanf("%d",&n);
if(n==0)break;
creattree(1,1,n);
for(i=0;i<n;i++)
{//
scanf("%d%d",&x,&y);
update(1,x,y);
}
for(i=1;i<=n-1;i++)
{
ans=0; count(1,i);
printf("%d ",ans);
}
ans=0;count(1,i);
#include<math.h>
struct node
{
};
node a[500000];
int ans;
void creattree(int i,int x,int y)
{
}
void update(int i,int x,int y)
{
}
void count(int i,int ii) // 由于更新的函数 只记录 a到b区间被color 一次
{ // 因此对于每个气球被涂抹了多少次需要 从线段树的第一个一直搜索到具体的i
}
int main()
{