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. java.lang.OutOfMemoryError: PermGen space有效解决方法

    PermGen space的全称是Permanent Generation space,是指内存的永久保存区域OutOfMemoryError: PermGen space从表面上看就是内存益出,解决 ...

  2. MongoDB存储引擎(下)——In-Memory

    前两篇文章分别介绍了MMAPv1和WiredTiger,这两个存储引擎都是会将数据持久化存储到硬盘的,除此之外,MongoDB也有只将数据存储在内存的存储引擎,那就是In-Memory. In-Mem ...

  3. node.js连接MySQL操作及注意事项

    node.js作为服务端的js运行环境已经出现了有几年了,最近我有个朋友也在做这方面的开发,但是也是刚刚接触,遇到了很多坑.前几天他们在操作数据库的时候出现了点问题,后来我们一起看了看,其实都是nod ...

  4. 深入解析条件变量(condition variables)

    深入解析条件变量 什么是条件变量(condition variables) 引用APUE中的一句话: Condition variables are another synchronization m ...

  5. 关于classpath

    classpath是用来设计JAVA类文件(.class)所在的路径 classpath的主要作用就是当你在一个shell窗口下执行命令时,可以从在classpath中设置的目录搜索,不用一层再一层的 ...

  6. php做的一个简易爬虫

    对于爬虫的好奇好像由来已久,一直在研究python的爬虫,今天得空研究研究php的爬虫 index.php <?php header("Content-Type:text/html;c ...

  7. css多浏览常见问题

    关于CSS对各个浏览器兼容已经是老生常谈的问题了, 网络上的教程遍地都是.以下内容没有太多新颖, 纯属个人总结, 希望能对初学者有一定的帮助. 一.CSS HACK 以下两种方法几乎能解决现今所有HA ...

  8. NET Core2.0 Memcached踩坑,基于EnyimMemcachedCore整理MemcachedHelper帮助类。

    DotNetCore2.0下使用memcached缓存. Memcached目前微软暂未支持,暂只支持Redis,由于项目历史原因,先用博客园开源项目EnyimMemcachedCore,后续用到的时 ...

  9. Cesium剖面分析

  10. FastJson简单使用

    首先建立两个实体类,Student.java 和 Teacher.java public class Student { private int id; private String name; pr ...