[USACO13JAN] Seating
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:
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.
- 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.
输入输出样例
10 4
A 6
L 2 4
A 5
A 2
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的更多相关文章
- 洛谷 P3071 [USACO13JAN]座位Seating(线段树)
P3071 [USACO13JAN]座位Seating 题目链接 思路: 一开始把题给读错了浪费了好多时间呜呜呜. 因为第二个撤离操作是区间修改,所以我们可以想到用线段树来做.对于第一个操作,我们只需 ...
- luoguP3071 [USACO13JAN]座位Seating
https://www.luogu.org/problem/P3071 AC代码: https://www.luogu.org/blog/user33426/solution-p3071 莫名其妙RE ...
- 洛谷 P3071 [USACO13JAN]座位Seating-线段树区间合并(判断找,只需要最大前缀和最大后缀)+分治+贪心
P3071 [USACO13JAN]座位Seating 题目描述 To earn some extra money, the cows have opened a restaurant in thei ...
- Co-prime Array&&Seating On Bus(两道水题)
Co-prime Array Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
- [luogu P2205] [USACO13JAN]画栅栏Painting the Fence
[luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...
- Educational Codeforces Round 11 B. Seating On Bus 水题
B. Seating On Bus 题目连接: http://www.codeforces.com/contest/660/problem/B Description Consider 2n rows ...
- 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 ...
- 洛谷P2202 [USACO13JAN]方块重叠Square Overlap
P2202 [USACO13JAN]方块重叠Square Overlap 题目描述 Farmer John is planning to build N (2 <= N <= 50,000 ...
- 洛谷P3068 [USACO13JAN]派对邀请函Party Invitations
P3068 [USACO13JAN]派对邀请函Party Invitations 题目描述 Farmer John is throwing a party and wants to invite so ...
随机推荐
- 【BZOJ2882】工艺(后缀数组)
[BZOJ2882]工艺(后缀数组) 题面 BZOJ权限题,我爱良心洛谷 题解 最容易的想法: 把字符串在后面接一份 然后求后缀数组就行了... #include<iostream> #i ...
- linux系统连接的概念及删除原理
硬连接:ln 源文件 目标文件 软连接:ln -s 源文件 目标文件 (目标文件不能事先存在) 硬连接是通过索引节点inode来进行连接. 在Linux文件系统中,多个文件名指向同一个索引节点,硬连接 ...
- Angular2 - 概述
*Hi DAI, 我想学习 Angular2, 我应该怎么开始? 关于学习 Angular2, 我认为你应该按照下面的列表 概述: 在您为 Angular2 应用程序编写第一个代码之前, 这将为您提供 ...
- 25.django Model
django ORM基本配置 django中遵循 Code Frist 的原则,即:根据代码中定义的类来自动生成数据库表 1.修改project数据库配置 (1)settigs.py里面 默认 DAT ...
- Java集合中的LinkedHashMap类
jdk1.8.0_144 本文阅读最好先了解HashMap底层,可前往<Java集合中的HashMap类>. LinkedHashMap由于它的插入有序特性,也是一种比较常用的Map集合. ...
- Ubuntu14.04 设置wifi热点
Ubuntu14.04 设置wifi热点 $ sudo add-apt-repository ppa:nilarimogard/webupd8 $ sudo apt-get update $ sudo ...
- 实用的Docker入门
1 Docker概述 Docker和虚拟机一样,都拥有环境隔离的能力,但它比虚拟机更加轻量级,可以使资源更大化地得到应用.首先来看Docker的架构图: 理解其中几个概念: Client(Docker ...
- C#中的多线程与线程互斥
通过多线程,C#可以并行地执行代码. 每一个线程都有它独立的执行路径,所有线程都能访问共有变量. 这就引发了线程竞争 这时就需要使用线程安全的处理方式使得线程互斥 先来看一段多线程代码 using S ...
- 纯css3打造瀑布流布局
纯css3打造瀑布流布局 原理: 1.column-count 把div中的文本分为多少列 2.column-width 规定列宽 3.column-gap 规定列间隙 4.break-inside: ...
- Win7硬盘的AHCI模式
1.什么是硬盘的AHCI模式? AHCI是串行ATA高级主控接口的英文缩写,它是Intel所主导的一项技术,它允许存储驱动程序启用高级SATA功能,如本机命令队列(NCQ)和热插拔.开启AHCI之后可 ...