P2052【USACO 2013 January Gold】座位

问题描述

奶牛们开了一家餐馆。该餐馆里有N(1 <= N <= 500,000)个排成一列的座位(编号1到N),编号越小的座位越靠近窗户。早晨开业时,座位都是空的。

今天餐馆里发生了M(1 <= M <= 300,000)个事件,这些事件总共分两类:
1.一伙人一起来就餐,该伙人共P(1 <= p <= N)个人,这伙人想坐在一段连续的位置上就餐,如果能坐下,他们希望座位尽量靠近窗户。如果无法坐下,他们会马上离开。
2.坐在a号到b号(1 <= a <= b <= N) 这连续一段座位的客人用餐完毕后离开了。

请帮贝西计算今天有多少伙人因为没有满足他们要求的座位而离开了。

输入格式

第一行,两个空格间隔的整数N和M
接下来M行,按时间先后描述了今天发生的事件:
字母A和一个整数P表示一伙P个人到达的餐馆。
字母L和两个整数a,b表示a到b这一段位置的客人离开了。

输出格式

一个整数,表示所求的结果

样例输入

10 4
A 6
L 2 4
A 5
A 2

样例输出

1

题解

线段树,维护区间最大连续空位的长度。注意1.查询连续区间长度a时要从先找左再找中间再找右边,满足标号最小。2.数组大小。3.细节细节!

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stdio.h>
using namespace std;
#define maxn 500001
struct node{
    int l,r,a,b,lazy,max1;
};
node tree[maxn*20];
int n,m;
int ans;
void update(int p)
{
    int ls=p<<1,rs=p<<1|1;
    int le=tree[ls].b-tree[ls].a+1,re=tree[rs].b-tree[rs].a+1;
    if(le==tree[ls].l) tree[p].l=le+tree[rs].l;
    else tree[p].l=tree[ls].l;
    if(re==tree[rs].r) tree[p].r=re+tree[ls].r;
    else tree[p].r=tree[rs].r;
    tree[p].max1=max(tree[ls].max1,max(tree[rs].max1,tree[ls].r+tree[rs].l));
}
void bt(int p,int x,int y)
{
    tree[p].a=x;tree[p].b=y;
    if(x==y)
    {
        tree[p].l=tree[p].r=tree[p].max1=1;
        return ;
    }
    bt(p<<1,x,(x+y)>>1);
    bt(p<<1|1,((x+y)>>1)+1,y);
    tree[p].l=tree[p].r=tree[p].max1=y-x+1;
}
void pd(int s)
{
    int ls=s<<1,rs=s<<1|1;
    tree[ls].lazy=tree[rs].lazy=tree[s].lazy;
    if(tree[s].lazy==1)
    {
        tree[ls].l=tree[ls].r=tree[ls].max1=0;
        tree[rs].r=tree[rs].l=tree[rs].max1=0;
    }
    else 
    {
        tree[ls].l=tree[ls].r=tree[ls].max1=tree[ls].b-tree[ls].a+1;
        tree[rs].r=tree[rs].l=tree[rs].max1=tree[rs].b-tree[rs].a+1;
    }
    tree[s].lazy=0;
}
int check;
int find(int p,int d)//先查左边再查右边最后查中间
{
    int ls=p<<1,rs=p<<1|1;
    if(tree[p].l>=d) return tree[p].a;

    if(tree[p].max1<d) return -1;
    if(tree[p].lazy) pd(p);
    if(tree[ls].max1>=d) return find(ls,d);
    if(tree[ls].r+tree[rs].l>=d) return tree[ls].b-tree[ls].r+1;
    if(tree[rs].max1>=d) return find(rs,d);
}
void arrive(int s,int x,int y)
{    

    if(x<=tree[s].a&&tree[s].b<=y){
        tree[s].l=tree[s].r=tree[s].max1=0;
        tree[s].lazy=1;
        return;
    }
    if(tree[s].lazy)pd(s);
    int mid=(tree[s].a+tree[s].b)>>1;
    if(x<=mid) arrive(s<<1,x,y);
    if(y>mid) arrive(s<<1|1,x,y);
    update(s);
}
void leave(int s,int x,int y)
{
    if(x<=tree[s].a&&tree[s].b<=y){
        tree[s].l=tree[s].r=tree[s].max1=tree[s].b-tree[s].a+1;
        tree[s].lazy=2;
        return;
    }
    if(tree[s].lazy)pd(s);
    int mid=(tree[s].a+tree[s].b)>>1;
    if(x<=mid) leave(s<<1,x,y);
    if(y>mid) leave(s<<1|1,x,y);
    update(s);
}
int main()
{
    int i,j,k;
    scanf("%d%d",&n,&m);
    bt(1,1,n);
    char c;
    for(i=1;i<=m;i++)
    {
        int x,y,z;
        cin>>c;
        if(c=='A')
        {
          scanf("%d",&x);
          z=find(1,x);
          if(z==-1) ans++;
          else arrive(1,z,z+x-1);
        }
        else 
        {
            scanf("%d%d",&x,&y);
            leave(1,x,y);
        }
    }
    cout<<ans;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值