传送门(权限题)


3050: [Usaco2013 Jan]Seating

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 132 Solved: 76
[Submit][Status][Discuss]
Description

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.

m(m<=300,000)个操作。操作分2种:
1.A p,表示把编号最小的空着的长度为p的区间图上颜色。
2.L a b,表示把从a到b的区间(包括端点)全部擦干净(没颜色还是没颜色)。
Q:有多少个操作1不能实现?

Input

  • 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).

Output

  • Line 1: The number of parties that are turned away.

Sample Input

10 4

A 6

L 2 4

A 5

A 2

INPUT DETAILS: 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.

Sample Output

1

OUTPUT DETAILS: Party #3 is turned away. All other parties are seated.

HINT

样例解释:

首先将1~6图上颜色,再把2~4擦掉,这时不存在长度为5的空着的区间,该操作不能实现,跳过。最后把2~3图上颜色。所以不能实现的操作1有一个,即第三个操作。


写在前面:当时测试的时候被翻译坑了TAT,题意“找出所有可放的空闲中最左端的,没有可放的就ans++”理解成“从最左端空闲放,不能放就ans++”
思路:线段树吗,要记录的东西可能有点奇怪,是左、右端点各自所在的最大空闲ls,rs与整个区间内的最大空闲sum
对于查询操作,能不能放取决于[1,n]区间的最大空闲是否够,如果够就向下找,只有三种情况
1.左区间可以放,这时移到左节点继续找
2.1不满足时看看是不是能够放在中间,就是左边放一些右边放一些(仍是连续的),这就取决与左节点的右端最大空闲和右节点的左端最大空闲总和是否可以符合要求
3.1,2都不满足,移到右节点找
对于修改操作,我们往上返回左右节点信息时要注意我们要将父节点的三个信息改成什么,是如何由子节点的信息决定的,这里就不详细说明,留给大家自己思考,如果想不通可以看代码
注意:区间修改加lazy标记,注意lazy标记要分辨“未加过标记”“区间为0”“区间为1”
代码:

#include<bits/stdc++.h>
using namespace std;
int n,m,ans,p,x,y;
char ch;
struct os
{int ls,rs,sum,lazy;
}tree[2000010];
void pushdown(int now,int begin,int end)
{if (tree[now].lazy==0||begin==end) return;int mid=(begin+end)>>1;if (tree[now].lazy==1)tree[now<<1|1]=tree[now<<1]=(os){0,0,0,1};elsetree[now<<1]=(os){mid-begin+1,mid-begin+1,mid-begin+1,-1},tree[now<<1|1]=(os){end-mid,end-mid,end-mid,-1};tree[now].lazy=0;
}
void pushup(int now,int begin,int end)
{int mid=(begin+end)>>1;tree[now].sum=max(max(tree[now<<1].rs+tree[now<<1|1].ls,tree[now<<1].sum),tree[now<<1|1].sum);tree[now].ls=tree[now<<1].ls;if (tree[now<<1].ls==mid-begin+1) tree[now].ls+=tree[now<<1|1].ls;tree[now].rs=tree[now<<1|1].rs;if (tree[now<<1|1].rs==end-mid) tree[now].rs+=tree[now<<1].rs;
}
void build(int now,int begin,int end)
{tree[now]=(os){end-begin+1,end-begin+1,end-begin+1,0};if (begin==end) return;int mid=(begin+end)>>1;build(now<<1,begin,mid);build(now<<1|1,mid+1,end);
}
int find(int now,int begin,int end,int need)
{if (begin==end) return end;int mid=(begin+end)>>1;if (tree[now<<1].sum>=need) return find(now<<1,begin,mid,need);else if (tree[now<<1].rs+tree[now<<1|1].ls>=need) return mid+1-tree[now<<1].rs;else return find(now<<1|1,mid+1,end,need);
}
void update(int now,int begin,int end,int l,int r,int num)
{if (l<=begin&&end<=r){if (!num) tree[now]=(os){end-begin+1,end-begin+1,end-begin+1,-1};else tree[now]=(os){0,0,0,1};return;}pushdown(now,begin,end);int mid=(begin+end)>>1;if (mid>=l) update(now<<1,begin,mid,l,r,num);if (mid<r) update(now<<1|1,mid+1,end,l,r,num);pushup(now,begin,end);
}
main()
{scanf("%d%d",&n,&m);build(1,1,n);for (int i=1;i<=m;i++){ch=getchar();while (ch!='A'&&ch!='L') ch=getchar();if (ch=='A'){scanf("%d",&p);if (tree[1].sum<p){ans++;continue;}x=find(1,1,n,p);update(1,1,n,x,x+p-1,1);}else scanf("%d%d",&x,&y),update(1,1,n,x,y,0);}printf("%d",ans);
}

