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是区间内连续 ...
随机推荐
- 127、TensorFlow 计算图执行(二)
import tensorflow as tf # Define a placeholder that expects a vector of three floating-point values ...
- 测开之路六十二:接口测试平台之公共的js、html、平台入口
common.js //定义后台的host和端口var host = 'http://192.168.xxx.1:8000'; //'http://127.0.0.1:8000'; //用于发送htt ...
- EF框架之——Code First以及踩过的这些“坑”
传送门 Code First使用步骤 Code First报错和解决办法 以前在上海做了一段时间的Asp.net,基本用的都是.net自带的EF框架连接数据库,不过都是用的Model First,最近 ...
- redis 集群新增节点,slots槽分配,删除节点, [ERR] Calling MIGRATE ERR Syntax error, try CLIENT (LIST | KILL | GET...
redis reshard 重新分槽(slots) https://github.com/antirez/redis/issues/5029 redis 官方已确认该bug redis 集群重新(re ...
- 题解1235. 洪水 (Standard IO)
Description 一天, 一个画家在森林里写生,突然爆发了山洪,他需要尽快返回住所中,那里是安全的.森林的地图由R行C列组成,空白区域用点“.”表示,洪水的区域用“*”表示,而岩石用“X”表示, ...
- _groovy
_groovy与beanshell类似,只是它执行的是apache groovy脚本,并返回结果. 如果定义了属性 “groovy.utilities”,属性将会被脚本引擎加载,这样就可以定义一些通用 ...
- HTML5基本标签<搬运>
HTML语言基本标签: 创建一个HTML文档<html></html> 设置文档标题以及其他不在WEB网页上显示的信息<head></head> 设置文 ...
- windows8.1安装python
python3.8安装后缺少runtime.dll文件,试验了各种方法都不可行,最后安装了Anaconda3,这是一个python配置环境,但是好像Anaconda3只能兼容3.7,python3.8 ...
- python------生产者消费者模型 和 管道
一.为什么要使用生产者和消费者? 在线程世界里,生产者就是生产数据的线程,消费者就是消费数据的线程,在多线程开发当中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才 ...
- 怎么区分PV、IV、UV以及网站统计名词解释(pv、曝光、点击)
PV(Page View)访问量,即页面访问量,每打开一次页面PV计数+1,刷新页面也是. IV(Internet Protocol)访问量指独立IP访问数,计算是以一个独立的IP在一个计算时段内访问 ...