HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile
题意
给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大。
思路
- 先离散化纵坐标\(y\)的值
- 对\(n\)个点根据横坐标\(s\)进行排序
- 枚举横坐标,按顺序把点扔到线段树里,以离散化后\(y\)的\(id\)为下标\(pos\),存到线段树里
- 因为线段树可以在\(\log{n}\)的时间内插入数值,在\(O(1)\)的时间内查询当前区间最大子段和(线段树区间合并)
\(node[rt].Max :\)当前区间最大子段和
\(node[rt].lsum :\)当前区间从左端点开始最大连续子段和
\(node[rt].rsum:\)当前区间从右端点开始最大连续子段和
\(node[rt].sum:\)当前区间和
node[rt].Max = max(node[rt<<1].Max, node[rt<<1|1].Max);
node[rt].Max = max(node[rt].Max, node[rt<<1].rsum + node[rt<<1|1].lsum);
node[rt].lsum = max(node[rt<<1].lsum, node[rt<<1].sum + node[rt<<1|1].lsum);
node[rt].rsum = max(node[rt<<1|1].rsum, node[rt<<1|1].sum + node[rt<<1].rsum);
node[rt].sum = node[rt<<1].sum + node[rt<<1|1].sum;
然后就能愉快的解决这道题了,时间复杂度在\(O(n^2\log{n})\),枚举\(n^2\), 线段树插入\(\log{n}\)(今天下午做题一小时,自闭一下午也是没谁了,一直想不到做法,还是太菜了)
AC代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#define mes(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
const int inf = 1e9+7;
const int maxn = 2e3+10;
struct Node{
ll Max, lsum, rsum, sum;
}node[maxn<<2];
struct A{
int x, y;
ll w;
bool operator < (A const& b)const{
return x > b.x;
}
}a[maxn];
vector<int> y;
void build(int l, int r, int rt){
node[rt].Max = node[rt].lsum = node[rt].rsum = node[rt].sum = 0;
if(l == r)
return;
int mid = l+r>>1;
build(l, mid, rt<<1);
build(mid+1, r, rt<<1|1);
}
void pushup(int rt){
node[rt].Max = max(node[rt<<1].Max, node[rt<<1|1].Max);
node[rt].Max = max(node[rt].Max, node[rt<<1].rsum + node[rt<<1|1].lsum);
node[rt].lsum = max(node[rt<<1].lsum, node[rt<<1].sum + node[rt<<1|1].lsum);
node[rt].rsum = max(node[rt<<1|1].rsum, node[rt<<1|1].sum + node[rt<<1].rsum);
node[rt].sum = node[rt<<1].sum + node[rt<<1|1].sum;
}
void update(int pos, ll c, int l, int r, int rt){
if(l == r){
node[rt].sum += c;
if(node[rt].sum > 0){
node[rt].lsum = node[rt].Max = node[rt].rsum = node[rt].sum;
}
else{
node[rt].lsum = node[rt].Max = node[rt].rsum = 0;
}
return;
}
int mid = l+r>>1;
if(pos <= mid)
update(pos, c, l, mid, rt<<1);
else
update(pos, c, mid+1, r, rt<<1|1);
pushup(rt);
}
int main(){
int T, n;
scanf("%d", &T);
while(T--){
y.clear();
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d%d%lld", &a[i].x, &a[i].y, &a[i].w);
y.push_back(a[i].y);
}
sort(y.begin(), y.end());
y.erase(unique(y.begin(), y.end()), y.end());
sort(a+1, a+1+n);
ll ans = 0;
a[0].x = inf;
a[0].y = inf;
for(int i = 1; i <= n; i++){
if(a[i].x != a[i-1].x){ //相等的就跳过
build(1, n, 1);
for(int j = i; j <= n; j++){ //暴力枚举所有的横坐标x的左右区间
int id = lower_bound(y.begin(), y.end(), a[j].y) - y.begin() + 1;
update(id, a[j].w, 1, n, 1);
if(a[j].x == a[j+1].x && j != n) continue; //相等的就跳过
ans = max(ans, node[1].Max);
}
}
}
printf("%lld\n", ans);
}
return 0;
}
HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举的更多相关文章
- hdu 3397 Sequence operation (线段树 区间合并 多重标记)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意: 给你一串01串,有5种操作 0. 区间全部变为0 1.区间全部变为1 2.区间异或 3.询问 ...
- HDU 5316——Magician——————【线段树区间合并区间最值】
Magician Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 1540 Tunnel Warfare 线段树区间合并
Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...
- (简单) HDU 3308 LCIS,线段树+区间合并。
Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. ( ...
- hdu 4453 约会安排(线段树区间合并)
约会安排 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- hdu 3308 LCIS(线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3308 LCIS Time Limit: 6000/2000 MS (Java/Others) ...
- hdu 1540 Tunnel Warfare 线段树 区间合并
题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2. ...
- HDU 4351 Digital root 线段树区间合并
依然不是十分理解……待考虑…… #include <cstdio> #include <cstring> #include <cstdlib> #include & ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
随机推荐
- Ajax对数据的删除与查看
1.数据库找一张表: 颜色表2.主页面主页面的代码用到tbody:TBODY作用是:可以控制表格分行下载,从而提高下载速度.(网页的打开是先表格的的内容全部下载完毕后,才显示出来,分行下载可以先显示部 ...
- GitLab 安装,配置及维护
参考: GitLab 官方文档 docker-gitlab,通过 docker-compose 快速安装 GitLab rake,是 Rails 的工具,类似 ruby 中常用的的 make.通过 R ...
- js面向对象程序设计之构造函数
再上一篇的开头说了创建对象的两种方式,一种是Object构造函数的方式,一种是对象字面量的方法.但这些方式创建多个对象的时候都会产生大量的重复代码.经过技术的进步也演化出来许多的创建对象的模式.本章会 ...
- 【Unity系统知识】之unity文件操作路径
IOS:Application.dataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx ...
- error LNK2019: unresolved external symbol __vsnwprintf
老DX SDK,新VS2019问题,编译老项目GG,依赖库加入 legacy_stdio_definitions.lib 解决
- JavaScript LoopQueue
function Queue() { var items = []; this.enqueue = function(element) { items.push(element) } this.deq ...
- hdu5857 Median(模拟)
Median Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- Solr的学习使用之(二)schema.xml等配置文件的解析
上一篇文章已经讲解了如何部署Solr,部署是部署完了,可是总觉得心里空空的,没底,里面有N多配置文件,比如schema.xml.solrConfig.xml.solr.xml and so on……都 ...
- Mac版Navicat Premium激活教程
工具: Navicat Premium12.0.20 安装包 下载注册机工具包 链接:https://pan.baidu.com/s/1NS8gk780ds1Xn-zHrSIzIw 密码:dvke ...
- 记一次redis读取超时的排查过程(SADD惹的祸)
问题背景 在业务使用redis过程中,出现了read timeout 的异常. 问题排查 直接原因 运维查询redis慢查询日志,发现在异常时间节点,有redis慢查询日志,执行sadd 命令花费了1 ...