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 1Sample Output
100 6
题目大意:给一些点以及点权值,要求你画一个矩形,使得矩形内的所有点权值的和最大。
解题思路: 对于给出的点将其纵坐标离散化,横坐标排序,暴力枚举横坐标的所有区间,在纵坐标上找出一个最大连续子段和即可。所以在找最大连续区间子段和的时候,使用线段树更新维护。
线段树求最大连续区间子段和详见博客https://blog.csdn.net/weixin_41190227/article/details/96969771
/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const ll Maxn = 2000 + 10 ;
const ll INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const ull seed = 133 ;
#define lson l, mid, ri << 1
#define rson mid + 1, r, ri << 1 | 1
struct message {
ll x, y ;
ll val ;
message(){} ;
message(ll xx, ll yy, ll _val) : x(xx), y(yy), val(_val) {}
}a[Maxn];
struct Node {
ll l, r ;
ll lsum, rsum ;
ll sum, maxSum ;
}no[Maxn << 2];
bool cmp (message a, message b){
return a.x < b.x ;
}
ll ay[Maxn] ;
void pushUp(ll ri){
no[ri].sum = no[ri << 1].sum + no[ri << 1 | 1].sum ;
no[ri].lsum = max(no[ri << 1].lsum, no[ri << 1].sum + no[ri << 1 | 1].lsum) ;
no[ri].rsum = max(no[ri << 1].rsum + no[ri << 1 | 1].sum, no[ri << 1 | 1].rsum) ;
no[ri].maxSum = max(max(no[ri << 1].maxSum, no[ri << 1 | 1].maxSum), no[ri << 1].rsum + no[ri << 1 | 1].lsum) ;
}
void Build(ll l, ll r, ll ri) {
no[ri].l = l ;
no[ri].r = r ;
no[ri].sum = no[ri].lsum = no[ri].rsum = no[ri].maxSum = 0 ;
if (l == r) return ;
ll mid = (l + r) >> 1 ;
Build(lson) ;
Build(rson) ;
}
void update(ll ri, ll k, ll val){
if (no[ri].l == no[ri].r) {
no[ri].sum = no[ri].lsum = no[ri].rsum = no[ri].maxSum = no[ri].maxSum + val ;
return ;
}
ll mid = (no[ri].l + no[ri].r) >> 1 ;
if (k <= mid) update(ri << 1, k, val) ;
else update(ri << 1 | 1, k, val) ;
pushUp(ri) ;
}
int main (){
ll T, x, y, val ;
cin >> T ;
while (T--){
ll n ;
cin >> n ;
for (ll i = 1; i <= n; i++){
cin >> x >> y >> val ;
a[i] = message(x, y, val) ;
ay[i] = y ;
}
sort(ay + 1, ay + n + 1) ;
ll len = unique(ay + 1, ay + n + 1) - ay - 1 ;
for (ll i = 1; i <= n; i++){
ll index = lower_bound(ay + 1, ay + len + 1, a[i].y) - ay ;
a[i].y = index ;
}
sort(a + 1, a + n + 1, cmp) ;
ll ans = 0 ;
for (ll i = 1; i <= n; i++){
if (i != 1 && a[i].x == a[i - 1].x) continue ;
Build(1, len, 1) ;
for (ll j = i; j <= n; j++){
if (j != i && a[j].x != a[j - 1].x) {
ans = max(ans, no[1].maxSum) ;
}
update(1, a[j].y, a[j].val) ;
}
ans = max(ans, no[1].maxSum) ;
}
cout << ans << endl ;
}
return 0 ;
}