hdu6638 线段树求最大子段和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638
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.
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 .
integer, denoting the maximum total value.
#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<<];
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<<].sum+tr[rt<<|].sum;
tr[rt].lsum=max(tr[rt<<].lsum,tr[rt<<].sum+tr[rt<<|].lsum);
tr[rt].rsum=max(tr[rt<<|].rsum,tr[rt<<|].sum+tr[rt<<].rsum);
tr[rt].msum=max(max(tr[rt<<].msum,tr[rt<<|].msum),tr[rt<<|].lsum+tr[rt<<].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=;
return ;
}
int mid=l+r>>;
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>>;
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=;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+,x++n);
sort(y+,y++n);
int lenx=unique(x+,x++n)-x-;
int leny=unique(y+,y++n)-y-;
for(int i=;i<=n;i++)
{
a[i].x=lower_bound(x+,x++lenx,a[i].x)-x;
a[i].y=lower_bound(y+,y++leny,a[i].y)-y;
}
sort(a+,a++n);
ll ans=;
for(int i=;i<=leny;i++)//确定矩形下边界
{
build(,lenx,);
int pos=;
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,,lenx,);
}
ans=max(ans,tr[].msum);//更新答案
}
}
cout<<ans<<endl;
}
return ;
}
hdu6638 线段树求最大子段和的更多相关文章
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- UVA 11983 Weird Advertisement --线段树求矩形问题
题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...
- BNU 2418 Ultra-QuickSort (线段树求逆序对)
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...
- hdu 1394 (线段树求逆序数)
<题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...
- xdoj-1324 (区间离散化-线段树求区间最值)
思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i] 覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...
- 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)
题目链接:http://codevs.cn/problem/4163/ 题目:
- poj2299 Ultra-QuickSort(线段树求逆序对)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)
链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...
- HDU_1394_Minimum Inversion Number_线段树求逆序数
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- Python--day38---进程间通信--初识队列(multiprocess.Queue)之生产者,消费者模型
1,生产者消费者模型.py import random import time from multiprocessing import Queue, Process def producer(name ...
- P1054 全组合
题目描述 给定n,m,按字典序输出所有从1-n里面取出m个数的组合.比如从1-3里面取出2个的组合是: 1 2 1 3 2 3 输入格式 输入两个数n,m.其中 \(0<n<=10,0&l ...
- jQuery 工具类函数-检测浏览器是否属于W3C盒子模型
浏览器的盒子模型分为两类,一类为标准的w3c盒子模型,另一类为IE盒子模型,两者区别为在Width和Height这两个属性值中是否包含padding和border的值,w3c盒子模型不包含,IE盒子模 ...
- CSS 高度居中方案
实现高度自适应并且上下居中 <div id="wrap"> <div class="box">DemoSeat</div> ...
- 2018-11-17-win10-uwp-在-xaml-让-TextBlock-换行
title author date CreateTime categories win10 uwp 在 xaml 让 TextBlock 换行 lindexi 2018-11-17 16:2:29 + ...
- Python9_类
类的基础知识 属性:类变量.实例变量.方法:初始化方法 __init__ //初始化方法不是必须的:其他方法: //类的定义class Employee: empCount = 0 //类变量,有些 ...
- Hibernate映射文件详解(News***.hbm.xml)一
Hibernate是一个彻底的ORM(Object Relational Mapping,对象关系映射)开源框架. 我们先看一下官方文档所给出的,Hibernate 体系结构的高层视图: 其中PO=P ...
- vim 方式快捷编辑代码
说明 **I: ** 行首插入 **a: ** 追加 **A: ** 行尾插入 **R: ** 替换文字 **v: ** 选择 **ctrl-v: ** 选择举行区域 **x: ** 删除 **dd: ...
- Python - Tuple 怎么用,为什么有 tuple 这种设计?
背景 看到有同学很执着的用 tuple,想起自己刚学 python 时,也是很喜欢 tuple,为啥?因为以前从来没见过这种样子的数据 (1,2), 感觉很特别,用起来也挺好用 i,j=(1,2), ...
- Redis 都有哪些数据类型?分别在哪些场景下使用比较合适?
redis 主要有以下几种数据类型: string hash list set sorted set string 这是最简单的类型,就是普通的 set 和 get,做简单的 KV 缓存. set c ...