分析:
有两个区间要查找。
第一次写二维线段树,有蛮多细节要考虑的。。
二维线段树 由 母树 和 子树 组成。
母树的每个区间都有一棵树。
这里母树用高作为区间长度。子树用活泼值。然后母树和子树都有一个 max 域,用来存储最大值。
开始的时候,更新时我就只把母树的叶子节点更新了,结果查询出问题,老半天才看出来。
后来就改成 只要含有这个高度值的区间都更新。
这个题目需要解决几个问题:
1. 活泼值只到小数点后一位,所以乘上10取整就可以了。区间就成了 (0,1000)
这样可以AC , 但是有一组案例:
2 I 170 69.3 96.5 Q 144 184 38.3 69.2 0 答案 : -1就会答案为 96.5 .。是因为 做 int(b1*10) 乘法时出现了 精度误差 本来是 383 的 变成了382 。
要加上一个 1e-6 才行。 -------学到了。。。
2. 数据中会有 A1 > A2 or H1 > H2 比较下就OK
3. 数据中还会出现 更新时 H 和 A 值一样的 就与当下的缘分值比较下大小就OK
//Accepted 1823 203MS 7552K 3221 B C++
#include
#include
using namespace std;
#define MAXN 200
const double eps=1e-6;
struct node1
{
int left,right;
double max;
};
struct node2
{
int l,r;
double max;
node1 sub[6000];
}tree[MAXN*3];
double max(double a,double b) {return a>b?a:b;}
void creat1(int fa,int root,int left,int right)
{
tree[fa].sub[root].left=left;
tree[fa].sub[root].right=right;
tree[fa].sub[root].max=0;
if(left==right) return ;
int mid=(left+right)>>1;
creat1(fa,root<<1,left,mid);
creat1(fa,root<<1|1,mid+1,right);
}
double insert1(int fa,int root,int pos,double val)
{
if(tree[fa].sub[root].left>pos||tree[fa].sub[root].rightright||tree[fa].sub[root].right=left&&tree[fa].sub[root].right<=right)
return tree[fa].sub[root].max;
return max(query1(fa,root<<1,left,right),query1(fa,root<<1|1,left,right));
}
void creat2(int root,int left,int right)
{
tree[root].l=left;
tree[root].r=right;
tree[root].max=0;
creat1(root,1,0,1000);
if(left==right) return;
int mid=(left+right)>>1;
creat2(root<<1,left,mid);
creat2(root<<1|1,mid+1,right);
}
void insert2(int root,int h,int a,double vol)
{
insert1(root,1,a,vol);
if(tree[root].l==tree[root].r) return;
if(tree[root<<1].r >=h ) insert2(root<<1,h,a,vol);
else insert2(root<<1|1,h,a,vol);
}
double query2(int root,int h1,int h2,int a1,int a2)
{
if(tree[root].l>h2||tree[root].r=h1&&tree[root].r<=h2)
{
return query1(root,1,a1,a2);
}
return max(query2(root<<1,h1,h2,a1,a2),query2(root<<1|1,h1,h2,a1,a2));
}
void swap(int &a,int &b)
{
int t=a;
a=b; b=t;
}
int main()
{
int M;
while(~scanf("%d",&M)&&M)
{
char op[2];
creat2(1,0,100);
while(M--)
{
scanf("%s",op);
if(op[0]=='I')
{
int h,a; double act,aff;
scanf("%d%lf%lf",&h,&act,&aff);
a=int((act+eps)*10);
insert2(1,h-100,a,aff);
}
else if(op[0]=='Q')
{
int a1,a2; double b1,b2;
scanf("%d%d%lf%lf",&a1,&a2,&b1,&b2);
// cout<<"不加 "<a2) swap(a1,a2);
if(c1>c2) swap(c1,c2);
double ans=query2(1,a1-100,a2-100,c1,c2);
if(ans==0.0) printf("-1\n");
else printf("%.1lf\n",ans);
}
}
}
return 0;
}
/**
8
I 160 50.5 60.0
I 165 30.0 80.5
I 166 10.0 50.0
I 170 80.5 77.5
Q 130 120 50.5 60.1
2 //加上eps 才能过这案例!
I 170 69.3 96.5
Q 144 174 38.3 69.2
0 //答案 -1
2
I 170 69.3 96.5
Q 144 174 38.3 69.2
0
**/