HDU 6638 - Snowy Smile

题意

给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大。

思路

  1. 先离散化纵坐标\(y\)的值
  2. 对\(n\)个点根据横坐标\(s\)进行排序
  3. 枚举横坐标,按顺序把点扔到线段树里,以离散化后\(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 线段树区间合并+暴力枚举的更多相关文章

  1. hdu 3397 Sequence operation (线段树 区间合并 多重标记)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意: 给你一串01串,有5种操作 0. 区间全部变为0 1.区间全部变为1 2.区间异或 3.询问 ...

  2. HDU 5316——Magician——————【线段树区间合并区间最值】

    Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  3. HDU 1540 Tunnel Warfare 线段树区间合并

    Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...

  4. (简单) HDU 3308 LCIS,线段树+区间合并。

    Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. ( ...

  5. hdu 4453 约会安排(线段树区间合并)

    约会安排 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

  6. hdu 3308 LCIS(线段树区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3308 LCIS Time Limit: 6000/2000 MS (Java/Others)     ...

  7. hdu 1540 Tunnel Warfare 线段树 区间合并

    题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2. ...

  8. HDU 4351 Digital root 线段树区间合并

    依然不是十分理解……待考虑…… #include <cstdio> #include <cstring> #include <cstdlib> #include & ...

  9. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

随机推荐

  1. ruby的next if boolean

    next相当于continue

  2. delphi之猥琐的webserver实现

    http://www.birdol.com/cainiaobiancheng/238.html delphi之猥琐的webserver实现 菜鸟编程  十五楼的鸟儿  7年前 (2009-01-01) ...

  3. SqlServer 高级查询

    高级查询在数据库中用得是最频繁的,也是应用最广泛的. Ø 基本常用查询 --select select * from student;   --all 查询所有 select all sex from ...

  4. HTML5--浏览器全屏操作、退出全屏、是否全屏

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. _groovy

    _groovy与beanshell类似,只是它执行的是apache groovy脚本,并返回结果. 如果定义了属性 “groovy.utilities”,属性将会被脚本引擎加载,这样就可以定义一些通用 ...

  6. python学习第十五天集合的创建和基本操作方法

    集合是python独有的数据列表,集合可以做数据分析,集合是一个无序的,唯一的的数据类型,可以确定列表的唯一性,说一下集合的创建和基本常见操作方法 1,集合的创建 s={1,2,4} 也可以用set( ...

  7. 部署Lighttpd到252板子

    1.先到lighttpd官网下载对应版本的软件包: 如: lighttpd-1.4.30.tar.gz 2. 将压缩包解压到任意目录得到文件夹 lighttpd-1.4.30 3. 执行配置命令:  ...

  8. node的fs模块使用————node

    node的fs模块使用----node fs模块是调用文件的模块. var fs=require('fs'); //引用模块. //查看文件信息 fs.stat('index.txt',functio ...

  9. JS-01 书写规范

    此部分内容整理自私教指导和自我体会:(持续更新...) 1.运算符左右两边留空格 (webstorm快捷键ctrl+alt+l): 2.判断值是否相等尽量用“===” 严格等于 : 3.编程中,可有可 ...

  10. 4.ireport基本使用

    转自:https://wenku.baidu.com/view/104156f9770bf78a65295462.html 第一部分,下载与安装 Ireport官网:http:// jasperfor ...