首页 > 代码库 > HDU 1541 Stars (线段树)
HDU 1541 Stars (线段树)
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
Ural Collegiate Programming Contest 1999
题目意思: 给你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\n",ans[i]); } return 0; }
HDU 1541 Stars (线段树)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。