Snowy Smile HDU - 6638(扫描线,线段树区间合并)

2024-04-15 23:38

本文主要是介绍Snowy Smile HDU - 6638(扫描线,线段树区间合并),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 n n的范围只有2000,所以可以枚举矩阵的下边界( y y y轴)。然后每次将矩阵上边界移动一个单位(离散化后的单位)。线段树维护每个点在 x x x轴上的位置,维护一下最大值,就得到了当前的最大权值矩阵。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>using namespace std;typedef long long ll;
const int maxn = 4e3 + 7;
struct Node {int x,y,w;
}a[maxn];
vector<pair<int,int> >vec[maxn];struct Tree {int l,r;ll sum;ll mx,lmx,rmx;
}t[maxn << 2];void pushup(int i) {t[i].mx = max(max(t[i * 2].mx,t[i * 2 + 1].mx),t[i * 2].rmx + t[i * 2 + 1].lmx);t[i].sum = t[i * 2].sum + t[i * 2 + 1].sum;t[i].lmx = max(t[i * 2].lmx,t[i * 2].sum + t[i * 2 + 1].lmx);t[i].rmx = max(t[i * 2 + 1].rmx,t[i * 2 + 1].sum + t[i * 2].rmx);
}void build(int i,int l,int r) {t[i].l = l;t[i].r = r;if(l == r) {t[i].sum = t[i].mx = t[i].lmx = t[i].rmx = 0;return;}int m = (l + r) >> 1;build(i * 2,l,m);build(i * 2 + 1,m + 1,r);pushup(i);
}void update(int i,int x,int v) {if(t[i].l == t[i].r) {t[i].sum += v;t[i].lmx += v;t[i].rmx += v;t[i].mx += v;return;}int m = (t[i].l + t[i].r) >> 1;if(x <= m) update(i * 2,x,v);if(x > m)  update(i * 2 + 1,x,v);pushup(i);
}int main() {int T;scanf("%d",&T);while(T--) {int n;scanf("%d",&n);vector<int>vecx,vecy;for(int i = 1;i <= n;i++) {scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);vecx.push_back(a[i].x);vecy.push_back(a[i].y);vec[i].clear();}sort(vecx.begin(),vecx.end());sort(vecy.begin(),vecy.end());vecx.erase(unique(vecx.begin(),vecx.end()),vecx.end());vecy.erase(unique(vecy.begin(),vecy.end()),vecy.end());for(int i = 1;i <= n;i++) {a[i].x = lower_bound(vecx.begin(),vecx.end(),a[i].x) - vecx.begin() + 1;a[i].y = lower_bound(vecy.begin(),vecy.end(),a[i].y) - vecy.begin() + 1;vec[a[i].y].push_back({a[i].x,a[i].w});}ll ans = 0;for(int i = 1;i <= vecy.size();i++) { //矩阵下界build(1,1,vecy.size());for(int j = i;j <= vecy.size();j++) { //矩阵上界for(int k = 0;k < vec[j].size();k++) {int pos = vec[j][k].first,val = vec[j][k].second;update(1,pos,val);}ans = max(ans,t[1].mx);}}printf("%lld\n",ans);}return 0;
}

这篇关于Snowy Smile HDU - 6638(扫描线,线段树区间合并)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907245

相关文章

Python中合并列表(list)的六种方法小结

《Python中合并列表(list)的六种方法小结》本文主要介绍了Python中合并列表(list)的六种方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录一、直接用 + 合并列表二、用 extend() js方法三、用 zip() 函数交叉合并四、用

利用Python实现Excel文件智能合并工具

《利用Python实现Excel文件智能合并工具》有时候,我们需要将多个Excel文件按照特定顺序合并成一个文件,这样可以更方便地进行后续的数据处理和分析,下面我们看看如何使用Python实现Exce... 目录运行结果为什么需要这个工具技术实现工具的核心功能代码解析使用示例工具优化与扩展有时候,我们需要将

Python实现获取带合并单元格的表格数据

《Python实现获取带合并单元格的表格数据》由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,所以本文我们就来聊聊如何使用Python实现获取带合并单元格的表格数据吧... 由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,现将将封装成类,并通过调用list_exc

Python实现合并与拆分多个PDF文档中的指定页

《Python实现合并与拆分多个PDF文档中的指定页》这篇文章主要为大家详细介绍了如何使用Python实现将多个PDF文档中的指定页合并生成新的PDF以及拆分PDF,感兴趣的小伙伴可以参考一下... 安装所需要的库pip install PyPDF2 -i https://pypi.tuna.tsingh

使用Apache POI在Java中实现Excel单元格的合并

《使用ApachePOI在Java中实现Excel单元格的合并》在日常工作中,Excel是一个不可或缺的工具,尤其是在处理大量数据时,本文将介绍如何使用ApachePOI库在Java中实现Excel... 目录工具类介绍工具类代码调用示例依赖配置总结在日常工作中,Excel 是一个不可或缺的工http://

使用Python创建一个能够筛选文件的PDF合并工具

《使用Python创建一个能够筛选文件的PDF合并工具》这篇文章主要为大家详细介绍了如何使用Python创建一个能够筛选文件的PDF合并工具,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录背景主要功能全部代码代码解析1. 初始化 wx.Frame 窗口2. 创建工具栏3. 创建布局和界面控件4

Python自动化办公之合并多个Excel

《Python自动化办公之合并多个Excel》在日常的办公自动化工作中,尤其是处理大量数据时,合并多个Excel表格是一个常见且繁琐的任务,下面小编就来为大家介绍一下如何使用Python轻松实现合... 目录为什么选择 python 自动化目标使用 Python 合并多个 Excel 文件安装所需库示例代码

使用Python合并 Excel单元格指定行列或单元格范围

《使用Python合并Excel单元格指定行列或单元格范围》合并Excel单元格是Excel数据处理和表格设计中的一项常用操作,本文将介绍如何通过Python合并Excel中的指定行列或单... 目录python Excel库安装Python合并Excel 中的指定行Python合并Excel 中的指定列P

基于C#实现PDF文件合并工具

《基于C#实现PDF文件合并工具》这篇文章主要为大家详细介绍了如何基于C#实现一个简单的PDF文件合并工具,文中的示例代码简洁易懂,有需要的小伙伴可以跟随小编一起学习一下... 界面主要用于发票PDF文件的合并。经常出差要报销的很有用。代码using System;using System.Col

Python视频剪辑合并操作的实现示例

《Python视频剪辑合并操作的实现示例》很多人在创作视频时都需要进行剪辑,本文主要介绍了Python视频剪辑合并操作的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录介绍安装FFmpegWindowsMACOS安装MoviePy剪切视频合并视频转换视频结论介绍