https://www.luogu.org/problem/show?pid=3071

题目描述

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.

有一排n个座位,m次操作。A操作:将a名客人安置到最左的连续a个空位中,没有则不操作。L操作:[a,b]的客人离开。

求A操作的失败次数。

输入输出格式

输入格式:

  • 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).

输出格式:

  • Line 1: The number of parties that are turned away.

输入输出样例

输入样例#1:

10 4
A 6
L 2 4
A 5
A 2
输出样例#1:

1 

线段树
实践再次证明:数组比结构体要快
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,x,y,opl,opr;
#define N 500001*4
int maxx[N],max_l[N],max_r[N],sum[N],flag[N];
void build(int k,int l,int r)
{
sum[k]=maxx[k]=max_l[k]=max_r[k]=r-l+;
if(l==r) return;
int mid=l+r>>;
build(k<<,l,mid);build((k<<)+,mid+,r);
}
void down(int k)
{
flag[k<<]=flag[(k<<)+]=flag[k];
if(flag[k]==)
maxx[k<<]=max_l[k<<]=max_r[k<<]=maxx[(k<<)+]=max_l[(k<<)+]=max_r[(k<<)+]=;
else
{
maxx[k<<]=max_l[k<<]=max_r[k<<]=sum[k<<];
maxx[(k<<)+]=max_l[(k<<)+]=max_r[(k<<)+]=sum[(k<<)+];
}
flag[k]=;
}
int ask(int k,int l,int r)
{
if(l==r) return l;
if(flag[k]) down(k);
int mid=l+r>>;
if(maxx[k<<]>=x) return ask(k<<,l,mid);
if(max_l[(k<<)+]+max_r[k<<]>=x) return mid-max_r[k<<]+;
return ask((k<<)+,mid+,r);
}
void up(int k)
{
maxx[k]=max(max(maxx[k<<],maxx[(k<<)+]),max_r[k<<]+max_l[(k<<)+]);
if(sum[k<<]==maxx[k<<]) max_l[k]=sum[k<<]+max_l[(k<<)+];
else max_l[k]=max_l[k<<];
if(sum[(k<<)+]==maxx[(k<<)+]) max_r[k]=sum[(k<<)+]+max_r[k<<];
else max_r[k]=max_r[(k<<)+];
}
void change(int k,int f,int l,int r)
{
if(l>=opl&&r<=opr)
{
if(f==)
{
maxx[k]=max_l[k]=max_r[k]=;
flag[k]=;
return;
}
else
{
maxx[k]=max_l[k]=max_r[k]=sum[k];
flag[k]=;
return;
}
}
if(flag[k]) down(k);
int mid=l+r>>;
if(opr<=mid) change(k<<,f,l,mid);
else if(opl>mid) change((k<<)+,f,mid+,r);
else
{
change(k<<,f,l,mid);
change((k<<)+,f,mid+,r);
}
up(k);
}
void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
}
int main()
{
int cnt=;
char p[];
read(n); read(m);
build(,,n);
for(int i=;i<=m;i++)
{
scanf("%s",p);
if(p[]=='A')
{
read(x);
if(maxx[]<x) cnt++;
else
{
opl=ask(,,n);
opr=opl+x-;
change(,,,n);
}
}
else
{
read(opl); read(opr);
change(,,,n);
}
}
printf("%d",cnt);
}

