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) ...
随机推荐
- 洛谷P3527 MET-Meteors [POI2011] 整体二分
正解:整体二分 解题报告: 传送门! 还有个双倍经验!(明明是一样的题目为什么你们一个紫一个黑啊喂! 这题首先要想到可以二分嘛,然后看到多组询问肯定就整体二分鸭 那就是基本套路啊,发现是区间修改单点查 ...
- 最新版OpenWrt编译教程,解决依赖问题
Install git , to conveniently download the OpenWrt source code, and build tools to do the cross-comp ...
- 【JMeter】如何录制创建及得到曲线图
前段时间公司需要对服务器进行压力测试,包括登录前的页面和登录后的页面,主要目的是测试负载均衡的实现效果.不知道是不是因为Jmeter不如loadRunner火爆还是什么,网上关于Jmeter的资料有很 ...
- java-JProfiler(四)-HelloWorld示例
1.程序代码 package jProfiler; public class Test extends Thread{ public static void main(String[] args) t ...
- 深度学习之TensorFlow(一)——基本使用
一.目前主流的深度学习框架Caffe, TensorFlow, MXNet, Torch, Theano比较 库名称 开发语言 速度 灵活性 文档 适合模型 平台 上手难易 Caffe c++/cud ...
- python的时间差计算
import time start = time.clock() #当中是你的程序 elapsed = (time.clock() - start) print("Time used:&qu ...
- (转)Elasticsearch查询规则------match和term
es种有两种查询模式,一种是像传递URL参数一样去传递查询语句,被称为简单搜索或查询字符串(query string)搜索,比如 GET /megacorp/employee/_search //查询 ...
- nodejs+express的(前端跨域请求)
1.后端代码 var dp = 456; var back = 'callback(\{\dp\ : \ ' + dp + '\ }\)'; res.send(back); 2.前端代码 <sc ...
- 线段树(I tree)
Codeforces Round #254 (Div. 2)E题这题说的是给了一个一段连续的区间每个区间有一种颜色然后一个彩笔从L画到R每个区间的颜色都发生了 改变然后 在L和R这部分区间里所用的颜色 ...
- Java8函数接口实现回调及Groovy闭包的代码示例
本文适用于想要了解Java8 Function接口编程及闭包表达式的筒鞋. 概述 在实际开发中,常常遇到使用模板模式的场景: 主体流程是不变的,变的只是其中要调用的具体方法. 其特征是: Begi ...