HDU1823-Luck and Love-二维线段树(模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?
pid=1823
好吧,给这题跪了。
。。orz....
一道非常基础的二维线段树的模板题;
可是细节非常多;尤其注意了;
swap函数会丢失精度,用double就等着WA到死吧。
。
。
orz...
还有就是给你的区间不一定是按顺序的。得加一个推断;真的是坑。。。orz....
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<cmath>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define LL long long
#define inf 1<<30
#define s(a) scanf("%d",&a)
#define CL(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=5005;
int n,a,b;
float seg[N][N<<2]; // 表示X轴Y轴。
void Build_2(int l,int r,int deep,int rt) // 建y轴方向线段树(二维);
{
seg[deep][rt]=-1.0;
if(l==r) return;
int mid=(l+r)>>1;
Build_2(l,mid,deep,rt<<1);
Build_2(mid+1,r,deep,rt<<1|1);
}
void Build(int l,int r,int rt) // 建x轴方向线段树(一维)。
{
Build_2(0,1000,rt,1);
if(l==r) return;
int mid=(l+r)>>1;
Build(l,mid,rt<<1);
Build(mid+1,r,rt<<1|1);
}
void Insert_2(int act,float love,int l,int r,int deep,int rt) // y轴方向更新数据;(二维)
{
seg[deep][rt]=max(love,seg[deep][rt]);
if(l==r) return;
int mid=(l+r)>>1;
if(act<=mid) Insert_2(act,love,l,mid,deep,rt<<1);
else Insert_2(act,love,mid+1,r,deep,rt<<1|1);
seg[deep][rt]=max(seg[deep][rt<<1],seg[deep][rt<<1|1]);
}
void Insert(int h,int act,float love,int l,int r,int rt) // x轴,一维;
{
Insert_2(act,love,0,1000,rt,1);
if(l==r) return;
int mid=(l+r)>>1;
if(h<=mid) Insert(h,act,love,l,mid,rt<<1);
else Insert(h,act,love,mid+1,r,rt<<1|1);
}
float Query_2(int L,int R,int l,int r,int rt,int deep) // 查询,y轴,二维;
{
if(L<=l&&R>=r) return seg[deep][rt];
int mid=(l+r)>>1;
if(R<=mid) return Query_2(L,R,l,mid,rt<<1,deep);
else if(L>mid) return Query_2(L,R,mid+1,r,rt<<1|1,deep);
else return max(Query_2(L,R,l,mid,rt<<1,deep),Query_2(L,R,mid+1,r,rt<<1|1,deep));
}
float Query(int h1,int h2,int L,int R,int l,int r,int rt) // x轴。一维。
{
if(h1<=l&&h2>=r) return Query_2(L,R,0,1000,1,rt);
int mid=(l+r)>>1;
if(h2<=mid) return Query(h1,h2,L,R,l,mid,rt<<1);
else if(h1>mid) return Query(h1,h2,L,R,mid+1,r,rt<<1|1);
else return max(Query(h1,h2,L,R,l,mid,rt<<1),Query(h1,h2,L,R,mid+1,r,rt<<1|1));
}
int main()
{
int n;
while(~scanf("%d",&n)&&n){
Build(100,200,1);
char ch[5];
while(n--){
scanf("%s",&ch);
if(ch[0]=='I'){
int h;
float x,y;
scanf("%d%f%f",&h,&x,&y);
Insert(h,(int)(x*10),y,100,200,1);
}else{
int h1,h2;
float x1,x2;
scanf("%d%d%f%f",&h1,&h2,&x1,&x2);
//if(h1>h2) swap(h1,h2); // swap函数的使用丢失精度非常严重,得用float才干AC;
//if(x1>x2) swap(x1,x2);
if(h1>h2){ int temp=h1; h1=h2;h2=temp; }
if(x1>x2){ float temp=x1;x1=x2;x2=temp; }
float ans=Query(h1,h2,(int)(x1*10),(int)(x2*10),100,200,1);
if(ans==-1.0) printf("-1\n");
else printf("%.1lf\n",ans);
}
}
}
return 0;
}
HDU1823-Luck and Love-二维线段树(模板)的更多相关文章
- [hdu1823]Luck and Love(二维线段树)
解题关键:二维线段树模板题(单点修改.查询max) #include<cstdio> #include<cstring> #include<algorithm> # ...
- HDU1823 Luck ans Love 二维线段树
Luck and Love HDU - 1823 世界上上最远的距离不是相隔天涯海角 而是我在你面前 可你却不知道我爱你 ―― 张小娴 前段日子,枫冰叶子给Wiskey ...
- hdu1823(二维线段树模板题)
hdu1823 题意 单点更新,求二维区间最值. 分析 二维线段树模板题. 二维线段树实际上就是树套树,即每个结点都要再建一颗线段树,维护对应的信息. 一般一维线段树是切割某一可变区间直到满足所要查询 ...
- hdu 4819 二维线段树模板
/* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits ...
- 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 ...
- Luck and Love(二维线段树)
Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU1832 二维线段树求最值(模板)
Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 4819 Mosaic (二维线段树&区间最值)题解
思路: 二维线段树模板题,马克一下,以后当模板用 代码: #include<cstdio> #include<cmath> #include<cstring> #i ...
- UVA 11297 Census ——二维线段树
[题目分析] 二维线段树模板题目. 简直就是无比的暴力.时间复杂度为两个log. 标记的更新方式比较奇特,空间复杂度为N^2. 模板题目. [代码] #include <cstdio> # ...
随机推荐
- bzoj 1269 bzoj 1507 Splay处理文本信息
bzoj 1269 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 大致思路: 用splay维护整个文本信息,splay树的中序遍历即为 ...
- 50.分治算法练习: 二分算法: 2703 奶牛代理商 XII
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 小徐从美国回来后,成为了USACO中国区的奶牛销售代理商,专门出售 ...
- [转]android中drawable资源的解释及例子
原文链接: http://blog.csdn.net/wode_dream/article/details/38584693 文章中的内容参考Dev Guide中的Drawable R ...
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- javascript:window.history.forward(1);
javascript:window.history.forward(1);[转] 接下来我们要讨论的方法以后退按钮本身为中心,而不是浏览器缓存.这儿有一篇文章Rewiring the Back But ...
- 安卓之service简单介绍
一 什么是Service 二 如何使用Service 三 Service的生命周期 一 什么是Service Service,看名字就知道跟正常理解的“服务”差不多,后台运行,可交互这样的一个东西 ...
- Linear regulator=low-cost dc/dc converter
The circuit in Figure 1 is a good choice if you need a power supply with high efficiency and you don ...
- Boost Converter
Single Inductor Buck-Boost Converter in Tiny WCSP The TPS63036 is a non inverting buck-boost convert ...
- Maven:Generating Project in Batch mode 卡住问题
Maven命令执行到Generating Project in Batch mode 卡住,原因是网络带宽不足问题!需要下载一个约4.1M的archetype-catalog.xml文件. Maven ...
- Apache服务器和tomcat服务器有什么区别?
Apache与Tomcat都是Apache开源组织开发的用于处理HTTP服务的项目,两者都是免费的,都可以做为独立的 Web服务器运行.Apache是Web服务器而Tomcat是Java应用服务器. ...