题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638

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
 
 
将点按纵坐标从小到大排序并将点离散化,枚举纵坐标确定矩形下边界,依次往后向线段树加入纵坐标相同的点,每加完纵坐标相同的点(确认矩形上边界)就求一次x轴的最大字段和并更新答案
#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 线段树求最大子段和的更多相关文章

  1. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  2. UVA 11983 Weird Advertisement --线段树求矩形问题

    题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...

  3. BNU 2418 Ultra-QuickSort (线段树求逆序对)

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...

  4. hdu 1394 (线段树求逆序数)

    <题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...

  5. xdoj-1324 (区间离散化-线段树求区间最值)

    思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i]  覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...

  6. 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)

    题目链接:http://codevs.cn/problem/4163/ 题目:

  7. poj2299 Ultra-QuickSort(线段树求逆序对)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  8. HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)

    链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...

  9. HDU_1394_Minimum Inversion Number_线段树求逆序数

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

随机推荐

  1. 【React】 npm 常用的插件

    npm install –save-dev    package.json   安装环境 https://segmentfault.com/a/1190000008489881  全局 https:/ ...

  2. linux 禁止单个中断

    有时(但是很少!)一个驱动需要禁止一个特定中断线的中断递交. 内核提供了 3 个函数为 此目的, 所有都声明在 <asm/irq.h>. 这些函数是内核 API 的一部分, 因此我们描述它 ...

  3. JS的防抖与节流学习笔记

    防抖(debounce):当持续触发事件时,在一定的时间段内,只有最后一次触发的事件才会执行. 例: function debounce(fn, wait) { var timer = null; r ...

  4. jquery ajax请求步骤

    $.ajax({ type: "GET", url: "/alink-hq/checkCode", data: { "mobile": ph ...

  5. Linux 内核中的数据类型

    在我们进入更高级主题之前, 我们需要停下来快速关注一下可移植性问题. 现代版本的 Linux 内核是 高度可移植的, 它正运行在很多不同体系上. 由于 Linux 内核的多平台特性, 打算做认真使用的 ...

  6. Linux 内核提交 urb

    一旦 urb 被正确地创建,并且被 USB 驱动初始化, 它已准备好被提交给 USB 核心来发送 出到 USB 设备. 这通过调用函数 usb_submit_urb 实现: int usb_submi ...

  7. 【2016常州一中夏令营Day3】

    小 W 摆石子[问题描述]小 W 得到了一堆石子,要放在 N 条水平线与 M 条竖直线构成的网格的交点上.因为小 M 最喜欢矩形了,小 W 希望知道用 K 个石子最多能找到多少四边平行于坐标轴的长方形 ...

  8. ASP.NET Core 连接 GitLab 与 MatterMost 打造 devops 工具

    在现代化开发工具链里面就包含了自动化的通讯工具,而日志写代码我是推到 Gitlab 平台上,我今天听了郭锐大佬的分享之后,感觉我现在的团队的自动化做的远远不够.我在他的课程上学到的最重要一句话就是做工 ...

  9. jedis 连接池工具类

    maven <properties> <jedis.version>3.0.1</jedis.version> <junit.verion>4.12&l ...

  10. Python9_类

    类的基础知识 属性:类变量.实例变量.方法:初始化方法 __init__  //初始化方法不是必须的:其他方法: //类的定义class Employee: empCount = 0 //类变量,有些 ...