题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638
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.
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 .
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
将点按纵坐标从小到大排序并将点离散化,枚举纵坐标确定矩形下边界,依次往后向线段树加入纵坐标相同的点,每加完纵坐标相同的点(确认矩形上边界)就求一次x轴的最大字段和并更新答案
#include<iostream> #include<algorithm> using namespace std; #define maxn 2005 #define ll long long #define ls l,mid,rt<<1 #define rs mid+1,r,rt<<1|1 struct node{ ll sum,lsum,rsum,msum; }tr[maxn<<2]; struct ww{ ll x,y,val; bool operator <(const ww &w)const{ if(y==w.y)return x<w.x; return y<w.y; } }a[maxn]; inline void pushup(int rt) { tr[rt].sum=tr[rt<<1].sum+tr[rt<<1|1].sum; tr[rt].lsum=max(tr[rt<<1].lsum,tr[rt<<1].sum+tr[rt<<1|1].lsum); tr[rt].rsum=max(tr[rt<<1|1].rsum,tr[rt<<1|1].sum+tr[rt<<1].rsum); tr[rt].msum=max(max(tr[rt<<1].msum,tr[rt<<1|1].msum),tr[rt<<1|1].lsum+tr[rt<<1].rsum); } inline void build(int l,int r,int rt) { if(l==r) { tr[rt].sum=tr[rt].msum=tr[rt].lsum=tr[rt].rsum=0; return ; } int mid=l+r>>1; build(ls);build(rs); pushup(rt); } inline void update(int L,int c,int l,int r,int rt) { if(l==r) { tr[rt].sum=tr[rt].msum=tr[rt].lsum=tr[rt].rsum+=1ll*c; return ; } int mid=l+r>>1; if(L<=mid)update(L,c,ls); else update(L,c,rs); pushup(rt); } ll x[maxn],y[maxn]; int main() { int t; cin>>t; while(t--) { int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i].x>>a[i].y>>a[i].val; x[i]=a[i].x;y[i]=a[i].y; } sort(x+1,x+1+n); sort(y+1,y+1+n); int lenx=unique(x+1,x+1+n)-x-1; int leny=unique(y+1,y+1+n)-y-1; for(int i=1;i<=n;i++) { a[i].x=lower_bound(x+1,x+1+lenx,a[i].x)-x; a[i].y=lower_bound(y+1,y+1+leny,a[i].y)-y; } sort(a+1,a+1+n); ll ans=0; for(int i=1;i<=leny;i++)//确定矩形下边界 { build(1,lenx,1); int pos=1; while(a[pos].y<i)pos++; for(int j=i;j<=leny;j++)//确定矩形上边界 { for(;a[pos].y==j;pos++)//插入纵坐标相同的点 { update(a[pos].x,a[pos].val,1,lenx,1); } ans=max(ans,tr[1].msum);//更新答案 } } cout<<ans<<endl; } return 0; }