标签:ring ted 报名 swap pac tree pre query ane
这道题目被称为入门二维线段树。。
不好做的地方就是,开始没有想到这会是二维,建线段树必分区间。一开始想的就是从哪个量下手来分。
此处以身高为一维,活泼度为二维。
#include <iostream> #include <cstdio> #include <algorithm> #include<vector> #include<cstring> using namespace std; struct nod { int l,r; double maxv; }; struct node { int l,r; nod str[2050<<2]; }tr[2050<<2]; void subbuild(int ll,int rr,int n,int sn) { tr[n].str[sn].l=ll; tr[n].str[sn].r=rr; tr[n].str[sn].maxv=-1; if(ll==rr) return; int m=(ll+rr)>>1; subbuild(ll,m,n,sn<<1); subbuild(m+1,rr,n,sn<<1|1); } void build(int l,int r,int ll,int rr,int n) { tr[n].l=l; tr[n].r=r; subbuild(ll,rr,n,1); if(l==r) return; int m=(l+r)>>1; build(l,m,ll,rr,n<<1); build(m+1,r,ll,rr,n<<1|1); } void updata(int ho,double yu,int n,int sn) //subtree { int l=tr[n].str[sn].l; int r=tr[n].str[sn].r; if(l==r){ tr[n].str[sn].maxv=max(yu,tr[n].str[sn].maxv); return; } int m=(l+r)>>1; if(ho<=m) updata(ho,yu,n,sn<<1); else updata(ho,yu,n,sn<<1|1); tr[n].str[sn].maxv=max(tr[n].str[sn<<1].maxv,tr[n].str[sn<<1|1].maxv); } void up(int hi,int ho,double yu,int n) { int l=tr[n].l; int r=tr[n].r; updata(ho,yu,n,1); if(l==r) return; int m=(l+r)>>1; if(hi<=m) up(hi,ho,yu,n<<1); else up(hi,ho,yu,n<<1|1); } double querysub(int ll,int rr,int n,int sn) { int l=tr[n].str[sn].l; int r=tr[n].str[sn].r; if(l==ll&&r==rr){ return tr[n].str[sn].maxv; } int m=(l+r)>>1; double res=-1.0; if(ll<=m) res=max(res,querysub(ll,min(m,rr),n,sn<<1)); if(rr>m) res=max(res,querysub(max(ll,m+1),rr,n,sn<<1|1)); return res; } double query(int l,int r,int ll,int rr,int n) { if(tr[n].l==l&&tr[n].r==r){ return querysub(ll,rr,n,1); } int m=(tr[n].l+tr[n].r)>>1; double res=-1.0; if(l<=m){ res=max(res,query(l,min(m,r),ll,rr,n<<1)); } if(r>m){ res=max(res,query(max(l,m+1),r,ll,rr,n<<1|1)); } return res; } int main() { int t; while(scanf("%d",&t),t) { build(100,200,0,1000,1); while(t--) { char x[2]; scanf("%s",x); int h1,h2; double ho1,ho2,yu; if(x[0]==‘I‘){ scanf("%d%lf%lf",&h1,&ho1,&yu); up(h1,(int)(ho1*10),yu,1); } else{ scanf("%d%d%lf%lf",&h1,&h2,&ho1,&ho2); if(h1>h2) swap(h1,h2); if(ho1>ho2) swap(ho1,ho2); double res=query(h1,h2,(int)(ho1*10),(int)(ho2*10),1); if(res<0) puts("-1"); else printf("%.1lf\n",res); } } } }
标签:ring ted 报名 swap pac tree pre query ane
原文地址:http://www.cnblogs.com/ygtzds/p/7633530.html