不想多说这是已经要改崩溃了。。。
Stars
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 46676 | Accepted: 20125 |
Description
Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.
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.

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
The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
Output
The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
Sample Input
5 1 1 5 1 7 1 3 3 5 5
Sample Output
1 2 1 1 0
题目大意:在一个平面直角坐标系内有若干个点,把不在某个点右方或上方的点的个数称为这个点的等级,求每个等级的点的数量。
思路:不得不说一开始并没有什么思路。。注意到有这么一句话:Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate. 对于我这种不仅编程蒟蒻连英语也蒟蒻的人竟然把他忽略了。。翻译一下发现是左边按递增序列给出,再画个图比对一下发现因为纵坐标是第一关键字,所以只需要统计横坐标比当前点小的点的个数就可以了。线段树供上。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAXN=32005;
int f[MAXN<<2],level[MAXN<<2];
void update(int root,int left,int right,int q)
{
f[root]++;
if (left==right)
{
return ;
}
int mid=(left+right)>>1;
if (q<=mid)
{
update(2*root,left,mid,q);
}
else
{
update(2*root+1,mid+1,right,q);
}
}
int query(int root,int left,int right,int qleft,int qright)
{
if (left==qleft && right==qright)
{
return f[root];
}
int mid=(left+right)>>1;
if (mid>=qright)
{
return query(2*root,left,mid,qleft,qright);
}
else if (mid<qleft)
{
return query(2*root+1,mid+1,right,qleft,qright);
}
else
{
int ans1,ans2;
ans1=query(2*root,left,mid,qleft,mid);
ans2=query(2*root+1,mid+1,right,mid+1,qright);
return ans1+ans2;
}
}
int main()
{
int n,x,y;
scanf("%d",&n);
memset(f,0,sizeof(f));
memset(level,0,sizeof(level));
for (int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
++x;
++level[query(1,1,MAXN,1,x)];
update(1,1,MAXN,x);
}
for (int i=0;i<n;++i)
{
printf("%d\n",level[i]);
}
return 0;
}
大量的常数优化。。TLE无数次。不得不用位运算取代乘除。而且要注意在update和query的时候要把边界带入MAXN或者4*n,比较之后发现MAXN较小,带入,否则无法查询。
昨天没写博客,今天尽量补上一章。