HDU 1541 Stars (線段樹),hdu1541
Problem DescriptionAstronomers 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.
InputThe 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.
OutputThe 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
51 15 17 13 35 5
Sample Output
12110
SourceUral 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 32005struct 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;}
一道線段樹入門的題 hdu3468 RE了 不知道什地方錯了 大牛看看
看不懂~!
菜鳥教,hdu3265,給個思路就行,謝了O(∩_∩)O,詳細見下
這個不需要二維線段樹。
用二維線段樹的話可能大材小用了,複雜度也會比較高。
該題只要使用掃描線+一維線段樹就行了。