分析
一个非常显然的单调队列+线段树的常规题
当然,扫描线一样一样的
但因为以前是直接动态开点的,所以这次打个离散化版本
(Code)
#include<cstdio>
#include<algorithm>
#include<iostream>
#define LL long long
#define ls (k << 1)
#define rs (ls | 1)
using namespace std;
const int N = 10004;
int n, W, H, X[N], Y[N], T, d[N];
LL ans, mx[N << 2], tag[N << 2];
struct point{int x, y, v;}a[N];
inline bool cmp1(point a, point b){return a.x < b.x;}
inline bool cmp2(point a, point b){return a.y < b.y;}
void build(int l, int r, int k)
{
mx[k] = tag[k] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(l, mid, ls), build(mid + 1, r, rs);
}
void update(int l, int r, int k, int x, int y, int v)
{
if (x <= l && r <= y) return void(tag[k] += v);
int mid = (l + r) >> 1;
if (x <= mid) update(l, mid, ls, x, y, v);
if (y > mid) update(mid + 1, r, rs, x, y, v);
mx[k] = max(mx[ls] + tag[ls], mx[rs] + tag[rs]);
}
inline int search_up(int y)
{
int l = 1, r = n, mid, ret;
while (l <= r)
{
mid = (l + r) >> 1;
if (Y[mid] >= y) ret = mid, r = mid - 1;
else l = mid + 1;
}
return ret;
}
int main()
{
scanf("%d", &T);
for(; T; T--)
{
scanf("%d%d%d", &n, &W, &H);
ans = 0, build(1, n, 1);
for(register int i = 1; i <= n; i++) scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].v);
sort(a + 1, a + n + 1, cmp2);
for(register int i = 1; i <= n; i++) Y[i] = a[i].y, a[i].y = i;
sort(a + 1, a + n + 1, cmp1);
for(register int i = 1; i <= n; i++) X[i] = a[i].x, a[i].x = i;
int h = 1, t = 0;
for(register int i = 1; i <= n; i++)
{
while (h <= t && X[a[i].x] - X[a[d[h]].x] >= W)
update(1, n, 1, search_up(Y[a[d[h]].y] - H + 1), a[d[h]].y, -a[d[h]].v), ++h;
d[++t] = i, update(1, n, 1, search_up(Y[a[i].y] - H + 1), a[i].y, a[i].v);
ans = max(ans, mx[1] + tag[1]);
}
printf("%lld
", ans);
}
}