题目链接

Snowy Smile

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

题意

平面上有n个点,每个点有价值\(w_i\),可以任意选一个矩形,获取矩形内所有点的值,求最大的价值和为多少

题解

先对所有点坐标离散化,枚举矩形上界,对于上界及以下的点,以y坐标相等的点为一组,按y从大到小,一组一组的插入线段树,每插入完一组点,用线段树求出当前的最大子段和,整个过程相当于在枚举矩形上下界,利用线段树维护最大子段和。

线段树每个节点维护:区间和,左端点向右最大子段和,右端点向左最大子段和,区间最大子段和,用类似区间合并的方式合并

#include <bits/stdc++.h>using namespace std;
typedef long long ll;
const int mx = 2005;
const ll INF = 1e18;bool vis[mx][mx];struct Node {int x, y, w;int p, q;
}node[mx];vector <int> vx, vy;
vector <Node> mp[mx];int getidx(int x) {return lower_bound(vx.begin(), vx.end(), x) - vx.begin() + 1;
}int getidy(int y) {return lower_bound(vy.begin(), vy.end(), y) - vy.begin() + 1;
}struct Tree {ll sum;ll Lans, Rans, ans;
}tree[mx<<2];void pushUp(int rt) {tree[rt].ans = max(max(tree[rt<<1].ans, tree[rt<<1|1].ans), tree[rt<<1].Rans+tree[rt<<1|1].Lans);tree[rt].Lans = max(tree[rt<<1].Lans, tree[rt<<1].sum+tree[rt<<1|1].Lans);tree[rt].Rans = max(tree[rt<<1|1].Rans, tree[rt<<1|1].sum+tree[rt<<1].Rans);tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum;
}void build(int l, int r, int rt) {if (l == r) {tree[rt].sum = tree[rt].Lans = tree[rt].Rans = tree[rt].ans = 0;return;}int mid = (l + r) / 2;build(l, mid, rt<<1);build(mid+1, r, rt<<1|1);pushUp(rt);
}void update(int pos, int val, int l, int r, int rt) {if (l == r) {tree[rt].sum += val;tree[rt].Lans = tree[rt].Rans = tree[rt].ans = tree[rt].sum;return;}int mid = (l + r) / 2;if (pos <= mid) update(pos, val, l, mid, rt<<1);else update(pos, val, mid+1, r, rt<<1|1);pushUp(rt);
}int main() {int T;scanf("%d", &T);while (T--) {vx.clear(); vy.clear();int n;scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d%d%d", &node[i].x, &node[i].y, &node[i].w);vx.push_back(node[i].x);vy.push_back(node[i].y);}sort(vx.begin(), vx.end()); sort(vy.begin(), vy.end());vx.erase(unique(vx.begin(), vx.end()), vx.end());vy.erase(unique(vy.begin(), vy.end()), vy.end());for (int i = 1; i <= n; i++) {node[i].p = getidx(node[i].x);node[i].q = getidy(node[i].y);}for (int i = 1; i <= vy.size(); i++) mp[i].clear();for (int i = 1; i <= n; i++) mp[node[i].q].push_back(node[i]);ll ans = 0;for (int i = 1; i <= vy.size(); i++) {build(1, vx.size(), 1);for (int j = i; j <= vy.size(); j++) {for (int k = 0; k < mp[j].size(); k++) {Node tmp = mp[j][k];update(tmp.p, tmp.w, 1, vx.size(), 1);}ans = max(ans, tree[1].ans);}}printf("%lld\n", ans);}return 0;
}

转载于:https://www.cnblogs.com/bpdwn-cnblogs/p/11317316.html

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. 2019杭电暑假多校训练 第六场 Snowy Smile HDU - 6638

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

  4. 暑假N天乐【比赛篇】 —— 2019杭电暑期多校训练营(第六场)

    我胡汉三又滚回来了....保质期过了的题也得记下来. 以下题解包括: \[1002[HDU-6635] \\ 1005[HDU-6638] \\ 1006[HDU-6639] \\ 1008[HDU- ...

  5. 2019HDU多校补题

    心得:做不出,补不动 HUD第一场: 1001 Blank Y 1002 Operation Y 1003 Milk 1004 Vication Y 1005 Path Y 1006 Typewrit ...

  6. 杭电多校(六)2019.08.07--暑假集训

    [HDU 6634] UNSOLVED [HDU 6634] UNSOLVED [HDU 6636] UNSOLVED [HDU 6637] UNSOLVED [HDU 6638] UNSOLVED ...

  7. 2019 杭电多校第六场 题解

    比赛记录 注意随机数据 ,1-n排列这种,一般都有啥暴力重构之类的方法,期望重构次数很少之类的 1005也是这样,因为n^2但只有n个值有数,所以就可以n^2logn 题解 1001 Salty Fi ...

  8. hdu 过山车_从机械工程师到软件开发人员–我的编码过山车

    hdu 过山车 There aren't many people out there who grew up dreaming of writing code. I definitely didn't ...

  9. HDU 4389 - X mod f(x)

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4389 2012多校,第9场,1010 . 问题是,询问区间内 存在多少个 哈沙德数(Harshad ...

  10. hdu 4389 囧,打表

    http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 :一个数能被他各个位数之和整除则符合要求,给L,R,问区间里有多少个数符合要求. 囧,居然打表就能过 ...

最新文章

  1. 七夕要到了,用Python比心表白
  2. CSS3之利用选择器和content属性在页面中插入内容
  3. Spring(十二)之JDBC框架
  4. python 文件操作的模块_Python之文件操作修改模块
  5. C语言实例:3个数从小到大排序
  6. 国家开放大学2021春1127实用卫生统计学题目
  7. 遗传算法的交叉变异详解
  8. HTC ThunderBolt无法打开3G问题解决方法
  9. Struts入门经验(二)
  10. 通过PHP的Curl函数模拟Post获取内容
  11. 数据库系统概论完整笔记
  12. 图片转换js (img对象,file对象,base64,canvas对象),以及图片压缩方式
  13. 《全职高手》人物词频分析和词云图片生成
  14. 面试积累——嵌入式软件工程师面试题(非常经典)
  15. 关于二级域名与三级域名的解释
  16. java异常类_java中常见的异常类
  17. [转]项目实施过程中的风险控制
  18. 苹果5港行和大陆行货的区别
  19. 流程工业需要什么样的工控安全?
  20. An infrared and visible image fusion algorithm based on ResNet‑152

热门文章

  1. TOEFL wordlist 24
  2. android 计步器acc,利用腾讯云云函数执行部署修改小米运动步数代码_每天自动修改步数...
  3. (Java-11)简单的银行账户模拟
  4. 应届生年薪 40w 在杭州可以过上什么样的生活?
  5. laravel框架中Cache缓存类中的原子锁
  6. python多个文件打包成exe_多个py文件生成一个可运行exe文件
  7. android 调用webservice实现手机号码归属地查询
  8. linux让grep带颜色,在linux下给grep命令添加颜色
  9. MQTT协议(四) 【PUBLISH】发布消息
  10. 设置局域网内共享磁盘