bzoj 3050: [Usaco2013 Jan]Seating
来源:互联网 发布:java web开发实战入门 编辑:程序博客网 时间:2024/04/28 15:08
3050: [Usaco2013 Jan]Seating
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 128 Solved: 73
[Submit][Status][Discuss]
Description
To earn some extra money, the cows have opened a restaurant in their barn specializing in milkshakes. The restaurant has N seats (1 <= N <= 500,000) in a row. Initially, they are all empty. Throughout the day, there are M different events that happen in sequence at the restaurant (1 <= M <= 300,000). The two types of events that can happen are: 1. A party of size p arrives (1 <= p <= N). Bessie wants to seat the party in a contiguous block of p empty seats. If this is possible, she does so in the lowest position possible in the list of seats. If it is impossible, the party is turned away. 2. A range [a,b] is given (1 <= a <= b <= N), and everybody in that range of seats leaves. Please help Bessie count the total number of parties that are turned away over the course of the day.
Input
* Line 1: Two space-separated integers, N and M.
* Lines 2..M+1: Each line describes a single event. It is either a line of the form "A p" (meaning a party of size p arrives) or "L a b" (meaning that all cows in the range [a, b] leave).
Output
* Line 1: The number of parties that are turned away.
Sample Input
A 6
L 2 4
A 5
A 2
INPUT DETAILS: There are 10 seats, and 4 events. First, a party of 6 cows arrives. Then all cows in seats 2..4 depart. Next, a party of 5 arrives, followed by a party of 2.
Sample Output
OUTPUT DETAILS: Party #3 is turned away. All other parties are seated.
HINT
Source
Gold
题解参见:poj 3667 hotel
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define N 500003using namespace std;int n,m,ans;int sum[4*N],lx[N*4],rx[N*4],delta[4*N];void update(int x){ sum[x]=sum[x<<1]+sum[x<<1|1]; lx[x]=rx[x]=sum[x];}void build(int now,int l,int r){ if (l==r) { sum[now]=lx[now]=rx[now]=1; return; } int mid=(l+r)/2; build(now<<1,l,mid); build(now<<1|1,mid+1,r); update(now);}void pushdown(int x,int l,int r){ if (!delta[x]) return; int mid=(l+r)/2; sum[x<<1]=lx[x<<1]=rx[x<<1]=(delta[x]==1?0:mid-l+1); sum[x<<1|1]=lx[x<<1|1]=rx[x<<1|1]=(delta[x]==1?0:r-mid); delta[x<<1]=delta[x<<1|1]=delta[x]; delta[x]=0;}int find(int now,int l,int r,int x){ if(l==r) return l; pushdown(now,l,r); int mid=(l+r)/2; if (sum[now<<1]>=x) return find(now<<1,l,mid,x); else if (rx[now<<1]+lx[now<<1|1]>=x) return mid-rx[now<<1]+1; else return find(now<<1|1,mid+1,r,x);}void update1(int x,int l,int r){ int mid=(l+r)/2; sum[x]=max(max(sum[x<<1],sum[x<<1|1]),rx[x<<1]+lx[x<<1|1]); lx[x]=lx[x<<1]+(lx[x<<1]==mid-l+1?lx[x<<1|1]:0); rx[x]=rx[x<<1|1]+(rx[x<<1|1]==r-mid?rx[x<<1]:0);}void qjchange(int now,int l,int r,int ll,int rr,int v){ if (l>=ll&&r<=rr) { if (v==0) sum[now]=lx[now]=rx[now]=0; else sum[now]=lx[now]=rx[now]=r-l+1; delta[now]=v+1; return; } pushdown(now,l,r); int mid=(l+r)/2; if (ll<=mid) qjchange(now<<1,l,mid,ll,rr,v); if (rr>mid) qjchange(now<<1|1,mid+1,r,ll,rr,v); update1(now,l,r);}int main(){ freopen("seating.in","r",stdin); freopen("seating.out","w",stdout); scanf("%d%d",&n,&m); build(1,1,n); for (int i=1;i<=m;i++) { char s[10]; scanf("%s",s); if (s[0]=='A') { int x; scanf("%d",&x); if (sum[1]<x) ans++; else { int pos=find(1,1,n,x); qjchange(1,1,n,pos,pos+x-1,0); } } else { int x,y; scanf("%d%d",&x,&y); qjchange(1,1,n,x,y,1); } } printf("%d\n",ans);}
- bzoj 3050: [Usaco2013 Jan]Seating
- BZOJ 3050: [Usaco2013 Jan] Seating
- bzoj 3050: [Usaco2013 Jan] Seating
- [BZOJ3050][Usaco2013 Jan]Seating(线段树)
- 【BZOJ】【P3028】【Usaco2013 Jan】【Cow Lineup】【题解】
- BZOJ 3049: [Usaco2013 Jan]Island Travels
- bzoj 3048: [Usaco2013 Jan]Cow Lineup
- bzoj 3049: [Usaco2013 Jan]Island Travels
- BZOJ 3049: [Usaco2013 Jan]Island Travels
- 【bzoj 3048】[Usaco2013 Jan]Cow Lineup(单调队列)
- 【bzoj 3049】[Usaco2013 Jan]Island Travels(状压dp)
- BZOJ 3048: [Usaco2013 Jan]Cow Lineup 单调队列
- [Usaco2013 Jan] Square Overlap
- [Usaco2013 Jan]Island Travels
- bzoj3048 [Usaco2013 Jan]Cow Lineup
- 3048: [Usaco2013 Jan]Cow Lineup
- 【bzoj3048】[Usaco2013 Jan]Cow Lineup
- bzoj3048[Usaco2013 Jan]Cow Lineup 单调队列
- wav文件格式分析详解和解析代码
- ubuntu下通过unixODBC连接MS SqlServer2005
- 第五周 三角形5
- 类学习八
- 归并排序(附Java实现和分析)
- bzoj 3050: [Usaco2013 Jan]Seating
- C++拷贝构造函数详解
- 在Ubuntu 10.04中文版下Qt编程,使用unixODBC和FreeTDS连接MS SQL Server 2005,并且中文不出现乱码的方法
- POJ 1845 Sumdiv 简单数论问题
- spring中context:property-placeholder/元素
- C++文件读写 实现文件每行数据齐长输出
- 第五周 游戏角色类
- 第五周上机实践项目 项目1--三角形类雏形(1)
- 高度和宽度不固定元素水平和垂直居中的实现(父元素高度和宽度固定)(完美解决兼容问题)