[USACO13JAN] Seating的更多相关文章

  1. 洛谷 P3071 [USACO13JAN]座位Seating(线段树)

    P3071 [USACO13JAN]座位Seating 题目链接 思路: 一开始把题给读错了浪费了好多时间呜呜呜. 因为第二个撤离操作是区间修改,所以我们可以想到用线段树来做.对于第一个操作,我们只需 ...

  2. luoguP3071 [USACO13JAN]座位Seating

    https://www.luogu.org/problem/P3071 AC代码: https://www.luogu.org/blog/user33426/solution-p3071 莫名其妙RE ...

  3. 洛谷 P3071 [USACO13JAN]座位Seating-线段树区间合并(判断找,只需要最大前缀和最大后缀)+分治+贪心

    P3071 [USACO13JAN]座位Seating 题目描述 To earn some extra money, the cows have opened a restaurant in thei ...

  4. Co-prime Array&&Seating On Bus(两道水题)

     Co-prime Array Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  5. [luogu P2205] [USACO13JAN]画栅栏Painting the Fence

    [luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...

  6. Educational Codeforces Round 11 B. Seating On Bus 水题

    B. Seating On Bus 题目连接: http://www.codeforces.com/contest/660/problem/B Description Consider 2n rows ...

  7. Educational Codeforces Round 11B. Seating On Bus 模拟

    地址:http://codeforces.com/contest/660/problem/B 题目: B. Seating On Bus time limit per test 1 second me ...

  8. 洛谷P2202 [USACO13JAN]方块重叠Square Overlap

    P2202 [USACO13JAN]方块重叠Square Overlap 题目描述 Farmer John is planning to build N (2 <= N <= 50,000 ...

  9. 洛谷P3068 [USACO13JAN]派对邀请函Party Invitations

    P3068 [USACO13JAN]派对邀请函Party Invitations 题目描述 Farmer John is throwing a party and wants to invite so ...

随机推荐

  1. 【BZOJ2154】Crash的数字表格(莫比乌斯反演)

    [BZOJ2154]Crash的数字表格(莫比乌斯反演) 题面 BZOJ 简化题意: 给定\(n,m\) 求\[\sum_{i=1}^n\sum_{j=1}^mlcm(i,j)\] 题解 以下的一切都 ...

  2. 【POI2001】【HDU1814】和平委员会

    题面 Description 根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立. 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍. 此委员会必须满足下列条 ...

  3. 我的emacs考场配置

    豪华配置(复制的神犇的,已膜改) (global-set-key [f9] 'compile-file) (global-set-key [f10] 'gud-gdb) (global-set-key ...

  4. [POI2014]HOT-Hotels

    题目描述 There are towns in Byteotia, connected with only roads. Each road directly links two towns. All ...

  5. Spring【AOP模块】就是这么简单

    前言 到目前为止,已经简单学习了Spring的Core模块.....于是我们就开启了Spring的AOP模块了...在讲解AOP模块之前,首先我们来讲解一下cglib代理.以及怎么手动实现AOP编程 ...

  6. 使用IIS Server Farms搭建应用服务负载均衡

    当公司的业务扩大, 伴随着大量的请求,应用服务器的承受能力已经不能满足不断增长的业务需求,使用IIS Server Farms搭建应负载均衡的方式,把请求分发给不同的应用服务器进行处理,这个时候就降低 ...

  7. 深入理解Java虚拟机到底是什么

    摘自:http://blog.csdn.net/zhangjg_blog/article/details/20380971 什么是Java虚拟机 我们都知道Java程序必须在虚拟机上运行.那么虚拟机到 ...

  8. 误删 /user/bin目录后的补救

    当危险的动作发生, 误删 /user/bin目录后的补救 以下是昨天晚上真实的误操作现场,模拟记录一下 (这是测试环境,所以操作得很随意,有些执行动作很不规范) 在上面编译一个软件Dboop,完事以后 ...

  9. 【Python】 配置文件相对路径&软件自动执行的工作目录

    今天对监控脚本做了一些变更,然后突然发现监控全部都失效了..排查了半天问题仍然不知所踪.最终发现居然是一个踩过好几次的老坑.. 就是脚本内写的配置文件为了调试方便写成了相对路径,但是在上线时没有意识到 ...

  10. STL --> vector向量

    vector向量 vector是一种对象实体,能够容纳许多其他类型相同的元素,因为又被称为容器. 头文件 在使用它时,需要包含头文件 <vector>. #include <vect ...