码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2795 线段树(纵向)

时间:2015-02-09 12:23:42      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

注意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 }

 

hdu 2795 线段树(纵向)

标签:

原文地址:http://www.cnblogs.com/cnblogs321114287/p/4281005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!