LOJ#3094. 「BJOI2019」删数

之前做atcoder做到过这个结论结果我忘了。。。

em,就是\([1,n]\)之间每个数\(i\),然后\([i - cnt[i] + 1,i]\)可以放一条线段,没被线段放的地方就是需要改的数的总和

之后我们线段树维护区间最小值以及个数

我们要注意如果+1后使得一个本来在\([1,N]\)的点越出了范围,那么就要把这个区间给删掉,-1同理,要加进来

值域开成\(N + 2M\)也就是\(4.5*10^{5}\)即可

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 500005
#define ba 47
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MAXV = 150000,LEN = 450000;
int N,M;
int a[MAXN],d;
int cnt[LEN + 5];
int getpos(int x) {
return x - d + MAXV;
}
struct node {
int l,r,val,cnt,lz;
}tr[LEN * 4 + 5];
void update(int u) {
tr[u].val = min(tr[u << 1].val,tr[u << 1 | 1].val);
tr[u].cnt = 0;
if(tr[u].val == tr[u << 1].val) tr[u].cnt += tr[u << 1].cnt;
if(tr[u].val == tr[u << 1 | 1].val) tr[u].cnt += tr[u << 1 | 1].cnt;
}
void build(int u,int l,int r) {
tr[u].l = l;tr[u].r = r;
if(l == r) {tr[u].cnt = 1;return;}
int mid = (l + r) >> 1;
build(u << 1,l,mid);
build(u << 1 | 1,mid + 1,r);
update(u);
}
void addlz(int u,int v) {
tr[u].val += v;tr[u].lz += v;
}
void pushdown(int u) {
if(tr[u].lz) {
addlz(u << 1,tr[u].lz);
addlz(u << 1 | 1,tr[u].lz);
tr[u].lz = 0;
}
}
void add(int u,int l,int r,int v) {
if(tr[u].l == l && tr[u].r == r) {
addlz(u,v);
return;
}
pushdown(u);
int mid = (tr[u].l + tr[u].r) >> 1;
if(r <= mid) add(u << 1,l,r,v);
else if(l > mid) add(u << 1 | 1,l,r,v);
else {add(u << 1,l,mid,v);add(u << 1 | 1,mid + 1,r,v);}
update(u);
}
pii Query(int u,int l,int r) {
if(tr[u].l == l && tr[u].r == r) return mp(tr[u].val,tr[u].cnt);
pushdown(u);
int mid = (tr[u].l + tr[u].r) >> 1;
if(r <= mid) return Query(u << 1,l,r);
else if(l > mid) return Query(u << 1 | 1,l,r);
else {
pii a = Query(u << 1,l,mid),b = Query(u << 1 | 1,mid + 1,r);
if(a.fi > b.fi) swap(a,b);
if(a.fi == b.fi) a.se += b.se;
return a;
}
}
void Solve() {
read(N);read(M);
build(1,1,LEN);
for(int i = 1 ; i <= N ; ++i) {
read(a[i]);
a[i] += MAXV;
add(1,a[i] - cnt[a[i]],a[i] - cnt[a[i]],1);
cnt[a[i]]++;
}
int p,x;
for(int i = 1 ; i <= M ; ++i) {
read(p);read(x);
if(p == 0) {
if(x == 1) {
if(cnt[getpos(N)]) {add(1,getpos(N) - cnt[getpos(N)] + 1,getpos(N),-1);}
}
else {
if(cnt[getpos(N + 1)]) {add(1,getpos(N + 1) - cnt[getpos(N + 1)] + 1,getpos(N + 1),1);}
}
d += x;
}
else {
if(a[p] <= getpos(N)) {
add(1,a[p] - cnt[a[p]] + 1,a[p] - cnt[a[p]] + 1,-1);
}
cnt[a[p]]--;
a[p] = x - d + MAXV;
if(a[p] <= getpos(N)) {
add(1,a[p] - cnt[a[p]],a[p] - cnt[a[p]],1);
}
cnt[a[p]]++;
}
pii res = Query(1,getpos(1),getpos(N));
int ans = 0;
if(res.fi == 0) ans = res.se;
out(ans);enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

【LOJ】#3094. 「BJOI2019」删数的更多相关文章

  1. LOJ 3094 「BJOI2019」删数——角标偏移的线段树

    题目:https://loj.ac/problem/3094 弱化版是 AGC017C . 用线段树维护那个题里的序列即可. 对应关系大概是: 真实值的范围是 [ 1-m , n+m ] :考虑设偏移 ...

  2. Loj #3089. 「BJOI2019」奥术神杖

    Loj #3089. 「BJOI2019」奥术神杖 题目描述 Bezorath 大陆抵抗地灾军团入侵的战争进入了僵持的阶段,世世代代生活在 Bezorath 这片大陆的精灵们开始寻找远古时代诸神遗留的 ...

  3. Loj #3093. 「BJOI2019」光线

    Loj #3093. 「BJOI2019」光线 题目描述 当一束光打到一层玻璃上时,有一定比例的光会穿过这层玻璃,一定比例的光会被反射回去,剩下的光被玻璃吸收. 设对于任意 \(x\),有 \(x\t ...

  4. LOJ 3090 「BJOI2019」勘破神机——斯特林数+递推式求通项+扩域

    题目:https://loj.ac/problem/3090 题解:https://www.luogu.org/blog/rqy/solution-p5320 1.用斯特林数把下降幂化为普通的幂次求和 ...

  5. loj 3090 「BJOI2019」勘破神机 - 数学

    题目传送门 传送门 题目大意 设$F_{n}$表示用$1\times 2$的骨牌填$2\times n$的网格的方案数,设$G_{n}$$表示用$1\times 2$的骨牌填$3\times n$的网 ...

  6. LOJ 3089 「BJOI2019」奥术神杖——AC自动机DP+0/1分数规划

    题目:https://loj.ac/problem/3089 没想到把根号之类的求对数变成算数平均值.写了个只能得15分的暴力. #include<cstdio> #include< ...

  7. LOJ 3093 「BJOI2019」光线——数学+思路

    题目:https://loj.ac/problem/3093 考虑经过种种反射,最终射下去的光线总和.往下的光线就是这个总和 * a[ i ] . 比如只有两层的话,设射到第二层的光线是 lst ,那 ...

  8. LOJ 3092 「BJOI2019」排兵布阵 ——DP

    题目:https://loj.ac/problem/3092 同一个人的不同城堡之间没有什么联系,只是和<=m.所以对每个城堡的 s 个值排序,做一个 f[ i ][ j ] 表示第 i 个城堡 ...

  9. @loj - 2174@ 「FJOI2016」神秘数

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 一个可重复数字集合 S 的神秘数定义为最小的不能被 S 的子集的 ...

随机推荐

  1. java new一个对象的过程中发生了什么

    java在new一个对象的时候,会先查看对象所属的类有没有被加载到内存,如果没有的话,就会先通过类的全限定名来加载.加载并初始化类完成后,再进行对象的创建工作. 我们先假设是第一次使用该类,这样的话n ...

  2. 【CUDA 基础】6.5 流回调

    title: [CUDA 基础]6.5 流回调 categories: - CUDA - Freshman tags: - 流回调 toc: true date: 2018-06-20 21:56:1 ...

  3. MySQL 5.7半同步复制技术

    一.复制架构衍生史 在谈这个特性之前,我们先来看看MySQL的复制架构衍生史. 在2000年,MySQL 3.23.15版本引入了Replication.Replication作为一种准实时同步方式, ...

  4. openstack导入镜像

    本文以制作CentOS7.2镜像为例,详细介绍手动制作OpenStack镜像详细步骤,解释每一步这么做的原因.镜像上传到OpenStack glance,支持以下几个功能: 支持密码注入功能(nova ...

  5. maven上传jar包(oracle jdbc驱动)

    由于Oracle授权问题,Maven3不提供Oracle JDBC driver,为了在Maven项目中应用Oracle JDBC driver,必须手动添加到本地仓库.一.首先要得到Oracle J ...

  6. 01 关于jupyter的环境安装

      jupyter notebook环境安装 一.什么是Jupyter Notebook? 1. 简介 Jupyter Notebook是基于网页的用于交互计算的应用程序.其可被应用于全过程计算:开发 ...

  7. linux下查看tomcat的日志

    工作期间有碰到服务器日志相关的,需要看tomcat运行日志,简单搜了下,摘为随笔,以供参考 一种是利用docker查看 1.使用dockerdocker logs -f -t --since=&quo ...

  8. javascript实现集合Set、字典Dictionary、HashTable

    集合是由一组无序且唯一(即不能重复)的项组成的.这个数据结构使用了与有限集合相同的数学概念,但应用在计算机科学的数据结构中. function Set() { this.items = {}; } S ...

  9. 反射中的 Method 的 getReadMethod 与 getWriteMethod 使用 【获取一个对象的所有属性字段名称和其对应的值】

    转: class反射(一),以及Method 的 getReadMethod 与 getWriteMethod 使用 2018年11月28日 17:27:42 zich77521 阅读数 788   ...

  10. 数据中心网络架构的问题与演进 — 混合云与 VPC 专有网络

    目录 文章目录 目录 前文列表 历史背景 混合云 Why hybrid cloud? 混合云市场 混合云的逻辑架构 混合云应用场景 灾难恢复 数据备份 负载扩容 应用部署 开发测试生产部署 混合云产品 ...