题目:

There are nn pirate chests buried in Byteland, labeled by 1,2,…,n1,2,…,n. The ii-th chest's location is (xi,yi)(xi,yi), and its value is wiwi, wiwi can be negative since the pirate can add some poisonous gases into the chest. When you open the ii-th pirate chest, you will get wiwi 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)T(1≤T≤100), denoting the number of test cases.

In each test case, there is one integer n(1≤n≤2000)n(1≤n≤2000) in the first line, denoting the number of pirate chests.

For the next nn lines, each line contains three integers xi,yi,wi(−109≤xi,yi,wi≤109)xi,yi,wi(−109≤xi,yi,wi≤109), denoting each pirate chest.

It is guaranteed that ∑n≤10000∑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

题目大意

给你n(n<=2000)个点的坐标(x, y)以及该点的值(-1e9<val<1e9),让你找出一个矩形把它们框住,使得这个矩形内点值的和最大,求这个最大和。

解题思路

由于n小于2000,可以枚举这个矩形的左右两条边,然后对于这两条边内的点,我们用线段树维护最大子段和就可以了。注意要将坐标离散化一下。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cmath>using namespace std;
const int maxn = 2056;struct node{int x,y;long long w;
}p[maxn];
int numx[maxn];
int n,m,g;
long long ans;int idx(int x)
{return lower_bound(numx+1,numx+1+m,x)-numx;
}int cmp(node a,node b)
{return a.y<b.y;
}long long sum[maxn<<2],suml[maxn<<2],sumr[maxn<<2],ansSum[maxn<<2];
void add(int pos, long long val, int l, int r, int rt)
{
//    if(ans==150)
//    {
//        cout << l<<" "<<r<<" "<< val<<" "<< sum[rt]<<" "<< suml[rt]<<" "<< sumr[rt]<<" "<< ansSum[rt]<<endl;
//    }if(l==r){sum[rt]+=val;suml[rt]+=val;sumr[rt]+=val;ansSum[rt]+=val;return ;}int mid = (l+r)/2;if(pos<=mid) add(pos,val,l,mid,rt<<1);else add(pos,val,mid+1,r,rt<<1|1);ansSum[rt] = max(ansSum[rt<<1],ansSum[rt<<1|1]);ansSum[rt] = max(ansSum[rt],sumr[rt<<1]+suml[rt<<1|1]);sum[rt] = sum[rt<<1]+sum[rt<<1|1];suml[rt] = max(suml[rt<<1],sum[rt<<1]+suml[rt<<1|1]);sumr[rt] = max(sumr[rt<<1|1],sum[rt<<1|1]+sumr[rt<<1]);
}void init()
{memset(sum,0,sizeof(sum));memset(suml,0,sizeof(suml));memset(sumr,0,sizeof(sumr));memset(ansSum,0,sizeof(ansSum));}void solve()
{for(int l = 1; l <= n; l++){if(l!=1&&p[l].y==p[l-1].y)continue;init();int r = l;
//        cout <<"$$ "<< l<<" "<<r<<endl;for(r = l; r<= n; r++){if(r!=l&&p[r].y!=p[r-1].y)ans = max(ans,ansSum[1]);add(idx(p[r].x),p[r].w,1,m,1);
//            cout <<" ** "<<p[r].w<<endl;
//            cout <<"%% "<< p[r].y<<" "<<p[r+1].y<<endl;
//            if(r==n||p[r].y!=p[r+1].y){cout <<" "<< l<<" "<<r<<endl;
//                ans = max(ans,ansSum[1]);
//                if(ans==150)
//                    cout <<"$$ "<<l<<" "<<r<<endl;
//            }}//        cout <<"$$ "<< l<<" "<<r<<" "<<sum[1]<<" "<<m<<endl;ans = max(ans,ansSum[1]);}
}int main()
{int t;scanf("%d", &t);while(t--){scanf("%d", &n);for(int i = 1; i <= n; i++){scanf("%d%d%lld", &p[i].x, &p[i].y, &p[i].w);numx[i] = p[i].x;}sort(numx+1,numx+1+n);m = unique(numx+1,numx+1+n)-numx-1;sort(p+1,p+1+n,cmp);ans = 0;solve();printf("%lld\n", ans);}return 0;
}

HDU-6638 Snowy Smile 区间最大子段和相关推荐

  1. HDU 6638 [2019 Multi-University Training Contest 6]

    Snowy Smile Problem Description There are n pirate chests buried in Byteland, labeled by 1,2,-,n. Th ...

  2. HDU 6638二维扫描线+二维最大子段和+离线

    Snowy Smile HDU - 6638 题解看这位大佬,讲的很详细了. 传送门 反思: 本题做的时候,已经想到二维子段和了,但不知怎么维护. 看了题解,原来要用线段树,之后暴力算答案. 先只看纵 ...

  3. Can you answer these queries V SPOJ - GSS5 (分类讨论+线段树维护区间最大子段和)

    recursion有一个整数序列a[n].现在recursion有m次询问,每次她想知道Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 &l ...

  4. 区间最大子段和 + 最大子矩阵和 + 最大m子段和(DP)

    - 区间最大子段和(最大连续区间和) 动态规划解法: dp[i]表示以a[i]为结束的最大连续子段和 因为是以a[ i ]为结束且是连续子段 那么 dp[ i ] 要么就是 a[ i ]本身, 要么 ...

  5. 2019杭电暑假多校训练 第六场 Snowy Smile HDU - 6638

    很多题解都是简单带过,所以打算自己写一篇,顺便也加深自己理解 前置知识:线段树.线段树维护最大字段和.二维坐标离散化 题解: 1.很容易想到我们需要枚举所有子矩阵来得到一个最大子矩阵,所以我们的任务是 ...

  6. SP1043 GSS1 - Can you answer these queries I(线段树,区间最大子段和(静态))

    题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y} ...

  7. HDU 5115 Dire Wolf 区间dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5115 Dire Wolf Time Limit: 5000/5000 MS (Java/Others ...

  8. HDU 5693 D Game 区间dp

    D Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5693 Description 众所周知,度度熊喜欢的字符只有两个:B 和D. 今天,它 ...

  9. HDU 2836 (离散化DP+区间优化)

    Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/ ...

  10. hdu 5481(数学期望+区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5481 题解: 实际上求的是所有子集的并集长度之和. 把坐标离散化之后,可以单独考虑每一段区间在并集内部 ...

最新文章

  1. c、c++---linux上的GetTickCount函数
  2. ●BZOJ 1934 [Shoi2007]Vote 善意的投票
  3. javascript高程3 学习笔记(三)
  4. 1-第一个pyqt5程序
  5. dashboard windows 前端开发环境搭建
  6. 开发人员在编写 HTML 和 CSS 时最常犯的六大错误
  7. Eclipse JSP 页面设置 charset=UTF-8
  8. 张超超OC基础回顾01_类的创建,申明属性,以及本质
  9. crtmpserver 在VS2010下的build
  10. vecm matlab,VECM是什么?
  11. linux系统下载18.04,在Ubuntu 18.04系统中下载安装Persepolis Download Manager
  12. numeric比较大小 数据库_SQL数据库中Numeric(10,2)是什么意思?
  13. 电商直播元年 微媒云播打造私域流量火爆商业新模式
  14. 进程与程序的联系与区别
  15. 智能机器人的核心技术和技术指标总结
  16. python生成迁移文件
  17. Java 时间格式处理
  18. Flink Table和SQL的表和视图、Connectors和timestamp数据类型
  19. 赛扬处理器_首批15瓦四核处理器即将成为历史:英特尔宣布停产4个型号
  20. linux命令行下载cuda,linux 命令行下安装特定版本的 cuda (踩坑记录)

热门文章

  1. CAN bus的移植
  2. php正则表达式在线测试工具,在线测试正则表达式工具:适合asp.net vb.net cs.net等Web或者Windows程序,便于你快速编写正确正则表达式,提供正则表达式模板供参考。...
  3. python制作好看的界面_python漂亮界面
  4. 在线教育大数据营销平台实战(五):CRM线索培育机制及动态评分模型
  5. 科技爱好者周刊(第 163 期):你的城市有多少张病床?
  6. 【易通慧谷】盘点供应链金融的主要模式和对商业银行领域的影响
  7. VB中On Error Resume Next 什么意思,一般在什么情况下使用
  8. linux删除缓存文件swp,Vi下删除SWP文件
  9. c语言 continue什么意思,continue在C语言中什么意思?
  10. 土地利用分类详细教程——以高分一号影像为例(上)