[Luogu5324][BJOI2019]删数(线段树)
CF风格题,先猜结论,记数列中i这个数共出现了cnt[i]次,那么所有区间[i-cnt[i]+1,i]的并集的补集大小就是答案。
于是我们只需要线段树维护每个位置是否被某个区间覆盖到即可,对于整体加减操作,设一个偏移量即可。
#include<cstdio>
#include<algorithm>
#define ls (x<<1)
#define rs (ls|1)
#define lson ls,L,mid
#define rson rs,mid+1,R
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
using namespace std; const int N=;
int n,m,mx,py,L,p,x,a[N],num[N],v[N<<],c[N<<],mn[N<<],tag[N<<]; void upd(int x){
if (mn[ls]<mn[rs]) mn[x]=mn[ls],c[x]=c[ls];
else if (mn[ls]>mn[rs]) mn[x]=mn[rs],c[x]=c[rs];
else mn[x]=mn[ls],c[x]=c[ls]+c[rs];
if (!mn[x]) v[x]=c[x]; else v[x]=;
} void put(int x,int k){
mn[x]+=k; tag[x]+=k;
if (!mn[x]) v[x]=c[x]; else v[x]=;
} void push(int x){
if (!tag[x]) return;
put(ls,tag[x]); put(rs,tag[x]); tag[x]=;
} void build(int x,int L,int R){
if (L==R){ c[x]=v[x]=; return; }
int mid=(L+R)>>;
build(lson); build(rson); upd(x);
} void mdf(int x,int L,int R,int l,int r,int k){
if (L==l && r==R){ put(x,k); return; }
int mid=(L+R)>>; push(x);
if (r<=mid) mdf(lson,l,r,k);
else if (l>mid) mdf(rson,l,r,k);
else mdf(lson,l,mid,k),mdf(rson,mid+,r,k);
upd(x);
} int que(int x,int L,int R,int l,int r){
if (L==l && r==R) return v[x];
int mid=(L+R)>>; push(x);
if (r<=mid) return que(lson,l,r);
else if (l>mid) return que(rson,l,r);
else return que(lson,l,mid)+que(rson,mid+,r);
} void Mdf(int x,int w){
int k=num[py+x]+(w>); num[py+x]+=w;
if (x<=n) mdf(,,mx,py+x-k+,py+x-k+,w);
} int main(){
freopen("number.in","r",stdin);
freopen("number.out","w",stdout);
scanf("%d%d",&n,&m); mx=(n+m)*+; py=n+m; build(,,mx);
rep(i,,n) scanf("%d",&a[i]),Mdf(a[i],);
while (m--){
scanf("%d%d",&p,&x);
if (p>) Mdf(a[p]+L,-),a[p]=x-L,Mdf(a[p]+L,);
else if (x>){
py--; L++; int pos=py+n+;
if (num[pos]>) mdf(,,mx,pos-num[pos]+,pos,-);
}else{
int pos=py+n+; py++; L--;
if (num[pos]>) mdf(,,mx,pos-num[pos]+,pos,);
}
printf("%d\n",que(,,mx,py+,py+n));
}
return ;
}
[Luogu5324][BJOI2019]删数(线段树)的更多相关文章
- Luogu5324 BJOI2019删数(线段树)
考虑无修改怎么做.对于1~n的每个数,若其存在,将最后一个放在其值的位置,剩余在其前面依次排列,答案即为值域1~n上没有数的位置个数.带修改显然记一下偏移量线段树改一改就好了. #include< ...
- 【BJOI2019】删数 线段树
题目大意:一个数列若能在有限次数内删空,则称这个数列可以删空,一次删除操作定义如下: 记当前数列长度为$k$,则删掉数列中所有等于$k$的数. 现在有一个长度为$n$的数列$a$,有$m$次修改操作, ...
- [BJOI2019]删数(线段树)
[BJOI2019]删数(线段树) 题面 洛谷 题解 按照值域我们把每个数的出现次数画成一根根的柱子,然后把柱子向左推导,\([1,n]\)中未被覆盖的区间长度就是答案. 于是问题变成了单点修改值,即 ...
- [BJOI2019] 删数 [dp转贪心结论+线段树]
题面 传送门 思路 dp部分 以下称合法序列为原题面中可以删空的序列 这个是我在模拟考场上的思路 一开始我是觉得,这个首先可以写成一个dp的形式:$dp[i][j]$表示用$j$个数字填满了目标序列的 ...
- luogu P5324 [BJOI2019]删数
传送门 不如先考虑暴力,能删的序列首先有\(1,2,3...n\),还有就是升序排序后从后往前放数,第\(i\)位要么放\(i\),要么放\(i+1\)位置的数,例如\(1,2,4,4,5,6,9,9 ...
- 题解 洛谷 P5324 【[BJOI2019]删数】
先考虑对于一个序列,能使其可以删空的的修改次数. 首先可以发现,序列的排列顺序是没有影响的,所以可以将所有数放到桶里来处理. 尝试对一个没有经过修改的可以删空的序列来进行删数,一开始删去所有的\(n\ ...
- Problem 1007 幸运数 线段树成段更新
题目链接: 题目 Problem 1007 幸运数 Time Limit: 2000 mSec Memory Limit : 131072 KB 问题描述 皮特的幸运数是2和5.只由幸运数字2和5组成 ...
- [BJOI2019] 删数
https://www.luogu.org/problemnew/show/P5324 题解 首先我们需要弄清这个答案是什么. 对于一个长度为n的序列,那么它先删的肯定是\(n\),删完之后它就会跳到 ...
- hdu 4417 区间内比h小的数 线段树
题意求区间内比h小的数的个数 将所有的询问离线读入之后,按H从小到大排序.然后对于所有的结点也按从小到大排序,然后根据查询的H,将比H小的点加入到线段树,然后就是一个区间和. 2015-07-27:专 ...
随机推荐
- node依赖包格式区别
UMD:UMD 版本可以通过 <script> 标签直接用在浏览器中.jsDelivr CDN 的 https://cdn.jsdelivr.net/npm/vue 默认文件就是运行时 + ...
- Mybatis-Generator开发教程
转载:https://blog.csdn.net/qqyb2000/article/details/80031559 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyB ...
- MySQL两地三中心方案初步设计【转】
整体内容会按照如下的方式来进行设计: 首先说下方案的背景,我参考了一些资料(参见附件). 方案背景 随着互联网业务快速发展,多IDC的业务支撑能力和要求也逐步提升,行业内的“两地三中心”方案较为流行. ...
- TP5单元测试
tp5版本: 5.0.24 单元测试版本:1.* 1. 安装单元测试扩展: composer require topthink/think-testing .* 2.安装完毕,运行 php think ...
- tansition-group 使用方法
<transition-group name="breadcrumb"> <el-breadcrumb-item v-for="(item,index) ...
- linux下终端字体彩色显示
linux下python彩色显示 跨平台彩色显示库https://pypi.python.org/pypi/colorama jlive@MacBook-Pro:py_demo $pytho ...
- 约翰·麦斯威尔 | John C. Maxwell | A leader is one who knows the way, goes the way, and shows the way.
约翰·麦斯威尔_百度百科https://baike.baidu.com/item/%E7%BA%A6%E7%BF%B0%C2%B7%E9%BA%A6%E6%96%AF%E5%A8%81%E5%B0%9 ...
- Python去除文件中的空格、Tab键和回车
def stripFile(oldFile, newFile): '''remove the space or Tab or enter in a file, and output a new fil ...
- sqllite 学习-1
C# SQLite 数据库操作学习: https://www.cnblogs.com/leemano/p/6578050.html SQLite 之 C#版 System.Data.SQLite 使用 ...
- flutter中的listview的使用
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends Statele ...