poj 2464

很有意思的题目,貌似用树状树状解更合适,但是还是用线段树代替了

#include<iostream> #include<vector> #include<algorithm> using namespace std; #define MAXN 200002 int ind_x[MAXN]; int ind_y[MAXN]; int sum_x[MAXN]; int sum_y[MAXN]; struct point { int x,y; }p[MAXN]; bool comp(point a,point b){ if(a.x<b.x) return true; else if(a.x==b.x&&a.y<b.y) return true; return false; } int binarySearch(int target,int opt,int n){ int low,mid,high; if(opt==0){ //x low=1;high=n; while(low<=high){ mid=(low+high)>>1; if(ind_x[mid]==target) return mid; else if(ind_x[mid]<target) low=mid+1; else high=mid-1; } return -1; } else{ low=1;high=n; while(low<=high){ mid=(low+high)>>1; if(ind_y[mid]==target) return mid; else if(ind_y[mid]<target) low=mid+1; else high=mid-1; } return -1; } } struct SegmentTree{ int left[MAXN],right[MAXN]; int num[MAXN]; inline void build(int k,int l,int r){ left[k]=l; right[k]=r; num[k]=0; if(l==r) return ; int m=(l+r)>>1; build(k*2,l,m); build(k*2+1,m+1,r); } inline void push_down(int k){ return ; } inline void push_up(int k){ num[k]=num[k*2]+num[k*2+1]; return ; } inline int query(int k,int l,int r){ if(l>r) return 0; if(l<=left[k]&&r>=right[k]){ return num[k]; } else{ push_down(k); int mid=(left[k]+right[k])>>1; int ret=0; if(l<=mid) ret+=query(k*2,l,r); if(r>mid) ret+=query(k*2+1,l,r); push_up(k); return ret; } } inline void insert(int k,int ind){ if(left[k]==right[k]) num[k]++; else{ push_down(k); int mid=(left[k]+right[k])>>1; if(ind<=mid) insert(k*2,ind); else insert(k*2+1,ind); push_up(k); return ; } } }sgt_x,sgt_y; struct node{int s,o;}; bool compN(node a,node b){ if(a.o>b.o) return true; else if(a.o==b.o&&a.s<b.s) return true; return false; } int main() { int n; while(scanf("%d",&n)&&n){ int i; for(i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y); sort(p,p+n,comp); int j; ind_x[1]=p[0].x; sum_x[0]=0; sum_x[1]=1; for(j=1,i=1;i<n;i++){ if(p[i].x!=p[i-1].x){ sum_x[j]+=sum_x[j-1]; ind_x[++j]=p[i].x; sum_x[j]=1; } else sum_x[j]++; } sum_x[j]+=sum_x[j-1]; int nx=j,ny; for(i=0;i<n;i++) ind_y[i+1]=p[i].y; sort(ind_y+1,ind_y+n+1); sum_y[0]=0; sum_y[1]=1; for(j=1,i=2;i<=n;i++) if(ind_y[i]!=ind_y[i-1]){ sum_y[j]+=sum_y[j-1]; ind_y[++j]=ind_y[i]; sum_y[j]=1; } else{ sum_y[j]++; } sum_y[j]+=sum_y[j-1]; ny=j; /********************************************* for(i=1;i<=nx;i++) cout<<ind_x[i]<<" "<<sum_x[i]-sum_x[i-1]<<endl; puts(""); for(i=1;i<=ny;i++) cout<<ind_y[i]<<" "<<sum_y[i]-sum_y[i-1]<<endl; puts(""); /*********************************************/ sgt_x.build(1,1,nx); sgt_y.build(1,1,ny); int stan=-1; vector<int> ollie; int ts,to; vector<node> vec; node u; for(i=0;i<n;i++){ int x=p[i].x; int y=p[i].y; int tmp,tmp2,tmp3; ts=to=0; x=binarySearch(x,0,nx); y=binarySearch(y,1,ny); ts+=sgt_y.query(1,1,y-1); to+=sum_y[y-1]-ts; tmp3=sum_y[y-1]-ts; tmp2=sgt_x.query(1,x,x); ts-=tmp2; tmp=sgt_y.query(1,y+1,ny); to+=tmp; tmp3=sum_x[nx]-sum_x[x]-tmp3; ts+=tmp3-((sum_y[y]-sum_y[y-1])-1-sgt_y.query(1,y,y)); sgt_x.insert(1,x); sgt_y.insert(1,y); /****************************************** cout<<p[i].x<<" "<<p[i].y<<endl; cout<<"stan: "<<ts<<" ollie: "<<to<<endl; cout<<endl; /******************************************/ u.s=ts;u.o=to; vec.push_back(u); if(i==n-1||p[i].x!=p[i+1].x){ sort(vec.begin(),vec.end(),compN); ts=vec[0].s; to=vec[0].o; vec.clear(); if(ts>stan){ stan=ts; ollie.clear(); ollie.push_back(to); } else if(ts==stan){ ollie.push_back(to); } } } printf("Stan: %d; Ollie:",stan); sort(ollie.begin(),ollie.end()); printf(" %d",ollie[0]); for(i=1;i<ollie.size();i++) if(ollie[i]!=ollie[i-1]) printf(" %d",ollie[i]); puts(";"); } }  

 

你可能感兴趣的:(struct,query,ini,insert,Build,UP)