Snowy Smile
Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2996 Accepted Submission(s): 953
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从小到大排序,枚举每个x作为上边界的时候能得到的最大,离散化y轴,枚举x的时候可以在对应的y点添加对应的w值,线段树维护y轴上的最大子段和(区间合并的写法)。
#include <iostream>
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=10010;
ll t2[maxn];
int len2;
struct point
{
ll x,y;
ll w;
}a[maxn];
void init_hash2(int N)
{
/*for(int i=1;i<=N;i++)
{
t2[i]=a[i].y;
}*/
sort(t2+1,t2+1+N);
len2=unique(t2+1,t2+1+N)-t2-1;
}
int hashs2(ll x)
{
return lower_bound(t2+1,t2+1+len2,x)-t2;
}
bool cmp(point aa,point bb)
{
return aa.x<bb.x;
}
struct node
{
ll mx,lx,rx;
ll sum;
int l,r;
}tree[maxn<<2];
void pushup(int rt)
{
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
tree[rt].lx=max(tree[rt<<1].lx,tree[rt<<1].sum+tree[rt<<1|1].lx);
tree[rt].rx=max(tree[rt<<1|1].rx,tree[rt<<1|1].sum+tree[rt<<1].rx);
tree[rt].mx=max(tree[rt<<1].mx,tree[rt<<1|1].mx);
tree[rt].mx=max(tree[rt].mx,tree[rt<<1].rx+tree[rt<<1|1].lx);
}
void build(int rt,int l,int r)
{
tree[rt].l=l;tree[rt].r=r;
tree[rt].lx=tree[rt].rx=tree[rt].mx=tree[rt].sum=0;
if(l==r) return ;
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
}
void update(int rt,int p,ll v)
{
if(tree[rt].l==tree[rt].r)
{
tree[rt].lx+=v;
tree[rt].rx+=v;
tree[rt].mx+=v;
tree[rt].sum+=v;
return;
}
int mid=(tree[rt].l+tree[rt].r)>>1;
if(mid>=p) update(rt<<1,p,v);
else update(rt<<1|1,p,v);
pushup(rt);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld %lld %lld",&a[i].x,&a[i].y,&a[i].w);
t2[i]=a[i].y;
}
sort(a+1,a+n+1,cmp);
init_hash2(n);
ll ans=0;
for(int i=1;i<=n;i++)
{
if(i!=1&&a[i].x==a[i-1].x) continue;//相同的x枚举一次就好
build(1,1,len2);
for(int j=i;j<=n;j++)
{
if(j!=i&&a[j].x!=a[j-1].x)//同一个x的点应该添加完才查询,否则可能会出现矩形无法圈出来的点的集合
{
ans=max(ans,tree[1].mx);
}
int pos=hashs2(a[j].y);
update(1,pos,a[j].w);
}
ans=max(ans,tree[1].mx);
}
printf("%lld\n",ans);
}
return 0;
}