您的位置:首页 > 其它

HDU-2795-Billboard-线段树单点更新

2015-07-30 10:23 483 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795

好吧,写了这么多单点更新的题目,这样的就很简单了,不过我第一次用这样的风格写代码;向这种简短风格靠齐;

不过题目给的数据感觉还挺坑的,还好我机智的看了Discuss。。。。哈哈,仰天长笑。。。。

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<cmath>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define LL long long
#define inf 1<<30
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int N=250005;
int Max[N<<2];      //  用来存各个节点的最大长度;
int h,w,n;
void PushUp(int rt)
{
    Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    Max[rt]=w;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
}
int query(int x,int l,int r,int rt)
{
    if(l==r){
        Max[rt]-=x;
        return l;
    }
    int mid=(l+r)>>1;
    int ans=0;
    if(Max[rt<<1]>=x) ans=query(x,lson);
    else ans=query(x,rson);
    PushUp(rt);
    return ans;
}
int main()
{
    while(~scanf("%d%d%d",&h,&w,&n)){
        if(h>n) h=n;    //  这一条语句绝对不能少,虽然告诉你有10^9行,但是,我只有n张海报,所以最多有n行就够了,没有意思到这一点的话肯定会RE的;
        build(1,h,1);
        for(int i=0;i<n;i++){
            int x;
            scanf("%d",&x);
            if(Max[1]<x) printf("-1\n");
            else printf("%d\n",query(x,1,h,1));
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: