hdu-6638 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;
}
hdu-6638 Snowy Smile的更多相关文章
- HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...
- 最大矩阵覆盖权值--(静态连续最大子段 (线段树) )-HDU(6638)Snowy Smile
这题是杭电多校2019第六场的题目 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意:给你平面上n个点,每个点都有权值(有负权),让你计算一 ...
- 2019杭电多校第六场hdu6638 Snowy Smile(线段树+枚举)
Snowy Smile 题目传送门 解题思路 先把y离散化,然后把点按照x的大小进行排序,我们枚举每一种x作为上边界,然后再枚举其对应的每一种下边界.按照这种顺序插入点,这是一个压维的操作,即在线段树 ...
- 2019杭电暑假多校训练 第六场 Snowy Smile HDU - 6638
很多题解都是简单带过,所以打算自己写一篇,顺便也加深自己理解 前置知识:线段树.线段树维护最大字段和.二维坐标离散化 题解: 1.很容易想到我们需要枚举所有子矩阵来得到一个最大子矩阵,所以我们的任务是 ...
- 2019杭电多校6 hdu6638 Snowy Smile(二维最大矩阵和 线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意:给你一些点的权值,让找一个矩形圈住一部分点,问圈住点的最大权值和 分析:由于是稀疏图,明显要先把x, ...
- [2019杭电多校第六场][hdu6638]Snowy Smile(维护区间最大子段和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意为在一个平面上任意选择一个长方形,使得长方形内点权和最大. 因为长方形可以任意选择,所以上下 ...
- 2019 Multi-University Training Contest 7 - 1006 - Snowy Smile - 线段树
http://acm.hdu.edu.cn/showproblem.php?pid=6638 偷学一波潘哥的二维离散化和线段树维护最大子段和. 思路是枚举上下边界,但是不需要从左到右用最大子段和dp. ...
- 【HDOJ6638】Snowy Smile(线段树)
题意:一个二维平面上有n个点,每个点的坐标是(x[i],y[i]),权值是w[i] 求一个矩形使得其中所有点的权值和最大,输出权值和 n<=2e3,x[i],y[i],w[i]的绝对值<= ...
- 2019 Multi-University Training Contest 6 Snowy Smile (最大字段和变形)
题意: 求一个子矩阵要求其矩阵内的合最大. 题解: 正常的求最大子矩阵的复杂度是O(n^3) 对于这一题说复杂度过不去,注意到这个题总共只有2000个点关键点在与这里优化 最大子矩阵可以压缩矩阵变成最 ...
随机推荐
- web渗透---第二天
协议常识 HTTP协议 百度百科的解释:超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议. 所有的WWW文件都必须遵守这个标准. ...
- python_0基础开始_day03
第三节 一.整形和布尔值的转换 int整型 python3: 全部都是整型 python2: 整型,长整型long 十进制转换二进制 # 将十进制的168转换为二进制 #得出结果 将十进制的168转 ...
- 插入Oracle数据库后返回当前主键id
最近做一个spring版本3.0.4的老项目功能,应用场景要用到插入oracle表后返回主键ID拿来和其他表关联. 用oralce的可以一直用这种处理方式,高兼容低,搜索网上的资料都不能和这个Spri ...
- lvs模式及算法
一.三种模式 (一).Virtual Servervia Network Address Translation(VS/NAT) 通过网路地址转换,调度器重写请求报文的目标地址,根据预设的调度算法,将 ...
- Intent 常用方法总结
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本文主要是总结Intent 常用的方法,并封装成Utils类中 主要涉及以下内容 ...
- java并发编程(九)----(JUC)CyclicBarrier
上一篇我们介绍了CountDownlatch,我们知道CountDownlatch是"在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待",即CountDownL ...
- java并发编程(七)----(JUC)ReadWriteLock
前面我们已经分析过JUC包里面的Lock锁,ReentrantLock锁和semaphore信号量机制.Lock锁实现了比synchronized更灵活的锁机制,Reentrantlock是Lock的 ...
- 全球十大OTA 谁能有一席之地?
全球十大OTA 谁能有一席之地? http://www.traveldaily.cn/article/78381/1 2014-03-05 来源:i黑马 随着旅游行业日新月异的发展,在线旅游网站的出现 ...
- 动态SQL查询
if+where: 用于查询操作,where标签可以智能判断是否添加and.or.where关键词 示例: <select id="findByParam" resultTy ...
- 趣味CSS3效果挑战小汇总
众所周知,在CSS3中产生了诸多优秀的特性,现在就来分享一下我这段时间对于这些特性的效果实践,希望对大家有所启发. 挑战1: 画一个对话框 要画一个对话框,首先来学习做一个三角形.其实非常的简单. & ...