hdu2795(线段树单点更新&区间最值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795
题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要尽量将其靠上贴,并输出其最上能贴在哪个位置;
思路:可以将每行剩余空间大小存储到一个数组中,那么对于当前 1 * x 的广告,只需找到所有剩余空间大于的 x 的行中位置最小的即可;
不过本题数据量为 2e5,直接暴力因该会 tle.可以用个线段树维护一下区间最大值,然后查询时对线段树二分即可;
代码:
#include <iostream>
#include <stdio.h>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; const int MAXN = 2e5 + ;
int Max[MAXN << ], h, w, n; int max(int a, int b){
return a > b ? a : b;
} void push_up(int rt){
Max[rt] = max(Max[rt << ], Max[rt << | ]);
} void build(int l, int r, int rt){//建树
Max[rt] = w;
if(l == r) return;
int mid = (l + r) >> ;
build(lson);
build(rson);
} void update(int p, int x, int l, int r, int rt){//单点更新
if(l == r){
Max[rt] -= x;
return;
}
int mid = (l + r) >> ;
if(p <= mid) update(p, x, lson);
else update(p, x, rson);
push_up(rt);
} int query(int x, int l, int r, int rt){//查询
if(l == r) return l;
int mid = (l + r) >> ;
int ans = ;
if(Max[rt << ] >= x) ans = query(x, lson);
else ans = query(x, rson);
return ans;
} int main(void){
while(~scanf("%d%d%d", &h, &w, &n)){
if(h > n) h = n;
build(, h, );
while(n--){
int x, cnt;
scanf("%d", &x);
if(Max[] < x){
printf("-1\n");
continue;
}else printf("%d\n", cnt = query(x, , h, ));
update(cnt, x, , h, );
}
}
return ;
}
其实这个代码中的更新的路径和查询的路径是一样的,可以优化一下,将更新写进查询里面去;
优化代码:
#include <iostream>
#include <stdio.h>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; const int MAXN = 2e5 + ;
int Max[MAXN << ], h, w, n; int push_up(int rt){
Max[rt] = max(Max[rt << ], Max[rt << | ]);
} void build(int l, int r, int rt){
Max[rt] = w;
if(l == r) return;
int mid = (l + r) >> ;
build(lson);
build(rson);
} int query(int x, int l, int r, int rt){
if(l == r){
Max[rt] -= x;
return l;
}
int mid = (l + r) >> ;
int cnt = (Max[rt << ] >= x) ? query(x, lson) : query(x, rson);
push_up(rt);
return cnt;
} int main(void){
while(~scanf("%d%d%d", &h, &w, &n)){
if(h > n) h = n;
build(, h, );
while(n--){
int x;
scanf("%d", &x);
if(Max[] < x) printf("-1\n");
else printf("%d\n", query(x, , h, ));
}
}
return ;
}
hdu2795(线段树单点更新&区间最值)的更多相关文章
- 【HDU】1754 I hate it ——线段树 单点更新 区间最值
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 1754 I Hate It 线段树 单点更新 区间最值
线段树功能:update:单点更新 query:区间最值 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define r ...
- HDU 1754 I Hate It(线段树单点更新区间最值查询)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hihoCoder #1586 : Minimum-结构体版线段树(单点更新+区间最值求区间两数最小乘积) (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1586 : Minimum Time Limit:1000ms Case Time Limit:1000ms Memory Limit:256MB Description You are give ...
- HDU 1754 I Hate It(线段树单点替换+区间最值)
I Hate It [题目链接]I Hate It [题目类型]线段树单点替换+区间最值 &题意: 本题目包含多组测试,请处理到文件结束. 在每个测试的第一行,有两个正整数 N 和 M ( 0 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- 【linux】新添加一块硬盘制作LVM卷并进行分区挂载
linux服务器新添加一块硬盘,可以直接将盘格式化挂载就能用,比如挂载在/usr/local目录,但是这样有一个弊端,就是如果这一块磁盘满了,后续想要扩容的话,不能继续挂载这个/usr/local挂载 ...
- Java for LeetCode 103 Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- PAT 甲级 1116. Come on! Let's C (20) 【循环判断】
题目链接 https://www.patest.cn/contests/pat-a-practise/1116 思路 注意一个细节 如果没有本来 ID 的 后来又查了这个ID 不是输出 checked ...
- Nginx之解压编译安装-yellowcong
安装前准备 对于nginx编译安装需要先安装编译 的工具,然后再安装nginx依赖 yum -y install gcc gcc-c++ autoconf automake make yum -y i ...
- Contiki Timer & Stimer 模块
一.Timer API struct timer { clock_time_t start; clock_time_t interval; }; CCIF void timer_set(struct ...
- matlab给图片插入说明文字
text(cluster().center(),cluster().center(),num2str(),'color','k') 格式是text(x,y,'说明文字') x,y代表位置
- 清除float浮动三种方式
Float的作用? w3c的官方解释: Content flows down the right side of a left-floated box and down the left side o ...
- aoj 0033 玉
図のように二股に分かれている容器があります.1 から 10 までの番号が付けられた10 個の玉を容器の開口部 A から落とし.左の筒 B か右の筒 C に玉を入れます.板 D は支点 E を中心に左右 ...
- Python 2.7数据类型操作_20161010
为兼容python3.x版本 print 后都加了括号 python 数据类型 参考廖雪峰大神python2.7教程 http://www.liaoxuefeng.com/wiki/001374738 ...
- 【Lintcode】120.Word Ladder
题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...