HDU 1823 Luck and Love (二维线段树&区间最值)题解

思路:

树套树,先维护x树,再维护y树,多练练应该就能懂了

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int N = 300+5;
double node[205<<2][1005<<2];
int n;
void push_upx(int deep,int rt){
node[deep][rt] = max(node[deep<<1][rt],node[deep<<1|1][rt]);
}
void push_upy(int deep,int rt){
node[deep][rt] = max(node[deep][rt<<1],node[deep][rt<<1|1]);
}
void buildy(int ly,int ry,int deep,int rt){
node[deep][rt] = -1;
if(ly == ry) return;
int m = (ly + ry) >> 1;
buildy(ly,m,deep,rt << 1);
buildy(m+1,ry,deep,rt << 1 | 1);
}
void buildx(int lx,int rx,int rt){
buildy(0,1000,rt,1);
if(lx == rx) return;
int m = (lx + rx) >> 1;
buildx(lx,m,rt << 1);
buildx(m+1,rx,rt << 1 | 1);
}
void updatey(int act,double val,int ly,int ry,int deep,int rt){
node[deep][rt] = max(node[deep][rt],val);
if(ly == ry) return;
int m = (ly + ry) >> 1;
if(act <= m) updatey(act,val,ly,m,deep,rt << 1);
else updatey(act,val,m + 1,ry,deep,rt << 1 | 1);
push_upy(deep,rt);
}
void updatex(int h,int act,double val,int lx,int rx,int rt){ //更新h,act
updatey(act,val,0,1000,rt,1);
if(lx == rx) return;
int m = (lx + rx) >> 1;
if(h <= m) updatex(h,act,val,lx,m,rt << 1);
else updatex(h,act,val,m + 1,rx,rt << 1 | 1);
}
double queryy(int actl,int actr,int ly,int ry,int deep,int rt){
if(actl <= ly && ry <= actr) return node[deep][rt];
int m = (ly + ry) >> 1;
if(m >= actr)
return queryy(actl,actr,ly,m,deep,rt << 1);
else if(m < actl)
return queryy(actl,actr,m + 1,ry,deep,rt << 1 | 1);
return max(queryy(actl,actr,ly,m,deep,rt << 1),queryy(actl,actr,m + 1,ry,deep,rt << 1 | 1));
}
double queryx(int hl,int hr,int actl,int actr,int lx,int rx,int rt){
if(hl <= lx && rx <= hr){
return queryy(actl,actr,0,1000,rt,1);
}
int m = (lx + rx) >> 1;
if(m >= hr)
return queryx(hl,hr,actl,actr,lx,m,rt << 1);
else if(m < hl)
return queryx(hl,hr,actl,actr,m + 1,rx,rt << 1 | 1);
return max(queryx(hl,hr,actl,actr,lx,m,rt << 1),queryx(hl,hr,actl,actr,m + 1,rx,rt << 1 | 1));
}
int main(){
char o[2];
int h1,h2;
double l,a1,a2;
while(scanf("%d",&n) && n){
buildx(100,200,1);
for(int i = 0;i < n;i++){
scanf("%s",o);
if(o[0] == 'I'){
scanf("%d%lf%lf",&h1,&a1,&l);
int A = (int)(a1*10);
updatex(h1,A,l,100,200,1);
}
else{
scanf("%d%d%lf%lf",&h1,&h2,&a1,&a2);
int A1 = (int)(a1*10),A2 = (int)(a2*10);
if(h1 > h2) swap(h1,h2);
if(A1 > A2) swap(A1,A2);
double ans = queryx(h1,h2,A1,A2,100,200,1);
if(ans == -1.0)
printf("-1\n");
else
printf("%.1lf\n",ans);
}
}
}
return 0;
}
上一篇:Excel中使用VBA进行度分秒与十进制度的转换


下一篇:PyQt(Python+Qt)学习随笔:Qt Designer中部件的toolTip、toolTipDuration、statusTip、whatsThis属性