hdu2795(线段树单点更新&区间最值)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795

题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要尽量将其靠上贴,并输出其最上能贴在哪个位置;

思路:可以将每行剩余空间大小存储到一个数组中,那么对于当前 1 * x 的广告,只需找到所有剩余空间大于的 x 的行中位置最小的即可;

不过本题数据量为 2e5,直接暴力因该会 tle.可以用个线段树维护一下区间最大值,然后查询时对线段树二分即可;

代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #define lson l, mid, rt << 1
 4 #define rson mid + 1, r, rt << 1 | 1
 5 using namespace std;
 6
 7 const int MAXN = 2e5 + 10;
 8 int Max[MAXN << 2], h, w, n;
 9
10 int max(int a, int b){
11     return a > b ? a : b;
12 }
13
14 void push_up(int rt){
15     Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);
16 }
17
18 void build(int l, int r, int rt){//建树
19     Max[rt] = w;
20     if(l == r) return;
21     int mid = (l + r) >> 1;
22     build(lson);
23     build(rson);
24 }
25
26 void update(int p, int x, int l, int r, int rt){//单点更新
27     if(l == r){
28         Max[rt] -= x;
29         return;
30     }
31     int mid = (l + r) >> 1;
32     if(p <= mid) update(p, x, lson);
33     else update(p, x, rson);
34     push_up(rt);
35 }
36
37 int query(int x, int l, int r, int rt){//查询
38      if(l == r) return l;
39      int mid = (l + r) >> 1;
40      int ans = 0;
41      if(Max[rt << 1] >= x) ans = query(x, lson);
42      else ans = query(x, rson);
43      return ans;
44 }
45
46 int main(void){
47     while(~scanf("%d%d%d", &h, &w, &n)){
48         if(h > n) h = n;
49         build(1, h, 1);
50         while(n--){
51             int x, cnt;
52             scanf("%d", &x);
53             if(Max[1] < x){
54                 printf("-1\n");
55                 continue;
56             }else printf("%d\n", cnt = query(x, 1, h, 1));
57             update(cnt, x, 1, h, 1);
58         }
59     }
60     return 0;
61 }

其实这个代码中的更新的路径和查询的路径是一样的,可以优化一下,将更新写进查询里面去;

优化代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #define lson l, mid, rt << 1
 4 #define rson mid + 1, r, rt << 1 | 1
 5 using namespace std;
 6
 7 const int MAXN = 2e5 + 10;
 8 int Max[MAXN << 2], h, w, n;
 9
10 int push_up(int rt){
11     Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);
12 }
13
14 void build(int l, int r, int rt){
15     Max[rt] = w;
16     if(l == r) return;
17     int mid = (l + r) >> 1;
18     build(lson);
19     build(rson);
20 }
21
22 int query(int x, int l, int r, int rt){
23     if(l == r){
24         Max[rt] -= x;
25         return l;
26     }
27     int mid = (l + r) >> 1;
28     int cnt = (Max[rt << 1] >= x) ? query(x, lson) : query(x, rson);
29     push_up(rt);
30     return cnt;
31 }
32
33 int main(void){
34     while(~scanf("%d%d%d", &h, &w, &n)){
35         if(h > n) h = n;
36         build(1, h, 1);
37         while(n--){
38             int x;
39             scanf("%d", &x);
40             if(Max[1] < x) printf("-1\n");
41             else printf("%d\n", query(x, 1, h, 1));
42         }
43     }
44     return 0;
45 }

时间: 2024-08-22 09:12:13

hdu2795(线段树单点更新&区间最值)的相关文章

【HDU】1754 I hate it ——线段树 单点更新 区间最值

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 37448    Accepted Submission(s): 14816 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要

hdu 3308 线段树单点更新 区间合并

http://acm.hdu.edu.cn/showproblem.php?pid=3308 学到两点: 1.以区间端点为开始/结束的最长......似乎在Dp也常用这种思想 2.分类的时候,明确标准逐层分类,思维格式: 条件一成立: { 条件二成立: { } else { } } else { 条件二成立: { } else { } } 上面的这种方式很清晰,如果直接想到那种情况iif(条件一 &条件二)就写,很容易出错而且把自己搞乱,或者情况不全,,,我就因为这WA了几次 3.WA了之后 ,

hdu - 4973 - A simple simulation problem.(线段树单点更新 + 区间更新)

题意:初始序列 1, 2, ..., n,m次操作(1 <= n,m<= 50000),每次操作可为: D l r,将区间[l, r]中的所有数复制一次: Q l r,输出区间[l, r]中同一数字个数的最大值. (0 <= r – l <= 10^8, 1 <= l, r <= 序列元素个数) 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4973 -->>因为区间内数字是依次递增的,所以可以以数字为叶建线段

HDU 3308 LCIS (线段树&#183;单点更新&#183;区间合并)

题意  给你一个数组  有更新值和查询两种操作  对于每次查询  输出对应区间的最长连续递增子序列的长度 基础的线段树区间合并  线段树维护三个值  对应区间的LCIS长度(lcis)  对应区间以左端点为起点的LCIS长度(lle)  对应区间以右端点为终点的LCIS长度(lri)  然后用val存储数组对应位置的值  当val[mid + 1] > val[mid] 的时候就要进行区间合并操作了 #include <cstdio> #include <algorithm>

POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally sp

HDU 1540 Tunnel Warfare(线段树单点更新+区间合并)

Problem Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every vill

HDU 3308 线段树单点更新+区间查找最长连续子序列

LCIS                                                              Time Limit: 6000/2000 MS (Java/Others)                                                                 Memory Limit: 65536/32768 K (Java/Others)                                       

hdu1394(枚举/树状数组/线段树单点更新&amp;区间求和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 或 -1, 交换两个不相邻数 a, b, 逆序数 += 两者间大于 a 的个数 - 两者间小于 a 的个数: 所以只要求出初始时的逆序对数,就可以推出其余情况时的逆序对数.对于求初始逆序对数,这里 n 只有 5e3,可以直接暴力 / 树状数组 / 线段树 / 归并排序: 代码: 1.直接暴力 1

HDU 2795 Billboard (线段树 单点更新 区间求最大值)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一块h*w 的广告版,有n块1*w[i]的广告,就着放广告尽量在顶上,尽量先放左边的原则,问在第几行能把广告放下,如果放不下,就打印-1: 思路:我们可以根据每一行建树,每一个子叶表示每一行的容量,而节点存放子节点的最大值,然后从最顶到底,快速查找能存放下广告的一行. 总之还是简单的线段树问题,难点在于抽象模型. #include <iostream> #include <cs