您的位置:首页 > 其它

hdu 1823 Luck and Love 二维线段树

2013-04-11 09:31 387 查看
第一次写二维线段树,题目算是简单,就是很多的陷阱;

题意比较的简单,就不说了;

二维线段树:就是在线段树里面嵌套一个线段树,可是由于结构的原因,一层线段树必须更新到底。如果各位大神有在一维不更新到底的方法,请提出来,我一定改正!

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

#define maxn 1000
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

struct par
{
int d;
int l,r;
double s[maxn<<2];
}mp[200<<2];
int n;

void updateson(int h,int hp,double yf,int l,int r,int rt)
{
if(l==r)
{
mp[h].s[rt]=mp[h].s[rt]>yf?mp[h].s[rt]:yf;
return;
}
int m=(l+r)>>1;
if(hp<=m)
updateson(h,hp,yf,lson);
if(m<hp)
updateson(h,hp,yf,rson);
mp[h].s[rt]=max(mp[h].s[rt<<1],mp[h].s[rt<<1|1]);
}

void update(int h,int hp,double yf,int l,int r,int rt)
{
if(l==r)
{
updateson(h,hp,yf,0,1000,1);
return ;
}
int m=(l+r)>>1;
if(h<=m)
update(h,hp,yf,lson);
if(m<h)
update(h,hp,yf,rson);
}
//在孩子上进行更新
double search_tree_son(int h,int h1,int h2,int l,int r,int rt)
{
if(h1<=l&&r<=h2)
{
double x=mp[h].s[rt];
return x;
}
int m=(l+r)>>1;
double t1=-1;
double t2=-1;
if(h1<=m)
t1=search_tree_son(h,h1,h2,lson);
if(h2>m)
t2=search_tree_son(h,h1,h2,rson);
return max(t1,t2);
}

double search_tree(int L,int R,int h1,int h2,int l,int r,int rt)
{
if(l==r)
{
return search_tree_son( l, h1, h2, 0, 1000, 1);//在身高的地方更新到底,想了想,如果不更新到底,是不太可能的,不知道大家有什么可以不更新到底的方法
}
int m=(l+r)>>1;
double t1=-1;
double t2=-1;
if(L<=m)
t1=search_tree(L,R,h1,h2,lson);
if(m<R)
t2=search_tree(L,R,h1,h2,rson);
return max(t1,t2);
}

int main()
{
char ch;
double hp,yf;
int h;
int h1,h2;
while(scanf("%d",&n),n)
{
memset(mp,0,sizeof(mp));
for(int i=0;i<n;i++)
{
scanf("%*c");
scanf("%c",&ch);
if(ch=='I')
{
scanf("%d%lf%lf",&h,&hp,&yf);
h-=100;
hp*=10;//具体化
int HP=hp;
yf+=1.0;//加1.0,只是为了控制没有结果的情况
update(h,HP,yf,0,100,1);
}
if(ch=='Q')
{
scanf("%d%d%lf%lf",&h1,&h2,&hp,&yf);
h1-=100;
h2-=100;
hp*=10;
yf*=10;
int HP=hp;
int YF=yf;
if(h1>h2)//在这个地方WA了几次
swap(h1,h2);
if(HP>YF)
swap(HP,YF);
double x=search_tree(h1,h2,HP,YF,0,100,1);
if(x==0)
printf("-1\n");
else
printf("%.1f\n",x-1.0);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息