luoguP3071 [USACO13JAN]座位Seating
https://www.luogu.org/problem/P3071
AC代码:
https://www.luogu.org/blog/user33426/solution-p3071
莫名其妙RE:
#include<cstdio>
#include<iostream>
using namespace std;
#define MAX 500005
#define lson o<<1
#define rson o<<1|1
int n,m,ans,cnt;
int addv[MAX];
struct node{
int lm, rm, xm;
//从左到右的最大空位置的数
//从右到左的
//整个的
}tr[MAX<<2];
inline int max(int x,int y) {
return x>y?x:y;
}
void push_up(int o, int l, int r) {
tr[o].xm = max(max(tr[lson].xm , tr[rson].xm ), tr[lson].rm + tr[rson].lm );
int mid = (l+r)>>1;
if(tr[lson].xm == mid-l+1) tr[o].lm = tr[lson].xm + tr[rson].lm ;
else tr[o].lm = tr[lson].lm ;
if(tr[rson].xm == r-mid) tr[o].rm = tr[rson].xm + tr[lson].rm ;
else tr[o].rm = tr[rson].rm ;
}
void build(int o, int l, int r) {
if(l == r) {
tr[o].lm = tr[o].rm = tr[o].xm = 1;
return ;
}
int mid = (l+r)>>1;
build(lson, l, mid);
build(rson, mid+1, r);
push_up(o, l, r);
}
void pushtag(int o) {
tr[o].lm = tr[o].rm = tr[o].xm = 0;
addv[o] = 1;
}
void push_down(int o, int l, int r) {
if(addv[o] == 0) return ;
int mid = (l+r)>>1;
tr[lson].lm = tr[lson].rm = tr[lson].xm = addv[o]>0 ? 0 : (mid-l+1); //-1为离开
tr[rson].lm = tr[rson].rm = tr[rson].xm = addv[o]>0 ? 0 : (r-mid); //1为坐下
addv[lson] = addv[rson] = addv[o];
addv[o] = 0;
return ;
}
void insert(int o, int l, int r, int a, int op) {//op=1表示前a个改为1,即前a个坐下, op=2表示后a个改为1
// printf("%d %d %d %d %d", o, l, r, a, op);
// printf("\n");
if(a == 0) return ;
if(r-l+1 == a) {pushtag(o); return ;}
int mid = (l+r)>>1;
push_down(o, l, r);
if(op == 1) {
if(tr[lson].xm >= a) insert(lson, l, mid, a, 1);
else if(tr[lson].rm + tr[rson].lm >= a) {
insert(rson, mid+1, r, a-tr[lson].rm , 1); //这个顺序太莫名其妙了....
insert(lson, l, mid+1, tr[lson].rm , 2);//因为是从前往后,所以lson肯定是坐下tr[lson].r个
}
else insert(rson, mid+1, r, a, 1);//
} else {
if(tr[rson].xm >= a) insert(rson, mid+1, r, a, 2);
else if(tr[lson].rm + tr[rson].lm >= a){
insert(lson, l, mid, a-tr[rson].lm , 2);
insert(rson, mid+1, r, tr[rson].lm , 1);
}
else insert(lson, l, mid, a, 2);
}
push_up(o, l, r);
}
void del(int o, int l, int r, int ql, int qr) {
if(qr<l||r<ql) return;
if(ql <= l && r <= qr) {
tr[o].lm = tr[o].rm = tr[o].xm = (r-l+1);
addv[o] = -1;
return ;
}
int mid = (l+r)>>1;
push_down(o, l, r);
del(lson, l, mid, ql, qr);
del(rson, mid+1, r, ql, qr);
push_up(o,l,r);
}
int main() {
scanf("%d%d",&n,&m);
build(1, 1, n);
// int tmp;
// scanf("%d",&tmp);
// printf("o : %d, %d", tmp, tr[tmp].lm);
char cmd;
while(m--) {
cin>>cmd;
if(cmd == 'A') {
int a;
scanf("%d",&a);
if(tr[1].xm < a) ans++;
else insert(1, 1, n, a, 1);
} else {
int x, y;
scanf("%d%d",&x, &y);
del(1, 1, n, x, y);
}
}
printf("%d",ans);
}
luoguP3071 [USACO13JAN]座位Seating的更多相关文章
- 洛谷 P3071 [USACO13JAN]座位Seating(线段树)
P3071 [USACO13JAN]座位Seating 题目链接 思路: 一开始把题给读错了浪费了好多时间呜呜呜. 因为第二个撤离操作是区间修改,所以我们可以想到用线段树来做.对于第一个操作,我们只需 ...
- 洛谷 P3071 [USACO13JAN]座位Seating-线段树区间合并(判断找,只需要最大前缀和最大后缀)+分治+贪心
P3071 [USACO13JAN]座位Seating 题目描述 To earn some extra money, the cows have opened a restaurant in thei ...
- [USACO13JAN] Seating
https://www.luogu.org/problem/show?pid=3071 题目描述 To earn some extra money, the cows have opened a re ...
- 【vijos】1286 座位安排(状压dp)
https://vijos.org/p/1286 这题好神不会捉... 首先我们知道所有情况有C(n*m, k)种,这个好搞.但是两两不相邻这个有点难搞.. 原来是状压dp..sigh. 设状态f[i ...
- 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 ...
- CSU 1997: Seating Arrangement【构造】
1997: Seating Arrangement Description Mr. Teacher老师班上一共有n个同学,编号为1到n. 在上课的时候Mr. Teacher要求同学们从左至右按1, 2 ...
- Seating Arrangement
1997: Seating Arrangement Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 543 Solved: ...
- 团体程序设计天梯赛-练习集L2-010. 排座位
L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...
- Co-prime Array&&Seating On Bus(两道水题)
Co-prime Array Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
随机推荐
- java直接存取MS Access的mdb数据库文件
jdbc 访问 access 的 mdb 数据库文件,使用一个叫ucanaccess的开发包实现这个功能. "Supported Access formats: 2000,2002/2003 ...
- Day4- Python基础4 深浅拷贝、三目运算、列表生成式,迭代器&生成器、装饰器
本节内容: 1.深浅拷贝 2.三目运算 3.迭代器和生成器 4.装饰器 1.深浅拷贝 拷贝意味着对数据重新复制一份,深浅拷贝的含义就是:对于修改复制的数据是否会影响到源数据,拷贝操作对于基本数据结构需 ...
- Hibernate 知识收纳.
一.乐观锁和悲观锁 悲观锁(Pessimistic Lock), 顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿 ...
- 用OC基于数组实现循环队列
一.简言 使用数组实现循环队列时,始终会有一个空余的位置预留着,作为一个判决队列已满的条件(当然也可以采用其他方式).在前面已经用C++介绍了基本的算法,可以去回顾一下https://www.cnbl ...
- css top,right,bottom,left设置为0有什么用?它和width:100%和height:100%有什么区别?
壹 ❀ 引 当我们使用position属性时,总免不了与top,left,right,bottom四个属性打交道,那么这四个属性都设置为0时有什么用,与宽高设置100%又有什么区别?本文对此展开讨论 ...
- 如何在本地开发Composer包
如何在本地开发Compoer包 周煦辰 2019-05-26 记录一下如何在本地开发一个Composer包,以及如何发布到Packgist. 假设你要开发一个名叫xuchen/biubiubiu的包. ...
- Flink中Idle停滞流机制(源码分析)
前几天在社区群上,有人问了一个问题 既然上游最小水印会决定窗口触发,那如果我上游其中一条流突然没有了数据,我的窗口还会继续触发吗? 看到这个问题,我蒙了???? 对哈,因为我是选择上游所有流中水印最小 ...
- [debug]ubuntu共享文件夹所在目录
使用Vmware虚拟机,Vmware Tools工具的复制粘贴一直无效,之后采用共享文件夹. 其默认的是在 \mnt\hgfs 下,在Vmware的设置中建立好文件夹,将文件传入进去,之后就可以去 \ ...
- Spring5源码解析1-从启动容器开始
从启动容器开始 最简单的启动spring的代码如下: @Configuration @ComponentScan public class AppConfig { } public class Mai ...
- Elasticsearch Query DSL 语言介绍
目录 0. 引言 1. 组合查询 2. 全文搜索 2.1 Match 2.2 Match Phase 2.3 Multi Match 2.4 Query String 2.5 Simple Query ...