HDU 2795.Billboard-完全版线段树(区间求最值的位置、区间染色、贴海报)

HDU2795.Billboard

这个题的意思就是在一块h*w的板子上贴公告,公告的规格为1*w,张贴的时候尽量往上,同一高度尽量靠左,求第n个公告贴的位置所在的行数,如果没有合适的位置贴则输出-1。

因为题意说尽量往上往左,所以线段树存区间的最大值,就是这段区间内的某行是有最大的空位长度,每输入一个长度,就查询子树就可以。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<cstdlib>
 7 #include<queue>
 8 #include<stack>
 9 using namespace std;
10 typedef long long ll;
11 const int maxn=5*1e5+10;
12 const int inf=0x3f3f3f3f;
13 #define lson l,m,rt<<1
14 #define rson m+1,r,rt<<1|1
15 int h,w,n;
16 int MAX[maxn<<2];
17 void PushUp(int rt){
18     MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
19 }
20 void build(int l,int r,int rt){
21     MAX[rt]=w;
22     if(l==r)return ;
23     int m=(l+r)>>1;
24     build(lson);
25     build(rson);
26 }
27 int query(int x,int l,int r,int rt){
28     if(l==r){
29         MAX[rt]-=x;
30         return l;
31     }
32     int m=(l+r)>>1;
33     int ret=(MAX[rt<<1]>=x)?query(x,lson):query(x,rson);
34     PushUp(rt);
35     return ret;
36 }
37 int main(){
38     while(~scanf("%d%d%d",&h,&w,&n)){
39         if(h>n) h=n;
40         build(1,h,1);
41         while(n--){
42             int x;
43             scanf("%d",&x);
44             if(MAX[1]<x)printf("-1
");
45             else printf("%d
",query(x,1,h,1));
46         }
47     }
48     return 0;
49 }
原文地址:https://www.cnblogs.com/ZERO-/p/9729092.html