Snowy Smile(线段树维护子矩阵最大值/降维打击维护子矩阵最大值(TLE))

http://acm.hdu.edu.cn/showproblem.php?pid=6638

问题 E: Snowy Smile

时间限制: 4 Sec  内存限制: 128 MB
提交: 54  解决: 9
[提交] [状态] [命题人:admin]

题目描述

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.

 

输入

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.

 

输出

For each test case, print a single line containing an integer, denoting the maximum total value.

 

样例输入

复制样例数据

2
4
1 1 50
2 1 50
1 2 50
2 2 -500
2
-1 1 5
-1 1 1

样例输出

100
6

将坐标离散化,按横坐标从小到大排序,枚举矩阵上边界,然后一层一层的插入(枚举下边界),n^2logn

hdu上能过,在石油大oj上TLE了,慢了2倍,……

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e16;
const int N=2e3+5;
struct node{
    int x,y;
    ll w;
}p[N];
struct TREE{
	ll ls,rs,ms;
	ll sum;
}tree[N<<2];
inline void pushup(int p){
	tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
	tree[p].ls=max(tree[p<<1].ls,tree[p<<1].sum+tree[p<<1|1].ls);
	tree[p].rs=max(tree[p<<1|1].rs,tree[p<<1|1].sum+tree[p<<1].rs);
	tree[p].ms=max(tree[p<<1].rs+tree[p<<1|1].ls,max(tree[p<<1].ms,tree[p<<1|1].ms));
}
void build(int l,int r,int p){
	if(l==r){
		tree[p].sum=tree[p].ls=tree[p].rs=tree[p].ms=0;
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,p<<1);
	build(mid+1,r,p<<1|1);
	pushup(p);
}
TREE query(int l,int r,int p,int nl,int nr){
	TREE a={-INF,-INF,-INF,-INF},b={-INF,-INF,-INF,-INF},ans={-INF,-INF,-INF,-INF};
	if(l>=nl&&r<=nr) return tree[p];
	int mid=(l+r)>>1;
	if(nl<=mid) a=query(l,mid,p<<1,nl,nr);
	if(nr>mid) b=query(mid+1,r,p<<1|1,nl,nr);
	ans.sum=a.sum+b.sum;
	ans.ls=max(a.ls,a.sum+b.ls);
	ans.rs=max(b.rs,b.sum+a.rs);
	ans.ms=max(a.rs+b.ls,max(a.ms,b.ms));
	return ans;	
}
void update(int l,int r,int p,int np,ll k){
	if(l==r){
		tree[p].sum+=k;
		tree[p].ls+=k;
		tree[p].rs+=k;
		tree[p].ms+=k;
		return ;
	}
	int mid=(l+r)>>1;
	if(np<=mid) update(l,mid,p<<1,np,k);
	else update(mid+1,r,p<<1|1,np,k);
	pushup(p);
}
bool cmpx(node a,node b){
    return a.x<b.x;
}
bool cmpy(node a,node b){
    return a.y<b.y;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d%d%lld",&p[i].x,&p[i].y,&p[i].w);
        }
        sort(p+1,p+1+n,cmpx);
        int idx=0;
        int last=-1e9-10;
        for(int i=1;i<=n;i++){
            if(p[i].x>last){
                last=p[i].x;
                p[i].x=++idx;    
            }
            else{
                p[i].x=idx;
            }
        }
        sort(p+1,p+1+n,cmpy);
        int idy=0;
        last=-1e9-10;
        for(int i=1;i<=n;i++){
            if(p[i].y>last){
                last=p[i].y;
                p[i].y=++idy;    
            }
            else{
                p[i].y=idy;
            }
        }
        ll ans=0;
        int pos;
        for(int i=1;i<=idy;i++){
        	for(int j=n;j>=1;j--){  //可以二分最后一个小于等于i的数的位置 
        		if(p[j].y<=i){
        			pos=j;
        			break;
				} 
			}
			build(1,idx,1);
			for(int j=i;j>=1;j--){
				while(pos&&p[pos].y>=j){
					update(1,idx,1,p[pos].x,p[pos].w);
					pos--;
				} 
				TREE tr=query(1,idx,1,1,idx);
				ll cnt=max(tr.ms,max(tr.ls,tr.rs));
				ans=max(ans,cnt);
			}
		}
        printf("%lld\n",ans);
    }    
    return 0;
}

将行或列压成一维,变成求最大子串和问题n^3,TLE 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e3+5;
struct node{
    int x,y;
    ll w;
}p[N];
bool cmpx(node a,node b){
    return a.x<b.x;
}
bool cmpy(node a,node b){
    return a.y<b.y;
}
ll dp[N][N];
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d%d%lld",&p[i].x,&p[i].y,&p[i].w);
        }
        sort(p+1,p+1+n,cmpx);
        int idx=0;
        int last=-1e9-10;
        for(int i=1;i<=n;i++){
            if(p[i].x>last){
                last=p[i].x;
                p[i].x=++idx;    
            }
            else{
                p[i].x=idx;
            }
        }
        sort(p+1,p+1+n,cmpy);
        int idy=0;
        last=-1e9-10;
        for(int i=1;i<=n;i++){
            if(p[i].y>last){
                last=p[i].y;
                p[i].y=++idy;    
            }
            else{
                p[i].y=idy;
            }
        }
        for(int i=1;i<=idx;i++){
            for(int j=1;j<=idy;j++){
                dp[i][j]=0;
            }
        }
        for(int i=1;i<=n;i++){
            dp[p[i].x][p[i].y]+=p[i].w;
        }
        for(int i=1;i<=idx;i++){
            for(int j=1;j<=idy;j++){
                dp[i][j]+=dp[i-1][j];
            }
        }
        ll ans=0;
        for(int i=1;i<=idx;i++){
            for(int j=1;j<=i;j++){
                ll sum=0,mx=0;
                for(int k=1;k<=idy;k++){
                    sum+=dp[i][k]-dp[j-1][k];
                    if(sum<0){
                        sum=0;
                    }
                    else{
                        mx=max(mx,sum);
                    }
                }
                ans=max(ans,mx);
            }
        }
        printf("%lld\n",ans);
    }    
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值