[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 ...
随机推荐
- java线程安全问题以及使用synchronized解决线程安全问题的几种方式
一.线程安全问题 1.产生原因 我们使用java多线程的时候,最让我们头疼的莫过于多线程引起的线程安全问题,那么线程安全问题到底是如何产生的呢?究其本质,是因为多条线程操作同一数据的过程中,破坏了数据 ...
- Codeforces Round #446 (Div. 2)
Codeforces Round #446 (Div. 2) 总体:rating涨了好多,虽然有部分是靠和一些大佬(例如redbag和ShichengXiao)交流的--希望下次能自己做出来2333 ...
- ------ Tor(洋葱路由器)匿名网络源码分析——主程序入口点(一)------
--------------------------------------------------------<概览> tor 的源码包可以从官网下载,可能需要预先利用其它FQ软件才能访 ...
- Java集合中的HashMap类
jdk1.8.0_144 HashMap作为最常用集合之一,继承自AbstractMap.JDK8的HashMap实现与JDK7不同,新增了红黑树作为底层数据结构,结构变得复杂,效率变得更高.为满足自 ...
- 回滚 - 每天5分钟玩转 Docker 容器技术(141)
kubectl apply 每次更新应用时 Kubernetes 都会记录下当前的配置,保存为一个 revision(版次),这样就可以回滚到某个特定 revision. 默认配置下,Kubernet ...
- Oracle数据库中in()参数超过一千报错代码报错
转载请注明出处:http://www.cnblogs.com/xunzhaorendaxia/p/8570604.html 解决方案将select * from tablename where fie ...
- 小程序input输入框获取焦点时,文字会出现闪动
最近在开发小程序时,发现一个有趣的现象.input里面设置了placeholder,随后当输入框获取焦点时,文字会出现一瞬间的抖动,随后正常. 猜想可能是设置的font-family不同引起的抖动,但 ...
- Java .classpath文件Classpath entry org.maven.eclipse.MAVEN2_CLASSPATH_CONTAINER will not be exported or published异常解决办法
在实际用Maven构建Java Web项目开发过程中,有时候会出现上述情况的警告,如果不解决这个警告,就会在启动Web服务器的时候抛出无法加载Maven管理的第三方jar包的异常. 所以,要解决上面的 ...
- 使用netflix Zuul 代理你的微服务
构建 "微服务" 时的一个常见挑战是为系统的使用者提供一个统一的接口.您的服务被分割成一个个积木式的小程序,事实上这些细节本不应该对用户可见. 为了解决这个问题, Netflix ...
- 建立简单的Hash table(哈希表)by C language
#define SIZE 1000 //定义Hash table的初始大小 struct HashArray { int key; int count; struct HashArray* next; ...