题目链接: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. H3C RIPv2配置举例

  2. 2018-2-13-win10-uwp-unix-timestamp-时间戳-转-DateTime

    title author date CreateTime categories win10 uwp unix timestamp 时间戳 转 DateTime lindexi 2018-2-13 17 ...

  3. H3C 计算子网内可用主机地址数

  4. Vue中通过属性绑定为元素绑定style行内样式

    1.直接在元素上通过:style的形式,书写样式对象 2.将样式对象定义在data中,并直接引用到:style中 3.在:style中通过数组,引用多个data上的样式对象

  5. jQuery 工具类函数-使用$.extend()扩展工具函数

    调用名为$. extend的工具函数,可以对原有的工具函数进行扩展,自定义类级别的jQuery插件,调用格式为: $. extend ({options}); 参数options表示自定义插件的函数内 ...

  6. Spark MLlib 示例代码阅读

    阅读前提:有一定的机器学习基础, 本文重点面向的是应用,至于机器学习的相关复杂理论和优化理论,还是多多看论文,初学者推荐Ng的公开课 /* * Licensed to the Apache Softw ...

  7. slim中的请求头

    请求头 每个 HTTP 请求都有请求头.这些元数据描述了 HTTP 请求,但在请求体中不可见.Slim 的 PSR 7 请求对象提供了几个检查请求头的方法. 获取所有的请求头,返回一个数组:getHe ...

  8. Vijos1774 机器翻译 [模拟]

    1.题意:给定一段长度为N个单词的文章(一个单词用一个非负整数表示),可以使用一个容量为M个元素的容器.你的任务是使用字典的帮助翻译文章,遇到一个单词,查询之后将此单词的释义放入容器中,下次遇到时若此 ...

  9. python列表的增删查改

    添加新的元素 append() insert() extend() +号 删除元素 pop() remove() del xxx[index] 修改 xxx[index] = value 查找 in. ...

  10. Kubernetes从私有镜像仓库中拉取镜像

    当我们尝试从私有仓库中拉取镜像时,可能会收到这样提示:requested access to the resource is denied Error response from daemon: pu ...