http://poj.org/problem?id=2482
题面很感人,撩妹子的小trick;
依旧是扫描线法,在此之前需要一步转化,
考虑对于每颗星星,能将其覆盖的矩形右上角的轨迹所组成的矩形,
显然答案变为求一块区域,使得矩形在其上覆盖产生的权值和最大,
建一棵线段树支持区间加和区间查询最大值,从左往右扫描即可,
注意边缘的星星不算,注意对线段的排序方式,和面积并周长并有区别。
#include
#include
using namespace std;
inline int read() {
int num = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9')
num = num * 10 + c - '0', c = getchar();
return num;
}
const int maxn = 1e4 + 5;
struct Line {
long long x, y1, y2, k;
bool operator < (const Line& rhs) const {
if (x == rhs.x) return k < rhs.k;
return x < rhs.x;
}
} line[2 * maxn];
struct SegmentTree {
int l, r, maxv, add;
} t[8 * maxn];
inline void up(int p) {
t[p].maxv = max(t[2 * p].maxv, t[2 * p + 1].maxv);
}
inline void mark(int p, int d) {
t[p].maxv += d, t[p].add += d;
}
inline void down(int p) {
if (t[p].add) {
mark(2 * p, t[p].add), mark(2 * p + 1, t[p].add);
t[p].add = 0;
}
}
void build(int p, int l, int r) {
t[p].l = l, t[p].r = r, t[p].maxv = t[p].add = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(2 * p, l, mid);
build(2 * p + 1, mid + 1, r);
}
void modify(int p, int l, int r, int d) {
if (l <= t[p].l && t[p].r <= r) {
mark(p, d);
return;
}
down(p);
int mid = (t[p].l + t[p].r) >> 1;
if (l <= mid) modify(2 * p, l, r, d);
if (r > mid) modify(2 * p + 1, l, r, d);
up(p);
}
long long a[2 * maxn];
int main() {
int n, w, h;
while (scanf("%d%d%d", &n, &w, &h) == 3) {
for (int i = 1; i <= n; ++i) {
int x = read(), y = read(), c = read();
long long xx = 1ll * x + w, yy = 1ll * y + h - 1;
line[i] = (Line){x, y, yy, c};
line[i + n] = (Line){xx, y, yy, -c};
a[i] = y, a[i + n] = yy;
}
sort(line + 1, line + 2 * n + 1);
sort(a + 1, a + 2 * n + 1);
a[0] = unique(a + 1, a + 2 * n + 1) - a - 1;
build(1, 1, a[0]);
int ans = 0;
for (int i = 1; i < 2 * n; ++i) {
int y1 = lower_bound(a + 1, a + a[0] + 1, line[i].y1) - a;
int y2 = lower_bound(a + 1, a + a[0] + 1, line[i].y2) - a;
modify(1, y1, y2, line[i].k);
ans = max(ans, t[1].maxv);
}
printf("%d\n", ans);
}
return 0;
}