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是区间内连续 ...
随机推荐
- CSS-父元素宽度自适应子元素宽度之和
最近碰见这样一个需求,要让图片横向排列设置 x 方向的滚动条滚动查看,原本当直接创建一个 IFC(inline,float 什么的)就解决了,搞了半天发现搞不定(IFC 也是不能父元素宽度自适应子元素 ...
- 像计算机科学家一样思考python-第2章 变量、表达式和语句
感想: 1.程序出现语义错误时,画状态图是一个很好的调试办法.打印出关键变量在不同代码处理后值的变化,就能发现问题的蛛丝马迹. 2.每当学习新语言特性时,都应当在交互模式中进行尝试,并故意犯下错误,看 ...
- WEB开发:Java与Php对比
比较PHP和JSP这两个Web开发技术,在目前的情况是其实是比较PHP和Java的Web开发.以下是我就几个主要方面进行比较: 一. 语言比较 PHP是解释执行的服务器脚本语言,首先php有简单容易上 ...
- Jenkins使用六:搭建流水线任务
流水线可以把多个任务串起来,比如发布版本的一系列流程 配置流水线任务 构建语法为Groovy,执行3次test(job名) node { stage("test") { echo ...
- delphi idhttpserver 服务器
[冒泡]lazarus(964489899) 10:01:27 哥 能复制成 字符串吗? [冒泡]lazarus(964489899) 10:01:44 我想快速输入一下 [传说]CHINY( ...
- Discuz升级 Database Error : pre_common_syscache ADD PRIMARY KEY (cname)【解决办法】
错误码: 1068Multiple primary key defined Execution Time : 00:00:00:000Transfer Time : 00:00:00:000Total ...
- HTML5移动应用——小心代码注入风险
近日在加州举行的移动安全技术大会上,Syracuse大学的研究者的研究报告显示HTML5移动应用可能会给企业带来新的安全风险.开发者的错误可能导致HTML5应用自动执行攻击者通过Wifi蓝牙或短信发送 ...
- 应用安全-XXE(XML外部实体注入)攻防整理
libxml2..1及以后,默认不解析外部实体.测试的时候window下使用php5.(libxml Version ), php5.(libxml Version ).Linux中需要将libxml ...
- java_第一年_JavaWeb(1)
注:此系列javaweb的知识是我在一位“孤傲苍狼”的园友学习后记下来的笔记,并非原创^_^ Web开发的基本概念 web应用程序——提供浏览器访问的程序,也成为web应用,包含静态或动态资源:所谓的 ...
- [BZOJ 2653] middle(可持久化线段树+二分答案)
[BZOJ 2653] middle(可持久化线段树+二分答案) 题面 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序 ...