Snowy Smile HDU - 6638 利用线段树区间合并 求最大子段和

Snowy Smile

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2429    Accepted Submission(s): 774


 

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,y),以及该点的价值w,让你找一个平行于xy轴的矩阵,使得该矩阵包含的价值最大,输出该价值。

思路:最大子矩阵和,由于坐标过大,但只有2000个点可进行离散化。

2种会T的方法:

1.枚举矩阵的左上角,左下角,右上角,右下角,维护一个最大值。时间复杂度 :O(N^4)

2.DP,降维转化为最子字段和。时间复杂度:O(N^3)

正解:这是在2方法上的改进,暴力枚举区间的左端点x1和右端点x2复杂度是O(N^2),右端点枚举到x2时,把x坐标是x2的点插入到线段树,复杂度是O(logN),线段树查询当前最大子段和,复杂度是O(1)。总体时间复杂度为O(N^2*logN);

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2010;
int x[maxn],y[maxn],cntx,cnty,n;
struct Node{
	int l;
	int r;
	ll left;
	ll right;
	ll sum;
	ll maxx; 
}tree[maxn<<2];
struct node{
	int x;
	int y;
	ll w; 
}a[maxn];
bool cmp(node xx,node yy)
{
	return xx.x<yy.x;
}
void pushup(int cur)
{
	tree[cur].sum=tree[cur<<1].sum+tree[cur<<1|1].sum;
	tree[cur].left=max(tree[cur<<1].left,tree[cur<<1].sum+tree[cur<<1|1].left);
	tree[cur].right=max(tree[cur<<1|1].right,tree[cur<<1|1].sum+tree[cur<<1].right);
	tree[cur].maxx=max(tree[cur<<1].maxx,tree[cur<<1|1].maxx);
	tree[cur].maxx=max(tree[cur].maxx,tree[cur<<1].right+tree[cur<<1|1].left);
}
void build(int l,int r,int cur)
{
	tree[cur].l=l;
	tree[cur].r=r;
	tree[cur].left=0;
	tree[cur].right=0;
	tree[cur].sum=0;
	tree[cur].maxx=0;
	if(l==r) return ;
	int m=(l+r)>>1;
	build(l,m,cur<<1);
	build(m+1,r,cur<<1|1);
}
void update(int tar,ll val,int cur)
{
	if(tree[cur].l==tree[cur].r)
	{
		tree[cur].left+=val;
		tree[cur].right+=val;
		tree[cur].sum+=val;
		tree[cur].maxx+=val;
		return ;
	}
	if(tar<=tree[cur<<1].r) update(tar,val,cur<<1);
	else update(tar,val,cur<<1|1);
	pushup(cur);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		ll ans=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d%I64d",&a[i].x,&a[i].y,&a[i].w);
			x[i]=a[i].x;
			y[i]=a[i].y;
		}
		sort(x+1,x+1+n);
		sort(y+1,y+1+n);
		cntx=unique(x+1,x+1+n)-(x+1);
		cnty=unique(y+1,y+1+n)-(y+1);
		for(int i=1;i<=n;i++)
		{
			a[i].x=lower_bound(x+1,x+1+cntx,a[i].x)-x;
			a[i].y=lower_bound(y+1,y+1+cnty,a[i].y)-y;
		}
		sort(a+1,a+1+n,cmp);
		a[0].x=-1;
		for(int i=1;i<=n;i++)
		{
			if(a[i].x!=a[i-1].x)
			{
				build(1,cnty,1);
				for(int j=i;j<=n;j++)
				{
					update(a[j].y,a[j].w,1);
					if(a[j].x==a[j+1].x&&j!=n) continue;
					ans=max(ans,tree[1].maxx);
				}
			}
		}
		printf("%I64d\n",ans);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值