题目链接

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的更多相关文章

  1. HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举

    HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...

  2. 最大矩阵覆盖权值--(静态连续最大子段 (线段树) )-HDU(6638)Snowy Smile

    这题是杭电多校2019第六场的题目 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意:给你平面上n个点,每个点都有权值(有负权),让你计算一 ...

  3. 2019杭电多校第六场hdu6638 Snowy Smile(线段树+枚举)

    Snowy Smile 题目传送门 解题思路 先把y离散化,然后把点按照x的大小进行排序,我们枚举每一种x作为上边界,然后再枚举其对应的每一种下边界.按照这种顺序插入点,这是一个压维的操作,即在线段树 ...

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

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

  5. 2019杭电多校6 hdu6638 Snowy Smile(二维最大矩阵和 线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意:给你一些点的权值,让找一个矩形圈住一部分点,问圈住点的最大权值和 分析:由于是稀疏图,明显要先把x, ...

  6. [2019杭电多校第六场][hdu6638]Snowy Smile(维护区间最大子段和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意为在一个平面上任意选择一个长方形,使得长方形内点权和最大. 因为长方形可以任意选择,所以上下 ...

  7. 2019 Multi-University Training Contest 7 - 1006 - Snowy Smile - 线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=6638 偷学一波潘哥的二维离散化和线段树维护最大子段和. 思路是枚举上下边界,但是不需要从左到右用最大子段和dp. ...

  8. 【HDOJ6638】Snowy Smile(线段树)

    题意:一个二维平面上有n个点,每个点的坐标是(x[i],y[i]),权值是w[i] 求一个矩形使得其中所有点的权值和最大,输出权值和 n<=2e3,x[i],y[i],w[i]的绝对值<= ...

  9. 2019 Multi-University Training Contest 6 Snowy Smile (最大字段和变形)

    题意: 求一个子矩阵要求其矩阵内的合最大. 题解: 正常的求最大子矩阵的复杂度是O(n^3) 对于这一题说复杂度过不去,注意到这个题总共只有2000个点关键点在与这里优化 最大子矩阵可以压缩矩阵变成最 ...

随机推荐

  1. python Django编写接口并用Jmeter测试

    一.环境准备 python3.6.7 Pycharm 二.创建项目 我这里是在Django项目中新建了个APP,目录结构如下图所示: 那么怎么在已有的Django项目中新建APP并进行配置呢: 2.1 ...

  2. 【Android】java.lang.SecurityException: getDeviceId: Neither user 10065 nor current process has android.permission.READ_PHONE_STATE

    RT, 异常信息如下: java.lang.SecurityException: getDeviceId: Neither user 10065 nor current process has and ...

  3. 最火的分布式 HTAP 数据库 TiDB - 入门实践教程

    偶然在某篇博客看到了 TiDB,一个融合 OLTP 和 OLAP 的分布式开源数据库, GitHub 上 Star 很多,然后 watch 了,发现 commit 和 pull request 一直都 ...

  4. WTM 构建DotNetCore开源生态,坐而论道不如起而行之

    作为一个8岁开始学习编程,至今40岁的老程序员,这辈子使用过无数种语言,从basic开始,到pascal, C, C++,到后来的 java, c#,perl,php,再到现在流行的python. 小 ...

  5. 中国地区表SQL语句

    /* Navicat MySQL Data Transfer Source Server : 本机 Source Server Version : 50553 Source Host : localh ...

  6. 如何在Vue项目中使用vw实现移动端适配

    有关于移动端的适配布局一直以来都是众说纷纭,对应的解决方案也是有很多种.在< 使用Flexible实现手淘H5页面的终端适配>提出了Flexible的布局方案,随着 viewport 单位 ...

  7. Docker系列之烹饪披萨(二)

    前言 上一篇我们讲解了虚拟机和容器的区别,本节我们来讲讲Docker中关于Dockerfile.镜像.容器等基本概念.Docker是一个在容器内开发.部署.运行应用程序的平台,Docker本质上是容器 ...

  8. 教老婆学Linux运维(一)初识Linux

    零.前言 之一 为什么写这个系列?为什么是Linux? 老婆自从怀孕以后,辞职在家待了好几年了,现在时常感觉与社会脱节.所以想找个工作. 做了多年程序员,有点人脉也都基本是在IT圈子里,只能帮忙找找I ...

  9. Leetcode solution 227: Basic Calculator II

    Problem Statement Implement a basic calculator to evaluate a simple expression string. The expressio ...

  10. 【0728 | 预习】第三篇 Python基础

    第三篇 Python基础预习 Part 1 变量 一.什么是变量? 二.为什么要有变量? 三.定义变量 四.变量的组成 五.变量名的命名规范 六.变量名的两种风格 Part 2 常量 Part 3 P ...