【BZOJ3050】Seating,线段树相关推荐

  1. 【codevs2421】【BZOJ1858】序列操作,线段树

    传送门1 传送门2 写在前面:垃圾题目毁我青春 思路: 这是一道很明显但你就是要调很久而且不会舒服的线段树 每个区间你需要维护7个量和2个标记 分别是1的总数,最长连续1(0)长度,左(右)端点的连续 ...

  2. 二逼平衡树——树套树(线段树套Splay平衡树)

    题面 Bzoj3196 解析 线段树和Splay两棵树套在一起,常数直逼inf,但最终侥幸过了 思路还是比较简单, 在原数组维护一个下标线段树,再在每一个线段树节点,维护一个对应区间的权值Splay. ...

  3. 线段树——HDU - 1698

    题目含义 就是初始化一堆数为1 可以经过操作把一个区间的数都改变 并求这堆数的总大小 题目分析 有一个 #include<iostream> #include<stdio.h> ...

  4. BZOJ.1558.[JSOI2009]等差数列(线段树 差分)

    BZOJ 洛谷 首先可以把原序列\(A_i\)转化成差分序列\(B_i\)去做. 这样对于区间加一个等差数列\((l,r,a_0,d)\),就可以转化为\(B_{l-1}\)+=\(a_0\),\(B ...

  5. 【线段树分治 线性基】luoguP3733 [HAOI2017]八纵八横

    不知道为什么bzoj没有HAOI2017 题目描述 Anihc国有n个城市,这n个城市从1~n编号,1号城市为首都.城市间初始时有m条高速公路,每条高速公路都有一个非负整数的经济影响因子,每条高速公路 ...

  6. [bzoj1582][Usaco2009 Hol]Holiday Painting 节日画画_线段树

    Holiday Painting 节日画画 bzoj-1582 Usaco-2009 Hol 题目大意:给定两个n*m的01网格图.q次操作,每次将第二个网格图的子矩阵全部变成0或1,问每一次操作后两 ...

  7. codefores 786B. Legacy(最短路,线段树优化拆点,好题)

    题目链接 B. Legacy time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...

  8. 【题解】BZOJ 3065: 带插入区间K小值——替罪羊树套线段树

    题目传送门 题解 orz vfk的题解 3065: 带插入区间K小值 系列题解 一 二 三 四 惨 一开始用了一种空间常数很大的方法,每次重构的时候merge两颗线段树,然后无限RE(其实是MLE). ...

  9. 树链剖分+线段树 HDOJ 4897 Little Devil I(小恶魔)

    题目链接 题意: 给定一棵树,每条边有黑白两种颜色,初始都是白色,现在有三种操作: 1 u v:u到v路径(最短)上的边都取成相反的颜色 2 u v:u到v路径上相邻的边都取成相反的颜色(相邻即仅有一 ...

最新文章

  1. volatile - 如何实现线程安全
  2. 【数据库复习】第一章绪论
  3. Java之线程同步练习
  4. qt运行C语言后无显示,qt designer启动后不显示界面问题的原因与解决办法-站长资讯中心...
  5. python中config命令_【Python】 配置解析ConfigParser 命令行参数解析optparser
  6. java1.6升级1.7_jdk从1.6升级到1.7可能会遇到的Version问题
  7. python中对象和类的关系_Python面向对象之类与类之间的关系
  8. php模拟登陆正方教务系统csdn,curl模拟登陆正方教务系统查成绩,出现Object moved to here,已登陆首页获取cookie...
  9. ActivityMQ介绍与使用
  10. iMX8MM u-boot2021.04移植
  11. R语言实现地理探测器的流程及代码
  12. 对任意合式公式求真值表以及主析取范式和主合取范式(JAVA)
  13. 关于NMDS的一知半解
  14. 《云计算技术与应用基础》课程标准
  15. Word 利用 VBA 批量设置图片格式
  16. python ttk.notebook_python – 删除Ttk Notebook标签虚线
  17. XRecycleView (Scrapped or attached views may not be recycled)
  18. 计算机网络术语总结1
  19. c语言求最小公倍数_最小公倍数
  20. LikeLib,这才是真实的百万级别TPS

热门文章

  1. 大前端的自动化工厂(1)——Yeoman
  2. jwt php tp5,TP5框架中使用JWT的方法示例
  3. php一定要用phpstudy,用phpstudy有什么好处
  4. 实用的powershell小技巧,持续更新……
  5. 【汇总】numpy函数合集
  6. 【Paper-Attack Defense】Adversarial Label-Flipping Attack and Defense for Graph Neural Networks
  7. Markdown编辑器基本语法
  8. 删除顽固文件夹cygwin的方法,挺折腾的
  9. Sublime Text 2安装插件的方法
  10. ajax velocity,velocity 使用js