Description
For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Input
Output
Sample Input
5 1 1 5 1 7 1 3 3 5 5
Sample Output
1 2 1 1 0
Hint
Source
#include<stdio.h>
#include<string.h>
int level[15005];
int a[33005];
struct ele
{
int left;
int right;
int sum;
}p[150000];
void build(int x,int y,int step)
{
//printf("1\n");
p[step].left=x;
p[step].right=y;
p[step].sum=0;
if(p[step].left==p[step].right)
return;
else
{
int Mid=(p[step].left+p[step].right)/2;
build(x,Mid,2*step);
build(Mid+1,y,2*step+1);
}
}
int sum(int x,int y,int step)
{
//printf("2\n");
if(p[step].left==x&&p[step].right==y)
return p[step].sum;
else
{
int Mid=(p[step].left+p[step].right)/2;
if(y<=Mid)
return sum(x,y,2*step);
else
if(x>=Mid+1)
return sum(x,y,2*step+1);
else
return sum(x,Mid,2*step)+sum(Mid+1,y,2*step+1);
}
}
void update(int x,int step)
{
//printf("3\n");
if(p[step].left==p[step].right)
p[step].sum+=1;//不能写成=1
else
{
int Mid=(p[step].left+p[step].right)/2;
if(x<=Mid)
update(x,2*step);
else
update(x,2*step+1);
p[step].sum=p[2*step].sum+p[2*step+1].sum;
}
}
int main()
{
int n;
int x,y,z;
int i;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
memset(level,0,sizeof(level));
build(1,32310,1);
for(i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
z=sum(1,x+1,1);
level[z]++;
update(x+1,1);
}
for(i=0;i<=n-1;i++)
printf("%d\n",level[i]);
}
return 0;
}