Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
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个点,每个点都有价值,求一个矩形(包括边界)内所有点价值和的最大值
分析:
将所有点按照x排序,枚举矩形的上边界,按顺序将每一个点update,直到下一个点要换行时更新ans,上边界更新时要重新build,clear线段树,ans维护的是区间最大连续子段和。
TIPS:在这里留下网上看起来很不错的板子qwq
顾z的博客 感谢!
#include<bits/stdc++.h>
#define ll long long
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
const int maxn=2007;
struct node{ll x,y,w;} a[maxn];
bool cmp(node A,node B) {return A.x<B.x;}
ll c[maxn];
int n,m;
ll ssum[maxn<<2],lsum[maxn<<2],rsum[maxn<<2],tree[maxn<<2];
void build(int rt,int l,int r)
{
ssum[rt]=lsum[rt]=rsum[rt]=tree[rt]=0;
if(l==r) return;
int mid=(l+r)>>1;
build(lson,l,mid);build(rson,mid+1,r);
}
void pushup(int rt)
{
tree[rt]=tree[lson]+tree[rson];
ssum[rt]=max(max(ssum[lson],ssum[rson]),rsum[lson]+lsum[rson]);
lsum[rt]=max(lsum[lson],tree[lson]+lsum[rson]);
rsum[rt]=max(rsum[rson],tree[rson]+rsum[lson]);
}
void update(int rt,int l,int r,int pos,int val)
{
if(l==r && l==pos)
{
tree[rt]+=val;
if(tree[rt]>0) ssum[rt]=lsum[rt]=rsum[rt]=tree[rt];
else ssum[rt]=lsum[rt]=rsum[rt]=0;
return;
}
int mid=(l+r)>>1;
if(pos<=mid) update(lson,l,mid,pos,val);
else update(rson,mid+1,r,pos,val);
pushup(rt);
}
int main()
{
int t;scanf("%d",&t);
while (t--)
{
int n;scanf("%d",&n);
int tot=0;
for (int i=1;i<=n;i++)
{
ll x,y,w;scanf("%lld%lld%lld",&x,&y,&w);
c[++tot]=y; a[i]={x,y,w};
}
sort(c+1,c+1+tot);
int m=unique(c+1,c+1+tot)-(c+1);
sort(a+1,a+1+n,cmp);
for (int i=1;i<=n;i++) a[i].y=lower_bound(c+1,c+1+m,a[i].y)-c;
ll ans=0;
for (int i=1;i<=n;i++)
{
if(i==1 || a[i].x!=a[i-1].x)
{
build(1,1,m);
for (int j=i,k;j<=n;j=k)
{
for (k=j;k<=n && a[k].x==a[j].x;k++) update(1,1,m,a[k].y,a[k].w);
ans=max(ans,ssum[1]);
}
}
}
printf("%lld\n",ans);
}
return 0;
}