[SCOI2007]降雨量
Description
Solution
这个题的思路其实还是很好想的,就是细节有点多...
Code
#include <cstdio>
#include <algorithm>
const int N = 5e4 + 10;
const int INF = 0x7fffffff;
int y[N], h[N];
int n, m;
int mx[N<<2];
#define mid ((l+r)>>1)
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
void build(int o, int l, int r) {
if (l == r) {
mx[o] = h[l];
return;
}
build(lson);
build(rson);
mx[o] = std::max(mx[o<<1], mx[o<<1|1]);
}
int query(int o, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
return mx[o];
}
int ans = 0;
if (ql <= mid) ans = std::max(ans, query(lson, ql, qr));
if (qr > mid) ans = std::max(ans, query(rson, ql, qr));
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &y[i], &h[i]);
}
build(1, 1, n);
scanf("%d", &m);
for (int i = 1, l, r; i <= m; ++i) {
scanf("%d%d", &l, &r);
int py = std::lower_bound(y+1, y+n+1, l) - y; // lower_bound查到的是不小于的
int px = std::lower_bound(y+1, y+n+1, r) - y;
if (y[px] == r) {
if (y[py] == l) {
if (h[py] < h[px]) {
puts("false"); // x比y高
} else {
int max = -INF;
if (py + 1 <= px - 1) max = query(1, 1, n, py+1, px-1);
if (max >= h[px]) puts("false"); // z比x高
else puts(r-l == px-py ? "true" : "maybe"); // 中间年份全部已知
}
} else {
int max = -INF;
// printf("%d %d\n", y[py], y[px]);
if (py <= px - 1) max = query(1, 1, n, py, px-1);
// printf("%d\n", max);
if (max >= h[px]) puts("false"); // z比x高
else puts("maybe"); // x比z高但y未知
}
} else {
if (y[py] == l) {
int max = -INF;
if (py + 1 <= px - 1) max = query(1, 1, n, py+1, px-1);
if (max >= h[py]) puts("false"); // z比y高
else puts("maybe"); // x未知
} else {
puts("maybe"); // x和y都未知
}
}
}
return 0;
}
Note
lower_bound
是不小于x的第一个数啊!!!!!!
分类讨论要有条理!!
不要query
之后不写max =
啊!!!!!!