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
    评论
好的,你想了解关于使用FastAPI和Snowy搭建后台管理系统的内容,是吗?那么我会为你提供一些相关的信息。 首先,FastAPI是一个基于Python的高性能Web框架,它使用异步编程方式,支持单线程处理多个请求。相比于其他Python框架,FastAPI的运行速度更快,而且使用简单,易于上手。 而Snowy则是一个基于Vue.js的前端UI框架,它提供了一系列的组件和模板,可以快速搭建出美观、易用的后台管理系统。Snowy的特点是轻量级、易于扩展、响应式设计,很适合用于构建现代化的Web应用程序。 要使用FastAPI和Snowy搭建后台管理系统,你可以按照以下步骤进行: 1. 安装FastAPI和相关依赖 使用pip命令安装FastAPI和uvicorn(一个基于ASGI的Web服务器): ``` pip install fastapi uvicorn ``` 2. 创建FastAPI应用程序 在你的Python项目中创建一个FastAPI应用程序,可以使用类似下面的代码: ```python from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} ``` 这个例创建了一个简单的FastAPI应用程序,其中定义了一个处理HTTP GET请求的路由(/),返回一个JSON对象。 3. 运行FastAPI应用程序 在终端中使用uvicorn命令来运行FastAPI应用程序: ``` uvicorn main:app --reload ``` 这个命令将启动一个Web服务器,监听本地的8000端口,可以通过浏览器访问http://localhost:8000来测试你的应用程序。 4. 集成Snowy前端框架 通过npm命令安装Snowy框架: ``` npm install snowy ``` 在你的Vue.js应用程序中引入Snowy组件: ```javascript import Vue from 'vue' import Snowy from 'snowy' Vue.use(Snowy) ``` 现在你可以使用Snowy提供的组件来构建你的后台管理系统,例如表格、表单、图表等。你可以参考Snowy的文档来学习如何使用这些组件。 5. 开始构建后台管理系统 现在你可以开始使用FastAPI和Snowy来构建你的后台管理系统了。你可以在FastAPI中定义路由和API接口,提供数据服务,而在Snowy中构建前端页面,展示数据和交互。你可以使用Vue.js的数据绑定和事件处理机制,将前端页面和后端数据连接起来。 总之,使用FastAPI和Snowy搭建后台管理系统可以让你快速开发出高性能、易用的Web应用程序。它们都是开源的项目,拥有广泛的社区支持和生态环境,非常适合用于构建现代化的Web应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值