【LOJ】#3094. 「BJOI2019」删数
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」删数的更多相关文章
- LOJ 3094 「BJOI2019」删数——角标偏移的线段树
题目:https://loj.ac/problem/3094 弱化版是 AGC017C . 用线段树维护那个题里的序列即可. 对应关系大概是: 真实值的范围是 [ 1-m , n+m ] :考虑设偏移 ...
- Loj #3089. 「BJOI2019」奥术神杖
Loj #3089. 「BJOI2019」奥术神杖 题目描述 Bezorath 大陆抵抗地灾军团入侵的战争进入了僵持的阶段,世世代代生活在 Bezorath 这片大陆的精灵们开始寻找远古时代诸神遗留的 ...
- Loj #3093. 「BJOI2019」光线
Loj #3093. 「BJOI2019」光线 题目描述 当一束光打到一层玻璃上时,有一定比例的光会穿过这层玻璃,一定比例的光会被反射回去,剩下的光被玻璃吸收. 设对于任意 \(x\),有 \(x\t ...
- LOJ 3090 「BJOI2019」勘破神机——斯特林数+递推式求通项+扩域
题目:https://loj.ac/problem/3090 题解:https://www.luogu.org/blog/rqy/solution-p5320 1.用斯特林数把下降幂化为普通的幂次求和 ...
- loj 3090 「BJOI2019」勘破神机 - 数学
题目传送门 传送门 题目大意 设$F_{n}$表示用$1\times 2$的骨牌填$2\times n$的网格的方案数,设$G_{n}$$表示用$1\times 2$的骨牌填$3\times n$的网 ...
- LOJ 3089 「BJOI2019」奥术神杖——AC自动机DP+0/1分数规划
题目:https://loj.ac/problem/3089 没想到把根号之类的求对数变成算数平均值.写了个只能得15分的暴力. #include<cstdio> #include< ...
- LOJ 3093 「BJOI2019」光线——数学+思路
题目:https://loj.ac/problem/3093 考虑经过种种反射,最终射下去的光线总和.往下的光线就是这个总和 * a[ i ] . 比如只有两层的话,设射到第二层的光线是 lst ,那 ...
- LOJ 3092 「BJOI2019」排兵布阵 ——DP
题目:https://loj.ac/problem/3092 同一个人的不同城堡之间没有什么联系,只是和<=m.所以对每个城堡的 s 个值排序,做一个 f[ i ][ j ] 表示第 i 个城堡 ...
- @loj - 2174@ 「FJOI2016」神秘数
目录 @description@ @solution@ @accepted code@ @details@ @description@ 一个可重复数字集合 S 的神秘数定义为最小的不能被 S 的子集的 ...
随机推荐
- Linux shell -"a-d"命令
shell中条件判断if中的-z到-d的意思 分类:shellLinux (2006) (0) shell中条件判断if中的-z到-d的意思 [ -a FILE ] 如果 FILE 存在则为真. ...
- [Luogu] 相关分析
不想调了 #include <bits/stdc++.h> ; #define LL long long #define gc getchar() int fjs; struct Node ...
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6455 Accepted: 1392 ...
- 图论小专题B
2 树 2.1 树的定义 一个只有\(N-1\)条边,且任意两个点连通的图叫做树.通过这样定义的树往往是一棵无根树,而我们通常会任意选定一个根节点使其变成有根树.有根树可以定义"父亲和儿子& ...
- 一次router拦截器的应用
实现 退出登陆 无法回退到其它页面 当有登陆状态时 可以拿其它页面的地址直接访问 若没有登陆状态 拿其它页面的地址直接访问 会报错 router.beforeEach((to, from, n ...
- [python]@cached_property缓存装饰器
cached_property缓存装饰器 class cached_property(object): """ Decorator that converts a met ...
- MySQL之MyISAM和InnoDB
一.区别 1.MySQL默认采用的是MyISAM. 2.MyISAM不支持事务和外键,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提 ...
- NUnit -- Test discovery or execution might not work for this project
[7/31/2019 2:06:58.100 PM Warning] No test matches the given testcase filter `FullyQualifiedName=Sil ...
- Android APP切换到后台接收不到推送消息
1. Android端进程被杀死后,目前自带的保护后台接收消息活跃机制.暂时没有什么好的机制保持任何情况下都活跃 android原生系统用home键杀进程可以起来,如果是强行停止就只能用户自己手动 ...
- linux下如何基于已有容器创建image并运行?
1. 通过docker ps命令先找到容器id,示例如下,123456789012就是我们要找的 jello@~$ docker ps CONTAINER ID IMAGE COMMAND CREAT ...