Problem 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
Source
题目意思: 给你n个坐标,然后当中随意一个点,假如有s个点的横坐标和纵坐标都不大于这个点。那么这个点的价值为s,
最后让你输出 价值从1~n-1,的点的个数
比赛的时候有一个队做过这个题,非常快做出来了。我纠结好久(太菜了%>_<%),一直想着给x排序。然后再给y排序,
希望得出什么规律。最后想到了给x排序后,推断这个点的价值。须要高速查询前面点(按x以排序)不比他大的的点的个数(数据非常大。易超时)。最终想到线段树(⊙o⊙)…,用线段树保存le~ri已经存在的个数
详细的思路说完了,上代码了
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) using namespace std; #define N 32005 struct stud1{ int le,ri; int va; }f[N*4]; //刚開始没有乘4,搞得答案一直对不上 struct stud{ int x,y; }s[N]; int ans[N]; int cmp(stud a,stud b) { if(a.x==b.x) return a.y<b.y; //记得x同样时排y return a.x<b.x; } void build(int pos,int le,int ri) { f[pos].le=le; f[pos].ri=ri; f[pos].va=0; if(le==ri) return ; int mid=MID(le,ri); build(L(pos),le,mid); build(R(pos),mid+1,ri); } void update(int pos,int le) { f[pos].va++; if(f[pos].le==le&&le==f[pos].ri) return ; int mid=MID(f[pos].le,f[pos].ri); if(mid>=le) update(L(pos),le); else update(R(pos),le); } int query(int pos,int le,int ri) { if(f[pos].le==le&&f[pos].ri==ri) return f[pos].va; int mid=MID(f[pos].le,f[pos].ri); if(mid>=ri) return query(L(pos),le,ri); else if(mid<le) return query(R(pos),le,ri); return query(L(pos),le,mid)+query(R(pos),mid+1,ri); } int main() { int i,n; while(~scanf("%d",&n)) { for(i=0;i<n;i++) scanf("%d%d",&s[i].x,&s[i].y); memset(ans,0,sizeof(ans)); sort(s,s+n,cmp); build(1,0,N-1); for(i=0;i<n;i++) { int x=query(1,0,s[i].y); ans[x]++; update(1,s[i].y); } for(i=0;i<n;i++) printf("%d ",ans[i]); } return 0; }