hdu 2795 线段树(纵向)

注意h的范围和n的范围,纵向建立线段树

题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子
思路:每次找到最大值的位子,然后减去L
线段树功能:query:区间求最大值的位子(直接把update的操作在query里做了)
3 5 5
2
4
3
3
3

1
2
1
3
-1

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 using namespace std;
10 const int maxn=222225;
11 int sum[maxn<<2];
12 int n,m,t,h,w;
13 void pushup(int rt)
14 {
15     sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);
16 }
17 void build(int l,int r,int rt)
18 {
19     sum[rt]=w;
20     if(l==r)    return;
21     int m=(l+r)>>1;
22     build(lson);
23     build(rson);
24 }
25 int query(int add,int l,int r,int rt)
26 {
27     if(l==r)
28     {
29         sum[rt]-=add;
30         return l;
31     }
32     int m=(l+r)>>1;
33     int ret;
34     if(sum[rt<<1]>=add)
35     {
36         ret=query(add,lson);
37     }
38     else ret=query(add,rson);
39     pushup(rt);
40     return ret;
41 }
42 int main()
43 {
44     int i,j,k;
45     //freopen("1.in","r",stdin);
46     while(scanf("%d%d%d",&h,&w,&n)!=EOF)
47     {
48         if(h>n) h=n;
49         build(1,h,1);
50         for(i=1;i<=n;i++)
51         {
52             scanf("%d",&k);
53             if(sum[1]<k)    printf("-1\n");
54             else printf("%d\n",query(k,1,h,1));
55         }
56     }
57     return 0;
58 }
时间: 2024-08-27 06:22:50

hdu 2795 线段树(纵向)的相关文章

hdu 2795 线段树--点更新

http://acm.hdu.edu.cn/showproblem.php?pid=2795 多校的第一场和第三场都出现了线段树,比赛期间没做,,这两天先做几道热身下,然后31号之前把那两道多校的线段树都搞了,这是一道热身题 关键是建模: 首先一定看清楚题目构造的场景,看有什么特点--------会发现,如果尽量往左上放置的话,那么由于 the i-th announcement is a rectangle of size 1 * wi.,完全可以对h建立线段树,表示一行,结点里的l,r就表示

hdu 2795 线段树(二维问题一维化)

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10961    Accepted Submission(s): 4863 Problem Description At the entrance to the university, there is a huge rectangular billboard of s

HDU 2795 线段树(转变思维方能改变世界)

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9895    Accepted Submission(s): 4413 Problem Description At the entrance to the university, there is a huge rectangular billboard of si

HDU 2795 线段树单点更新

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 23498    Accepted Submission(s): 9687 Problem Description At the entrance to the university, there is a huge rectangular billboard of s

HDU 2795 (线段树 单点更新) Billboard

h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子. 每次找能放纸条而且是最上面的位置,询问完以后可以同时更新,所以可以把update和query写在同一个函数里. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 200000 + 10; 7 8 int _max[maxn <&l

HDU 4893 线段树裸题

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2512    Accepted Submission(s): 751 Problem Description Recently, Doge got a funny birthday present from his new friend, Pro

HDU 4902 线段树(区间更新)

Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 353    Accepted Submission(s): 169 Problem Description There is an old country and the king fell in love with a devil. The devil alw

hdu 4893 线段树 --- 也是两个变 类似双标记

http://acm.hdu.edu.cn/showproblem.php?pid=4893 开始的时候,我按双标记,WA了一下午,搞不定,我是用的两个标记add--表示当前结点中有值发生变化,flag,斐波那契的懒惰标记,但是估计是我自己处理的有问题,一直不对 参考了别人的代码,写法还是很不错的,Add变量维护的是,完全变成Fibonacci的时候的和,---回头我再重新写一遍 #include <cstdio> #include <cstring> #include <a

HDU 4902 线段树||暴力

给定一个序列,两种操作 1:把一段变成x. 2:把一段每个数字,如果他大于x,就变成他和x的gcd,求变换完后,最后的序列. 线段树解法:用lazy标记下即可,优化方法还是很巧妙的, Accepted 4902 515MS 3308K 1941 B C++ #include "stdio.h" #include "string.h" struct node { int l,r,x;// 在叶子节点代表值,树节点代表成端更新的lazy操作. }data[400010]