cf-64d(Parking Lot)成段更新+區間合併

題目看了一個小時,想了半個小時,敲了一個小時,debug將近三個半小時。。。。。。//還是看數據的
將停車分成兩個操作,先找出停車的起始點位置,然後再成段更新。
這裏用線段樹維護區間最多能停多長的車(包括前距和後距)
找其實點時,先判斷能否放在起點,此時它要放的長度只需lth(+f也無妨),
如果可以,就找到了。
否則要放的長度需要lth+b+f(當放在末尾時,其實不需要+f,但爲了方便,我們可以將區間延長b+f,然後對求出的起始點判斷是否可行即可),如果求出結果可行,就以該起點到車的末尾成段更新,否則輸出-1.

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <stack>
#include <vector>
#include <string.h>
#include <queue>
#define msc(X) memset(X,-1,sizeof(X))
#define ms(X) memset(X,0,sizeof(X))
typedef long long LL;
using namespace std;
const int MAXN=1e5+1050;
#define lson tn<<1,l,mid
#define rson tn<<1|1,mid+1,r
struct _Tree
{
    int len,lmax,rmax,amax,lazy;
}tree[MAXN<<2];
void PushUp(int tn)
{
    tree[tn].lmax=(tree[tn<<1].lmax==tree[tn<<1].len?
        (tree[tn<<1].len+tree[tn<<1|1].lmax):tree[tn<<1].lmax);
    tree[tn].rmax=(tree[tn<<1|1].rmax==tree[tn<<1|1].len?
        (tree[tn<<1].rmax+tree[tn<<1|1].len):tree[tn<<1|1].rmax);
    tree[tn].amax=max(tree[tn<<1].amax,tree[tn<<1|1].amax);
    tree[tn].amax=max(tree[tn].amax,tree[tn<<1].rmax+tree[tn<<1|1].lmax);   
}
void build(int tn,int l,int r)
{
    tree[tn].len=tree[tn].lmax=
    tree[tn].rmax=tree[tn].amax=r-l+1;
    tree[tn].lazy=-1;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
}
void PushDown(int tn)
{
    if(tree[tn].lazy!=-1){
        int flg=tree[tn].lazy;
        tree[tn<<1].lazy=tree[tn<<1|1].lazy=flg;
        tree[tn<<1].amax=tree[tn<<1].lmax
            =tree[tn<<1].rmax=(flg?0:tree[tn<<1].len);
        tree[tn<<1|1].amax=tree[tn<<1|1].lmax
            =tree[tn<<1|1].rmax=(flg?0:tree[tn<<1|1].len);
        tree[tn].lazy=-1;
    }
}
int query(int tn,int l,int r,int len)
{

    if(l==r) return l;
    PushDown(tn);
    int mid=(l+r)>>1;
    if(tree[tn<<1].amax>=len)
        return query(lson,len);
    else if(tree[tn<<1].rmax+tree[tn<<1|1].lmax>=len)
            return mid-tree[tn<<1].rmax+1;
    else return query(rson,len);
}
void update(int tn,int l,int r,int x,int y,int flg)
{
    if(x<=l&&r<=y){
        tree[tn].lazy=flg;
        tree[tn].amax=tree[tn].lmax
            =tree[tn].rmax=
            (flg?0:tree[tn].len);
        return;
    }
    PushDown(tn);
    int mid=(l+r)>>1;
    if(x<=mid) update(lson,x,y,flg);
    if(y>mid) update(rson,x,y,flg);
    PushUp(tn);
}
struct _Query
{
    int frm,to;
}qry[110];
int main(int argc, char const *argv[])
{
    int L,b,f;
    scanf("%d %d %d",&L,&b,&f);
    build(1,1,L+b+f);
    ms(qry);
    int q;
    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        int typ,lth;
        scanf("%d %d",&typ,&lth);   
        if(typ==1){
            if(lth+b+f>tree[1].amax) {puts("-1");
            continue;}
            int pos=query(1,1,L+b+f,lth+f);//特殊處理
            if(pos==1){
                pos--;
                if(pos+lth<=L){
                update(1,1,L+b+f,pos+1,pos+lth,1);
                printf("%d\n",pos );
                qry[i].frm=pos+1,
                qry[i].to=pos+lth;
                }
            }
            else {
                pos=query(1,1,L+b+f,lth+b+f);
                if(pos!=-1){
                    pos+=b-1;
                    if(pos+lth<=L){
                    update(1,1,L+b+f,pos+1,pos+lth,1);
                    printf("%d\n",pos );
                    qry[i].frm=pos+1,
                    qry[i].to=pos+lth;
                    }
                    else puts("-1");
                }
                else puts("-1");
            }
        }
        else 
         update(1,1,L+b+f,qry[lth].frm,qry[lth].to,0);
    }
    return 0;
}
/*
2 2 3
1
1 2

10 0 0
11
1 1
1 1
1 1
1 1
1 1
2 5
1 1
1 1
1 1
1 1
1 1
1 1

20 1 2
10
1 3
1 2
2 2
2 1
1 4
1 2
1 2
2 7
1 2
1 1

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