1023 Hotel 端点线段树 区间清空 删除 定长区间查询 puts和cout不要一起用

 链接:https://ac.nowcoder.com/acm/problem/51140
来源:牛客网

题目描述

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N(1≤N≤50,000)(1 \leq N \leq 50,000)(1N50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).
The cows and other visitors arrive in groups of size Di(1≤Di≤N)D_i (1 \leq D_i \leq N)Di(1DiN) and approach the front desk to check in. Each group i requests a set of DiD_iDi contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di−1r..r+D_i-1r..r+Di1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of r to be the smallest possible.
Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi..Xi+Di−1(1≤Xi≤N−Di+1)X_i ..X_i +D_i-1 (1 \leq X_i \leq N-D_i+1)Xi..Xi+Di1(1XiNDi+1). Some (or all) of those rooms might be empty before the checkout.
Your job is to assist Canmuu by processing M (1≤M<50,000)(1 \leq M \lt 50,000)(1M<50,000) checkin/checkout requests. The hotel is initially unoccupied.

输入描述:

* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and Di (b) Three space-separated integers representing a check-out: 2, XiX_iXi, and DiD_iDi

输出描述:

* Lines 1.....: For each check-in request, output a single line with a single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.
示例1

输入

复制
10 6
1 3
1 3
1 3
1 3
2 5 5
1 6

输出

复制
1
4
7
0
5

分析

区间清空和装满操作,所以是线段树。

端点线段树,维护区间里空的房间的数量,左边界空房间数量,右边界空房间数量,

修改操作,如果是清空,就将所有数量清空,lazy标记改成1。装满人,就将所有房间装满,lazy标记改成0。

查询操作,由于宿管比较喜欢找下标低的给客人,所以从左端点开始找。

找到一个有长度为 len 的空间,如果左区间没有len ,就往右区间找,如果左区间的右端点和右区间的左端点有len ,就将起始位置mid - tr[ul].rsum + 1返回,把这段长度为len的区间清空

端点线段树,所以push_up操作的时候sum 值要对左区间sum 和右区间 sum 和左区间右边界 + 右区间 左边界取最大值。

//-------------------------代码----------------------------

// #define int ll
const int N = 2e6+10;
int n,m;

struct node {
    int l,r;
    int lsum,rsum,sum,len,add;
} tr[N];

#define root tr[u]
#define lt tr[ul]
#define rh tr[ur]
void build(int u,int l,int r) {
    root.add=0;root.l = l,root.r = r;
    root.sum=root.len=root.lsum=root.rsum=r-l+1;
    if(l==r)  return;
    int mid=(l+r)>>1;
    build(ul,l,mid);
    build(ur,mid+1,r);
}
void push_up(int u) {
    if(lt.sum == lt.len) root.lsum = lt.sum + rh.lsum; else root.lsum = lt.lsum;
    if(rh.sum == rh.sum) root.rsum = rh.sum + lt.rsum; else root.rsum = rh.rsum;
    root.sum = max({rh.sum,lt.sum,rh.lsum + lt.rsum});
}
void push_down(int u) {
    if(!root.add)  return;
    if(root.add==1)
    {
        lt.add=rh.add=1;
        lt.sum=lt.lsum=lt.rsum=0;
        rh.sum=rh.lsum=rh.rsum=0;
    }
    if(root.add==2)
    {
        lt.add=rh.add=2;
        lt.sum=lt.lsum=lt.rsum=lt.len;
        rh.sum=rh.rsum=rh.lsum=rh.len;
    }
    root.add=0;
}
void modify(int u,int l,int r,int add) {
    push_down(u);
    if(l<=tr[u].l&&root.r<=r)
    {
        if(add==1)  root.sum=root.lsum=root.rsum=0;
        else root.sum=root.lsum=root.rsum=root.len;
        root.add=add;
        return;
    }
    int mid=(root.l+root.r)>>1;
    if(l<=mid)  modify(ul,l,r,add);
    if(mid<r)  modify(ur,l,r,add);
    push_up(u);
}
int query(int u,int need) {
    push_down(u);
    if(root.l==root.r)  return root.l;
    int mid=(root.l+root.r)>>1;
    if(tr[ul].sum>=need)  return query(ul,need);
    if(lt.rsum+rh.lsum>=need)  return mid-lt.rsum+1;
    else return query(ur,need);
} 
void solve() 
{
    cin>>n>>m;
    build(1,1,n);
    while(m--)
    {
        int op,x,y;cin>>op>>x;
        if(op==1)
        {
            if(tr[1].sum>=x)
            {
                int left=query(1,x);
                cout<<left<<endl;
                modify(1,left,left+x-1,1);
            }
            else  cout<<0<<endl;//出大问题
        }
        else
        {
            cin>>y;
            modify(1,x,x+y-1,2);
        }
    }
}
void main_init() {}
signed main(){
    AC();clapping();TLE;
    cout<<fixed<<setprecision(12);
    main_init();
//  while(cin>>n,n)
//  while(cin>>n>>m,n,m)
//    int t;cin>>t;while(t -- )
    solve();
//    {solve(); }
    return 0;
}

/*样例区


*/

//------------------------------------------------------------

 

posted @ 2022-08-11 18:01  er007  阅读(22)  评论(0编辑  收藏  举报