POJ2352 Stars (靜態二叉檢索樹)
阿新 • • 發佈:2017-09-16
ace oid code n) mes ios 個數 col names
https://vjudge.net/problem/POJ-2352
分析:
由於是按照y坐標的升序,y坐標向等的按x的升序的順序給出星星。那麽某個星星的等級數就是在他前面x坐標小於等於他的x坐標的星星的個數。
暴力的時間復雜度為n^2,超時
所以我們要記錄前面所有x坐標出現的次數。然後要求出[0,xi]出現的和。
這就是靜態二叉檢索樹 的標準問題了。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> usingnamespace std; const int maxx=32010; const int maxn=15010; int c[maxx]; int level[maxn]; int sum(int x) { int res=0; while(x) { res+=c[x]; x-=(x&-x); } return res; } void add(int x,int d) { while(x<=maxx) { c[x]+=d; x+=(x&(-x)); } }int main() { int x,y,n; scanf("%d",&n); memset(c,0,sizeof(c)); memset(level,0,sizeof(level)); for(int i=0;i<n;i++) { scanf("%d %d",&x,&y); level[sum(x+1)]++; add(x+1,1); } for(int i=0;i<n;i++) printf("%d\n",level[i]);return 0; }
POJ2352 Stars (靜態二叉檢索樹)