hdu6638 Snowy Smile(线段树+最大子段和)

题目链接

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.

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(−1e9≤xi,yi,wi≤1e9), 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

思路

先离散化。再枚举矩阵的上下界。固定上界,枚举下界。

w1w2w3w4
w5w6w7w8
w9w10w11w12

a[]=

w1+w5+w9w2+w6+w10w3+w7+w11w4+w8+w12

如图,用一个序列a[]实时统计从当前上界到下界的元素之和。那么序列a[]的最大子段和就是当前上界下界的所有矩形中最大的权值和。序列a[]的最大子段和可用线段树维护。

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const ll N=2010;
ll n,nx,ny;
struct node{
    ll x,y,ls,rs,s,ms;
};
node tree[N<<2];

void init(){
	ll i;
	for(i=0;i<(N<<2);i++){
		tree[i].x=tree[i].y=tree[i].ls=tree[i].rs=tree[i].s=tree[i].ms=0;
	}
}

struct nodes{
	ll x,y,w;
}p[N];

bool cmp1(nodes u,nodes v){
	return u.x<v.x;
}

bool cmp2(nodes u,nodes v){
	return u.y<v.y;
}

bool cmp(nodes u,nodes v){
	if(u.x==v.x){
		return u.y<v.y;
	}
	else{
		return u.x<v.x;
	}
}

ll d1[N],d2[N];
void discret(){
	ll i;
	sort(p+1,p+1+n,cmp1);		
	memset(d1,0,sizeof(d1));
	d1[1]=1;
	for(i=2;i<=n;i++){
		if(p[i].x==p[i-1].x)d1[i]=d1[i-1];
		else{
			d1[i]=d1[i-1]+1;
		}
	}
	for(i=1;i<=n;i++)p[i].x=d1[i];		
	memset(d2,0,sizeof(d2));
	d2[1]=1;	
	sort(p+1,p+1+n,cmp2);
	for(i=2;i<=n;i++){
		if(p[i].y==p[i-1].y)d2[i]=d2[i-1];
		else{
			d2[i]=d2[i-1]+1;
		}
	}
	for(i=1;i<=n;i++)p[i].y=d2[i];
	nx=d1[n];ny=d2[n];
	sort(p+1,p+1+n,cmp);
}


void update(ll bh){
    tree[bh].ms=max(tree[bh<<1].ms,tree[(bh<<1)+1].ms);
    tree[bh].ms=max(tree[bh].ms,tree[bh<<1].rs+tree[(bh<<1)+1].ls);
    tree[bh].ls=max(tree[bh<<1].ls,tree[bh<<1].s+tree[(bh<<1)+1].ls);
    tree[bh].rs=max(tree[(bh<<1)+1].rs,tree[(bh<<1)+1].s+tree[bh<<1].rs);
    tree[bh].s=tree[bh<<1].s+tree[(bh<<1)+1].s;
}

void build(ll bh,ll l,ll r){
    tree[bh].x=l; tree[bh].y=r;
	if (l==r){
        tree[bh].s=tree[bh].ms=tree[bh].ls=tree[bh].rs=0;
        return;
    }
    ll mid=(l+r)>>1;
    build(bh<<1,l,mid);
    build((bh<<1)+1,mid+1,r);
    update(bh); 
}

void change(ll bh,ll mb,ll z) 
{
    if (tree[bh].x==tree[bh].y&&tree[bh].x==mb){
        tree[bh].s+=z;
		tree[bh].ms+=z;
		tree[bh].ls+=z;
		tree[bh].rs+=z;
        return;
    }
    ll mid=(tree[bh].x+tree[bh].y)>>1;
    if (mb<=mid) change(bh<<1,mb,z);
    else change((bh<<1)+1,mb,z);
    update(bh);
}

ll askl(ll bh,ll l,ll r){
    if (tree[bh].x==l&&tree[bh].y==r) return tree[bh].ls;
    ll mid=(tree[bh].x+tree[bh].y)>>1;
    if (r<=mid) askl(bh<<1,l,r);
    else if (l>mid) askl((bh<<1)+1,l,r);
    ll lans=(bh<<1,l,mid); 
    ll rans=((bh<<1)+1,mid+1,r); 
    return max(lans,rans+tree[bh<<1].s); 
}

ll askr(ll bh,ll l,ll r){
    if (tree[bh].x==l&&tree[bh].y==r) return tree[bh].rs;
    ll mid=(tree[bh].x+tree[bh].y)>>1;
    if (r<=mid) askr(bh<<1,l,r);
    else if (l>mid) askr((bh<<1)+1,l,r);
    ll lans=askr(bh<<1,l,mid);
    ll rans=askr((bh<<1)+1,mid+1,r);  
    return max(rans,lans+tree[(bh<<1)+1].s);
}

ll ask(ll bh,ll l,ll r){
    if (tree[bh].x==l&&tree[bh].y==r) return tree[bh].ms;
    ll mid=(tree[bh].x+tree[bh].y)>>1;
    if (r<=mid) ask(bh<<1,l,r);
    else if (l>mid) ask((bh<<1)+1,l,r);
    ll lans=ask(bh<<1,l,mid);
    ll rans=ask((bh<<1)+1,mid+1,r);
    ll ans=max(lans,rans);
    return max(ans,askr(bh<<1,l,mid)+askl((bh<<1)+1,mid+1,r)); 
}

int main()
{
    ll i,j,k,T;
    scanf("%lld",&T);
    while(T--){
    	ll ans=0;
		scanf("%lld",&n);
		for(i=1;i<=n;i++){
			scanf("%lld%lld%lld",&p[i].x,&p[i].y,&p[i].w);
		}	
		discret();
		for(i=1;i<=nx;i++){
			init();
			build(1,1,n);
			k=1;
			while(p[k].x<i)k++;
			for(j=i;j<=nx;j++){
				if(k>n)break;
				while(p[k].x==j){
					change(1,p[k].y,p[k].w);
					k++;
					if(k>n)break;
				}
				ans=max(ans,ask(1,1,n));	 
			}
		}
		cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值