标签:
二维线段树,写起来比较繁琐,难度并不大
#include<iostream> #include<cstdio> #define maxn 222 using namespace std; int h,hh; double a,b; struct stus { int l,r; double max; }; struct stu { int l,r; stus root[1111*4]; }; stu mapp[maxn*4]; void build2(int id,int l,int r,int count) { mapp[id].root[count].l=l; mapp[id].root[count].r=r; mapp[id].root[count].max=-1.0; if(l==r) return ; int mid=(l+r)/2; build2(id,l,mid,count*2); build2(id,mid+1,r,count*2+1); } void build(int l,int r,int ll,int rr,int count) { mapp[count].l=l; mapp[count].r=r; build2(count,ll,rr,1); if(l==r) return; int mid=(l+r)/2; build(l,mid,ll,rr,count*2); build(mid+1,r,ll,rr,count*2+1); } void updata2(int id,int count) { mapp[id].root[count].max=max(mapp[id].root[count].max,b); if(mapp[id].root[count].l==mapp[id].root[count].r) return; int mid=(mapp[id].root[count].l+mapp[id].root[count].r)/2; if(a<=mid) updata2(id,count*2); else updata2(id,count*2+1); } void updata(int count) { updata2(count,1); if(mapp[count].l==mapp[count].r) return; int mid=(mapp[count].l+mapp[count].r)/2; if(h<=mid) updata(count*2); else updata(count*2+1); } double que2(int l,int r,int id,int count) { //cout<<l<<"~"<<r<<endl; if(mapp[id].root[count].l==l&&mapp[id].root[count].r==r) return mapp[id].root[count].max; int mid=(mapp[id].root[count].l+mapp[id].root[count].r)/2; if(r<=mid) return que2(l,r,id,count*2); else if(l>=mid+1) return que2(l,r,id,count*2+1); else return max(que2(l,mid,id,count*2),que2(mid+1,r,id,count*2+1)); } double que(int l,int r,int count) { if(mapp[count].l==l&&mapp[count].r==r) return que2(a,b,count,1); int mid=(mapp[count].l+mapp[count].r)/2; if(r<=mid) return que(l,r,count*2); else if(l>=mid+1) return que(l,r,count*2+1); else return max(que(l,mid,count*2),que(mid+1,r,count*2+1)); } int main() { int n; while(cin>>n&&n) { build(100,200,0,1000,1); string cmd; for(int i=0;i<n;i++) { cin>>cmd; if(cmd=="I") { cin>>h>>a>>b; a*=10; updata(1); } else { cin>>h>>hh>>a>>b; if(h>hh) swap(h,hh);//坑 a*=10;b*=10; if(a>b) swap(a,b);//坑 if(que(h,hh,1)!=-1)printf("%.1f\n",que(h,hh,1)); else cout<<"-1"<<endl; } } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/47612571