HDU-6638
Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1609 Accepted Submission(s): 486
There are n pirate chests buried in Byteland, labeled by 1,2,…,n. The i-th chest’s location is (xi,yi), and its value is wi, wi can be negative since the pirate can add some poisonous gases into the chest. When you open the i-th pirate chest, you will get wi value.
You want to make money from these pirate chests. You can select a rectangle, the sides of which are all paralleled to the axes, and then all the chests inside it or on its border will be opened. Note that you must open all the chests within that range regardless of their values are positive or negative. But you can choose a rectangle with nothing in it to get a zero sum.
Please write a program to find the best rectangle with maximum total value.
The first line of the input contains an integer T(1≤T≤100), denoting the number of test cases.
In each test case, there is one integer n(1≤n≤2000) in the first line, denoting the number of pirate chests.
For the next n lines, each line contains three integers xi,yi,wi(−109≤xi,yi,wi≤109), denoting each pirate chest.
It is guaranteed that ∑n≤10000.
For each test case, print a single line containing an integer, denoting the maximum total value.
2
4
1 1 50
2 1 50
1 2 50
2 2 -500
2
-1 1 5
-1 1 1
100
6
第一行T组数据
第二行一个整数 n 表示 n 个点
接下来 n 行,每行三个整数,分别代表每个点的横坐标,纵坐标和点的权值
给出 n 个点的横纵坐标和权值,输出一个矩形区域总和的最大值
因为总共就 2000 个点,而数值是1e9,所以先将横纵坐标进行离散化,然后按照纵坐标 y 从大到小排序;从小到大枚举 y 的下界,从下界开始向上扫描上界,每次扫描一个上界就将这条线上的每个点加入到维护区间最大子段和的线段树中,(因为之前先按纵坐标进行排序了,所以只需要按顺序把点加进去即可,所以时间复杂度不会到 O(n^3)),把这条线上的点加入到树中后,更新 ans (直接访问根结点所维护的最大子段和即可);
#include
#include
#include
#include
#include
#define maxn 2010
using namespace std;
struct point {
int x, y, w;
} p[maxn];
struct node {
long long s, ms, rs, ls;
int r, l;
} tree[maxn<<3];
vector<int>yl;
vector<int>xl;
int cmp(point a, point b) {
return a.y == b.y ? a.x < b.x : a.y < b.y;
}
int getYId(int x) {
return lower_bound(yl.begin(), yl.end(), x) - yl.begin() + 1;
}
int getXId(int x) {
return lower_bound(xl.begin(), xl.end(), x) - xl.begin() + 1;
}
void clear(int root, int l, int r) {
tree[root].l = l;
tree[root].r = r;
tree[root].s = tree[root].ms = tree[root].ls = tree[root].rs = 0;
if(l == r) {
return;
}
int mid = (l + r)>>1;
clear(root<<1, l, mid);
clear(root<<1|1, mid+1, r);
}
void update(int root) {
tree[root].s = tree[root<<1].s + tree[root<<1|1].s;
tree[root].ms = max(tree[root<<1].ms, tree[root<<1|1].ms);
tree[root].ms = max(tree[root].ms, tree[root<<1].rs + tree[root<<1|1].ls);
tree[root].ls = max(tree[root<<1].ls, tree[root<<1].s + tree[root<<1|1].ls);
tree[root].rs = max(tree[root<<1|1].rs, tree[root<<1|1].s + tree[root<<1].rs);
}
void change(int root, int pos, int num) {
if(tree[root].l == tree[root].r && tree[root].l == pos) {
tree[root].rs += num;
tree[root].s = tree[root].ms = tree[root].ls = tree[root].rs;
return;
}
int mid = (tree[root].l + tree[root].r)>>1;
if(pos <= mid) change(root<<1, pos, num);
else change(root<<1|1, pos, num);
update(root);
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
yl.clear();
xl.clear();
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].w);
yl.push_back(p[i].y);
xl.push_back(p[i].x);
}
sort(yl.begin(), yl.end());
sort(xl.begin(), xl.end());
yl.erase(unique(yl.begin(), yl.end()), yl.end());
xl.erase(unique(xl.begin(), xl.end()), xl.end());
sort(p + 1, p + 1 + n, cmp);
for(int i = 1; i <= n; i++) {
p[i].x = getXId(p[i].x);
p[i].y = getYId(p[i].y);
}
int nx = xl.size(), ny = yl.size();
long long ans = -1e18;
int last = 1, k;
for(int i = 1; i <= ny; i++) { //枚举下界
clear(1, 1, nx);
k = last;
for(int j = i; j <= ny; j++) { //从下界开始依次扫描上界
for(; k <= n && p[k].y == j; k++) {//把y坐标等于j的点加入线段树
change(1, p[k].x, p[k].w);
}
if(j == i)
last = k;
ans = max(ans, tree[1].ms);
}
}
printf("%lld\n", ans);
}
}