hdu1823:luck and love
作者:互联网
题目大意:请写一种二维的数据结构,支持:
1)修改某个点
2)查询某一块内的最大值
solution:
这题网上的solution有些错了2333,写这个的目的也是给大家做一个参考。
既然二维树状数组无法解决,那我们就用二维线段树咯?
当然二维线段树还是非常好写的(调了我20min)
#include<cstdio> #include<iostream> #include<cstring> using namespace std; const double eps=1e-6; int n,tree[205<<2][1005<<2],h,h1,h2; double a,l,a1,a2; char op[5]; int work(double p){return (int) (10.0*p+eps);} void update1(int k,int x,int l,int r,int q,int val,bool flag){ if (l==r){ if (flag) tree[x][k]=max(tree[x][k],val); else tree[x][k]=max(tree[x<<1][k],tree[x<<1|1][k]); return ; } int mid=l+r>>1; if (q<=mid) update1(k<<1,x,l,mid,q,val,flag); else update1(k<<1|1,x,mid+1,r,q,val,flag); tree[x][k]=max(tree[x][k<<1],tree[x][k<<1|1]); } void update(int k,int l,int r,int v,int x,int y){ if (l==r){update1(1,k,0,1000,y,v,1);return ;} int mid=l+r>>1; if (x<=mid) update(k<<1,l,mid,v,x,y); else update(k<<1|1,mid+1,r,v,x,y); update1(1,k,0,1000,y,v,0); } int query1(int k,int w,int l,int r,int x,int y){ if (x<=l&&r<=y){return tree[w][k];} int mid=l+r>>1,ret=-1; if (x<=mid) ret=max(ret,query1(k<<1,w,l,mid,x,y)); if (mid<y) ret=max(ret,query1(k<<1|1,w,mid+1,r,x,y)); return ret; } int query(int k,int l,int r,int l1,int r1,int l2,int r2){ if (l>=l1&&r<=r1){return query1(1,k,0,1000,l2,r2);} int mid=l+r>>1,ret=-1; if (l1<=mid) ret=max(ret,query(k<<1,l,mid,l1,r1,l2,r2)); if (mid<r1) ret=max(ret,query(k<<1|1,mid+1,r,l1,r1,l2,r2)); return ret; } int main(){ memset (tree,-1,sizeof (tree)); scanf ("%d",&n); while (n--){ scanf ("%s",op); if (op[0]=='I'){ scanf ("%d",&h);scanf ("%lf%lf",&a,&l); update(1,100,200,work(l),h,work(a)); } else{ scanf ("%d%d",&h1,&h2); scanf ("%lf%lf",&a1,&a2); if (h1>h2) swap(h1,h2); if (a1>a2) swap(a1,a2); int ans=query(1,100,200,h1,h2,work(a1),work(a2)); if (ans<0) printf ("-1\n"); else printf ("%.1lf\n",(double) ans*1.0/10.0); } } return 0; }
标签:love,int,h1,a1,二维,a2,include,hdu1823,luck 来源: https://www.cnblogs.com/DFTMR/p/11257319.html