洛谷 P3071 [USACO13JAN]座位Seating-线段树区间合并(判断找,只需要最大前缀和最大后缀)+分治+贪心
P3071 [USACO13JAN]座位Seating
题目描述
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.
输入输出样例
说明
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.
Party #3 is turned away. All other parties are seated.
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e6+;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 struct Tree{
int lch,rch,val,lazy;
}tree[maxn<<]; void pushup(int l,int r,int rt)
{
int m=(l+r)>>;
if(tree[rt<<].val==(m-l+)) tree[rt].lch=tree[rt<<].val+tree[rt<<|].lch;
else tree[rt].lch=tree[rt<<].lch;
if(tree[rt<<|].val==(r-m)) tree[rt].rch=tree[rt<<|].val+tree[rt<<].rch;
else tree[rt].rch=tree[rt<<|].rch;
tree[rt].val=max(max(tree[rt<<].val,tree[rt<<|].val),tree[rt<<].rch+tree[rt<<|].lch);
} void pushdown(int l,int r,int rt)
{
int m=(l+r)>>;
if(tree[rt].lazy){
if(tree[rt].lazy==){//清空
tree[rt<<].lazy=tree[rt<<|].lazy=tree[rt].lazy;
tree[rt<<].lch=tree[rt<<].rch=tree[rt<<].val=m-l+;
tree[rt<<|].lch=tree[rt<<|].rch=tree[rt<<|].val=r-m;
}
if(tree[rt].lazy==){//坐满
tree[rt<<].lazy=tree[rt<<|].lazy=tree[rt].lazy;
tree[rt<<].lch=tree[rt<<].rch=tree[rt<<].val=;
tree[rt<<|].lch=tree[rt<<|].rch=tree[rt<<|].val=;
}
tree[rt].lazy=;
}
} void build(int l,int r,int rt)
{
tree[rt].lazy=;
if(l==r){
tree[rt].lch=tree[rt].rch=tree[rt].val=;
return ;
} int m=(l+r)>>;
build(lson);
build(rson);
pushup(l,r,rt);
} void update(int L,int R,int c,int l,int r,int rt)
{
if(tree[rt].lazy){
pushdown(l,r,rt);
} if(L<=l&&r<=R){
if(c==){
tree[rt].lch=tree[rt].rch=tree[rt].val=r-l+;
}
if(c==){
tree[rt].lch=tree[rt].rch=tree[rt].val=;
}
tree[rt].lazy=c;
return ;
} int m=(l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R> m) update(L,R,c,rson);
pushup(l,r,rt);
} int query(int c,int l,int r,int rt)
{
if(tree[rt].lazy){
pushdown(l,r,rt);
} if(l==r){
return l;
} int m=(l+r)>>;
if(tree[rt<<].val>=c) return query(c,lson);
else if(tree[rt<<].rch+tree[rt<<|].lch>=c) return m-tree[rt<<].rch+;
else return query(c,rson);
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
build(,n,);
int num=;
for(int i=;i<=m;i++){
char op[];
scanf("%s",op);
if(op[]=='A'){
int x;
scanf("%d",&x);
if(tree[].val>=x){
int ans=query(x,,n,);
update(ans,ans+x-,,,n,);
}
else num++;
}
else{
int l,r;
scanf("%d%d",&l,&r);
update(l,r,,,n,);
}
}
printf("%d\n",num);
}
洛谷 P3071 [USACO13JAN]座位Seating-线段树区间合并(判断找,只需要最大前缀和最大后缀)+分治+贪心的更多相关文章
- 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治
		
P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...
 - 洛谷 P3071 [USACO13JAN]座位Seating(线段树)
		
P3071 [USACO13JAN]座位Seating 题目链接 思路: 一开始把题给读错了浪费了好多时间呜呜呜. 因为第二个撤离操作是区间修改,所以我们可以想到用线段树来做.对于第一个操作,我们只需 ...
 - 洛谷 P2574 XOR的艺术(线段树 区间异或 区间求和)
		
To 洛谷.2574 XOR的艺术 题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的 ...
 - UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)
		
"Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...
 - 洛谷 P3373 【模板】线段树 2
		
洛谷 P3373 [模板]线段树 2 洛谷传送门 题目描述 如题,已知一个数列,你需要进行下面三种操作: 将某区间每一个数乘上 xx 将某区间每一个数加上 xx 求出某区间每一个数的和 输入格式 第一 ...
 - SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并
		
Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...
 - POJ 3667 Hotel(线段树 区间合并)
		
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
 - HDU 3911 线段树区间合并、异或取反操作
		
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
 - HDU 3911 Black And White(线段树区间合并+lazy操作)
		
开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...
 
随机推荐
- core EFCore 开始尝试
			
准备工作: 工程:core + console 引用包: Install-Package Microsoft.EntityFrameworkCore Install-Package Microsoft ...
 - Java8 新特性 Streams map() 示例
			
在Java 8中stream().map(),您可以将对象转换为其他对象.查看以下示例: 1.大写字符串列表 1.1简单的Java示例将Strings列表转换为大写. TestJava8.java p ...
 - Swift控制手电筒操作(iOS)
			
手电筒是iphone的一个常用功能,最常用的操作就是turn on和turn off,下面我们来实现一个简单的手电筒操作程序:一个按钮来控制iphone手电筒的On和Off,并且按钮的text也做相应 ...
 - asp.net 练习 js 调用webservice
			
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
 - 【CodeForces】961 F. k-substrings 字符串哈希+二分
			
[题目]F. k-substrings [题意]给定长度为n的串S,对于S的每个k-子串$s_ks_{k+1}...s_{n-k+1},k\in[1,\left \lceil \frac{n}{2} ...
 - CSS基础之选择器
			
一:CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到,一个样式表时,就会按照. 二:CSS语法 每个CSS有两部分组成:选择器和声明 ...
 - 树形dp(B - Computer HDU - 2196 )
			
题目链接:https://cn.vjudge.net/contest/277955#problem/B 题目大意:首先输入n代表有n个电脑,然后再输入n-1行,每一行输入两个数,t1,t2.代表第(i ...
 - 使用vue的一些经验
			
虽然说VUE是数据驱动视图的框架,但有时候不得不获取DOM来获得一些样式属性,做一些操作,这时候就需要VUE获取DOM对象的方法. vue获取DOM对象的方法: 如果是操作组件内部的DOM,可以通过给 ...
 - Permutation Index I & II
			
Given a permutation which contains no repeated number, find its index in all the permutations of the ...
 - linux查看目录的四种方法(ls只显示目录)【转】
			
1.ls -d * amosli@amosli-pc:~$ ls -d * %APPDATA% develop many sorted.txt workspace bank Documents Mus ...