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 ...
随机推荐
- 51nod1370 排列与操作
性质:最终值域相同的一定是连续一段 花费最小?一定是值域个数个!并且当最后为i的数恰好只有i一个位置的时候,肯定选择不动,少花费一个 所以,我们考虑:每个最终方案在花费最小的方案下恰好被统计一次! 而 ...
- CF1151div2(Round 553)
CF1151div2(Round 553) 思路题大赛 A 少考虑了一种情况,到死没想到 B 貌似我随机化50000次,没找到就无解貌似也过了 感觉随随便便乱搞+分类讨论都可以过的样子 #includ ...
- vue-learning:20 - js - 区别:filters / data / computed / watch / methods
区别:filters / data / computed / watch / methods 在配置对象options中,filters/data/computed/watch/methods的每一项 ...
- CITRIX VPX配置四层负载
网络拓扑如下: Step1:开启四层负载特性 在Configuration->Traffic Management->Load Balancing上右键弹出菜单点击enable,如下图: ...
- LeetCode3_无重复字符的最长子串(数组&字符串问题)
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "ab ...
- Python网络编程笔记一
AF_INET:IPV4 AF_INET6:IPV6 套接字类型: SOCK_STREAM:TCP SOCK_DGRAM:UDP 创建TCP套接字,也可以不传递参数,默认创建TCP套接字 tcpSoc ...
- 16.MindManager整理交互思路
点住主题同时按ins键可以插入一个支节点 右键主题选择下方的排列图表 可以选择排列方式 按住主题同时ctr+v就会粘帖成一个子主题 文本也可以复制黏贴 主题内容可以直接选择拖动更改结构 选择主题框上的 ...
- 《HelloGitHub》第 45 期
兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...
- win32汇编简单实现窗口程序
.386 .model flat,stdcall option casemap:none ;========================== ;include部分 include windows. ...
- 使用这些idea插件让开发效率提高5倍
idea 有很多非常好用的插件,用好了这些插件能够极大的提高开发效率 插件用的好,bug 就追不上了我