标签:des style blog io ar os sp 数据 div
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 150 166 10.0 60.0 Q 166 177 10.0 50.0 I 166 40.0 99.9 Q 166 177 10.0 50.0 0
80.5 50.0 99.9
裸二维线段树。
有几个地方需要注意,查询可能x1 > x2, y1 > y2,还有更新的时候如果该位置已经有值 不是直接覆盖而是取max。
(还有一个地方我也没注意但是AC了,就是一开始不应该置0, 因为题目活了缘分值是可能为0的)
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
int T, h, N = 202, M = 1002, x1, x2, y1, y2, a;
double tmpa, ll, tmpy1, tmpy2;
char s[3];
double mx[222<<2][1111<<2];
void updatey(int o, int l, int r, int oo, int ok) {
if(l == r) {
if(ok == -1) mx[oo][o] = max(ll, mx[oo][o]); // 非直接覆盖,而是取最大值
else mx[oo][o] = max(mx[oo<<1][o], mx[oo<<1|1][o]);
return;
}
int m = (l+r) >> 1;
if(a <= m) updatey(lson, oo, ok);
else updatey(rson, oo, ok);
mx[oo][o] = max(mx[oo][o<<1], mx[oo][o<<1|1]);
}
void updatex(int o, int l, int r) {
if(l == r) {
updatey(1, 1, M, o, -1);
return;
}
int m = (l+r) >> 1;
if(h <= m) updatex(lson);
else updatex(rson);
updatey(1, 1, M, o, 1);
}
double queryy(int o, int l, int r, int oo) {
if(y1 <= l && r <= y2) return mx[oo][o];
int m = (l+r) >> 1;
double ans = -1.0;
if(y1 <= m) ans = queryy(lson, oo);
if(m < y2 ) ans = max(ans, queryy(rson, oo));
return ans;
}
double queryx(int o, int l, int r) {
if(x1 <= l && r <= x2) return queryy(1, 1, M, o);
int m = (l+r) >> 1;
double ans = -1.0;
if(x1 <= m) ans = queryx(lson);
if(m < x2 ) ans = max(ans, queryx(rson));
return ans;
}
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d", &T) && T) {
memset(mx, 0, sizeof(mx));
while(T--) {
scanf("%s", s);
if(s[0] == 'I') {
scanf("%d%lf%lf", &h, &tmpa, &ll);
a = (int)(tmpa*10);
// printf("(%d %d) -> %.1lf\n", h, a, ll);
updatex(1, 1, N);
} else {
scanf("%d%d%lf%lf", &x1, &x2, &tmpy1, &tmpy2);
y1 = (int)(tmpy1*10);
y2 = (int)(tmpy2*10);
if(x1 > x2) swap(x1, x2);
if(y1 > y2) swap(y1, y2); // 开始的时候忽略了。。
// printf("x1 = %d , x2 = %d , y1 = %d , y2 = %d\n", x1, x2, y1, y2);
double ans = queryx(1, 1, N);
if(ans == 0) puts("-1");
else printf("%.1lf\n", ans);
}
}
}
return 0;
}
HDU 1832 Luck and Love (二维线段树)
标签:des style blog io ar os sp 数据 div
原文地址:http://blog.csdn.net/u013923947/article/details/40896135