线段树-HDU2795Billboard

来源:互联网 发布:php字符串替换 第一个 编辑:程序博客网 时间:2024/05/16 18:25

题目大意:给个h*w的告示板 每次贴告示(1*w)都要尽量往高 和 左边贴 求每个告示贴上去的高度

这个代码我真不好意思贴···  扒的 勿怪···  谁叫我是靠模板生存的渣呢···

原帖地址

http://www.notonlysuccess.com/index.php/segment-tree-complete/

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include <algorithm>#define ll long long#define eps 1e-8#define ms(x,y) (memset(x,y,sizeof(x)))#define fr(i,x,y) for(int i=x;i<=y;i++)using namespace std;/*线段树 点修改 区间最大值*/#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=200000+10;int tree[maxn<<2];int h,w,n;void pushup(int rt){    tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);}void build(int l,int r,int rt){    tree[rt]=w;    if(l==r)return ;    int m=(l+r)>>1;    build(lson);    build(rson);    pushup(rt);}int query(int x,int l,int r,int rt){    if(l==r)    {        tree[rt]-=x;        return l;    }    int m=(l+r)>>1;    int ret=(tree[rt<<1]>=x)?query(x,lson):query(x,rson);    pushup(rt);    return ret;}/*end-线段树 点修改 区间最大值*/void init(){    h=min(h,n);    build(1,h,1);}void doit(){    fr(i,1,n)    {        int x;        scanf("%d",&x);        if(tree[1]<x)printf("-1\n");        else printf("%d\n",query(x,1,h,1));    }}int main(){    while(~scanf("%d%d%d",&h,&w,&n))    {        init();        doit();    }    return 0;}


0 0