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;
}
HDU 1823 Luck and Love (二维线段树&区间最值)题解的更多相关文章
- HDU 1823 Luck and Love 二维线段树(树套树)
点击打开链接 Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 1823 Luck and Love 二维线段树
题目链接 很裸的题, 唯一需要注意的就是询问时给出的区间并不是l<r, 需要判断然后交换一下, WA了好多发... #include<bits/stdc++.h> using nam ...
- HDU 4819 Mosaic (二维线段树&区间最值)题解
思路: 二维线段树模板题,马克一下,以后当模板用 代码: #include<cstdio> #include<cmath> #include<cstring> #i ...
- HDU1823 Luck ans Love 二维线段树
Luck and Love HDU - 1823 世界上上最远的距离不是相隔天涯海角 而是我在你面前 可你却不知道我爱你 ―― 张小娴 前段日子,枫冰叶子给Wiskey ...
- [hdu1823]Luck and Love(二维线段树)
解题关键:二维线段树模板题(单点修改.查询max) #include<cstdio> #include<cstring> #include<algorithm> # ...
- HDU1832 二维线段树求最值(模板)
Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu 5465 Clarke and puzzle 二维线段树
Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- HDU 1823 Luck and Love(二维线段树)
之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...
- HDU 4819 Mosaic(13年长春现场 二维线段树)
HDU 4819 Mosaic 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给定一个n*n的矩阵,每次给定一个子矩阵区域(x,y,l) ...
随机推荐
- 学习计划 mysql explain执行计划任务详解
我们在之前已经找到了需要优化的SQL,但是怎么知道它的那些方面需要优化呢? explain就是为了这个使用的. explain显示了 mysql 如何使用索引来处理select语句以及连接表.可以帮助 ...
- Java实现批量插入
//方法执行的开始时间 long startTime = System.currentTimeMillis(); Connection conn = null; try{ //获取连接 conn = ...
- mysql 权限管理 grant 命令
只有root账号可以授权,其他账号不能用grant 授权 mysql> select user(); +----------------+ | user() | +--------------- ...
- Spark Sort Based Shuffle内存分析
分布式系统里的Shuffle 阶段往往是非常复杂的,而且分支条件也多,我只能按着我关注的线去描述.肯定会有不少谬误之处,我会根据自己理解的深入,不断更新这篇文章. 前言 借用和董神的一段对话说下背景: ...
- PAT 1068 Find More Coins[dp][难]
1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...
- PAT 1029 Median[求中位数][难]
1029 Median(25 分) Given an increasing sequence S of N integers, the median is the number at the midd ...
- win10 + cuda(v9.0) 安装TensorFlow-gpu版
之前在实习公司的电脑上装过TensorFlow-gpu,那时候很快就装好了.但在自己的笔记本上装时,却搞了很久... 一部分原因是因为用校园网下载cuda toolkit 和cudnn ,总是在最后时 ...
- ES6(简)
一. let.const 和 var let和const只在当前块级作用域中有效const用来声明常量var是全局作用域有效的 constants.js 模块export const A = 1;ex ...
- vs2010用NuGet(程序包管理)安装EF失败之解决办法
今天用程序包管理控制台安装EF.报错.如下
- 利用lodop打印控件轻松实现批量打印 (转载http://www.thinkphp.cn/topic/13085.html)
最近在做一个打印程序,要实现批量打印功能,在网上找了很多天,也在tp官网咨询大牛们,对大家的的提议我一一进行了研究,总结如下: 要实现批量打印可以有两个办法: 一是利用专业的报表程序,能实现十分复杂的 ...