思路:二维线段树模板题。第一维是身高,范围100-200,第二维是活泼度,范围0-1000。
一开始还在想浮点数要怎么建树,结果愣了半天发现只有一位小数。。。
网上大部分的代码是基于结构体实现,所以自己打了一个数组版本(因为是求最值又没有初始值,所以数组版本不需要Build函数)
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define debug(a) printf("Debug: %d\n",a)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define lhs l,m,lson(rt)
#define rhs m+1,r,rson(rt)
#define half(x) ((x)>>1)
const int Mod = 1000000007;
const int maxn =1e5+50;
const int INF = 0x3f3f3f3f;
const double eps=1e-12;
int Max[820][4050];
void PushUp(int pa,int rt)
{
Max[pa][rt]=max(Max[pa][lson(rt)],Max[pa][rson(rt)]);
}
void UpdateSub(int p,int pa,int add,int l,int r,int rt)
{
// printf("UpdateSub p:%d pa:%d add:%d l:%d r:%d rt:%d\n",p,pa,add,l,r,rt);
if (l==r)
{
Max[pa][rt]=max(add,Max[pa][rt]);
return;
}
int m=half(l+r);
if (p<=m) UpdateSub(p,pa,add,lhs);
if (p>m) UpdateSub(p,pa,add,rhs);
PushUp(pa,rt);
}
void Update(int p,int sonid,int add,int l,int r,int rt)
{
// printf("Update p:%d sonid:%d add:%d l:%d r:%d rt:%d\n",p,sonid,add,l,r,rt);
UpdateSub(sonid,rt,add,0,1000,1);
if (l==r) return;
int m=half(l+r);
if (p<=m) Update(p,sonid,add,lhs);
if (p>m) Update(p,sonid,add,rhs);
}
int QuerySub(int pa,int L,int R,int l,int r,int rt)
{
// printf("QuerySub pa:%d L:%d R:%d l:%d r:%d rt:%d\n",pa,L,R,l,r,rt);
if (L<=l && R>=r) return Max[pa][rt];
int m=half(l+r);
int res=-1;
if (L<=m) res=max(res,QuerySub(pa,L,R,lhs));
if (R>m) res=max(res,QuerySub(pa,L,R,rhs));
return res;
}
int Query(int L,int R,int Ls,int Rs,int l,int r,int rt)
{
// printf("Query L:%d R:%d Ls:%d Rs:%d l:%d r:%d rt:%d\n",L,R,Ls,Rs,l,r,rt);
if (L<=l && R>=r) return QuerySub(rt,Ls,Rs,0,1000,1);
int m=half(l+r);
int res=-1;
if (L<=m) res=max(res,Query(L,R,Ls,Rs,lhs));
if (R>m) res=max(res,Query(L,R,Ls,Rs,rhs));
return res;
}
int N;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while (scanf("%d",&N)!=EOF && N)
{
mem(Max,-1);
char c; int a,b; double d,e;
for (int i=1;i<=N;i++)
{
cin>>c;
if (c=='I')
{
scanf("%d %lf %lf",&a,&e,&d);
Update(a,int(e*10),int(d*10),100,200,1);
}
else
{
scanf("%d %d %lf %lf",&a,&b,&d,&e);
if (a>b) swap(a,b);
if (d>e) swap(e,d);
double ans=double(Query(a,b,int(d*10),int(e*10),100,200,1));
if (ans<0) puts("-1");
else printf("%.1lf\n",ans/10);
}
}
}
return 0;
}
//结构体版参考资料
//http://www.tuicool.com/articles/EFb22u
//http://blog.csdn.net/leolin_/article/details/7265278