HDU 2795 線段樹

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


因爲h的範圍爲(1,10^9),而n的範圍是(1,200000)

而用到的h不會超過n個,所以h可以取(n,h)的最小值,那麼h的範圍也就變成了(1,200000)


線段樹,求值過程中更新

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#define N 200002
using namespace std;
int Max[N<<2],w;
void build(int L,int R,int rt){
    Max[rt]=w;
    if(L==R){
        return ;
    }
    int mid=(L+R)>>1;
    build(L,mid,rt<<1);
    build(mid+1,R,rt<<1|1);
    return ;
}
void pushup(int rt){
    Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
int get(int x,int L,int R,int rt){
   if(L==R){
        Max[rt]-=x;
        return L;
    }

    int mid=(L+R)>>1;
    int l=rt<<1,r=l|1;
    int ret=Max[l]>=x?get(x,L,mid,l):get(x,mid+1,R,r);
    pushup(rt);
    return ret;
}
int main()
{
    int x,h,n;
    while(cin>>h>>w>>n){
        memset(Max,0,sizeof(Max));
        if(h>n)
        h=n;
        build(1,h,1);
        while(n--){
            scanf("%d",&x);
            if(Max[1]<x)
            printf("-1\n");
            else
            printf("%d\n",get(x,1,h,1));
        }
    }
    return 0;
}




發佈了104 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章