Snowy Smile
Problem Description
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.
Input
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.
Output
For each test case, print a single line containing an integer, denoting the maximum total value.
Sample Input
2
4
1 1 50
2 1 50
1 2 50
2 2 -500
2
-1 1 5
-1 1 1
Sample Output
100
6
思路
题目的意思是让我们在给定的n个坐标的一个平面图中,找出一个矩形,它所框住的点的值的和最大,求最大子矩阵和。虽然只有2000个点,但是坐标却能达到 ±109大小,普通的二位数组记录不下来,需要对录入的数据先进行离散化,再储存的结构体里排序,用线段树维护最大前缀,后缀,区间最值,区间和。
代码
#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 2000+5;
typedef long long ll;
int t, n, vec_length, last;
int x[maxn], y[maxn], w[maxn];
ll ans;
vector<int> xl,yl;
struct node1 {
ll sum,lm,rm,tm;
} tree[4 * maxn];
struct node2 {
int x,y,w;
} v[maxn];
bool cmp(node2 a,node2 b) {
return a.y == b.y ? a.x < b.x : a.y > b.y;
}
ll getXId(ll x) {
return lower_bound(xl.begin(), xl.end(), x) - xl.begin() + 1;
}
ll getYId(ll x) {
return lower_bound(yl.begin(), yl.end(), x) - yl.begin() + 1;
}
void build_tree(int node, int l, int r) {
tree[node].lm = tree[node].rm = tree[node].tm = tree[node].sum = 0;
if(l == r) {
return;
}
int mid = (l + r) / 2;
int left_node = 2 * node;
int right_node = 2 * node + 1;
build_tree(left_node, l, mid);
build_tree(right_node, mid+1, r);
}
void update_tree(int node, int l, int r, int pos, int val) {
if(l == r) {
tree[node].lm = tree[node].rm = tree[node].tm = tree[node].sum = tree[node].sum + val;
return;
}
int mid = (l + r) / 2;
int left_node = 2 * node;
int right_node = 2 * node + 1;
if(pos > mid) {
update_tree(right_node, mid+1, r, pos, val);
} else {
update_tree(left_node, l, mid, pos, val);
}
tree[node].sum = tree[left_node].sum + tree[right_node].sum;
tree[node].lm = max(tree[left_node].lm, tree[left_node].sum + tree[right_node].lm);
tree[node].rm = max(tree[right_node].rm, tree[right_node].sum + tree[left_node].rm);
tree[node].tm = max(max(tree[left_node].tm, tree[right_node].tm), tree[left_node].rm + tree[right_node].lm);
}
int main() {
while(scanf("%d", &t) != EOF) {
while(t--) {
xl.clear();
yl.clear();
scanf("%d", &n);
for(int i=1; i<=n; i++) {
scanf("%d%d%d", &x[i], &y[i], &w[i]);
xl.push_back(x[i]);
yl.push_back(y[i]);
}
sort(xl.begin(), xl.end());
sort(yl.begin(), yl.end());
xl.erase(unique(xl.begin(), xl.end()), xl.end());
yl.erase(unique(yl.begin(), yl.end()), yl.end());
vec_length = xl.size();
for(int i=1; i<=n; i++) {
v[i].x = getXId(x[i]);
v[i].y = getYId(y[i]);
v[i].w = w[i];
}
sort(v+1,v+n+1,cmp);
// for(int i=1; i<=n; i++) {
// cout << v[i].x << " " << v[i].y << " " << v[i].w << endl;
// }
//离散化结束
ans = 0, last = -1;
for(int i=1; i<=n; i++) {
if(v[i].y == last) {
continue;
}
build_tree(1, 1, vec_length);
int k;
for(int j=i; j<=n; j=k) {
for(k=j; k<=n && v[k].y == v[j].y; k++)
update_tree(1, 1, vec_length, v[k].x, v[k].w);
ans = max(ans, tree[1].tm);
}
last = v[i].y;
}
cout << ans << endl;
}
}
}
/*
100
4
1 1 50
2 1 50
1 2 50
2 2 -500
2
-1 1 5
-1 1 1
4
-1 25 123
-3 36 546
0 0 908
1 14 682
*/
题目来源
HDU 6638 Snowy Smile [2019 Multi-University Training Contest 6]