Sample Input

5 4

1 2 3 4 5

-5 -1 -3 -1

Sample Output

3


思路,首先发现a[i]的值的范围是在1~n之间,每次插入我们可以直接把cnt[a[i]]++

删除的时候,要把所有在后面的数往前挪一位,那么其实也相当于所有在后面的数的前面

的cnt的前缀和减去1,于是利用树状数组动态维护前缀和,利用前缀和来找到删除的元素

时间复杂度O(nlogn)


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<LL,LL>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()
const LL N=1e6+10;
int bit[N];
int lowbit(int x){
return x&-x;
}
void update(int index){
while(index<N){
bit[index]++;
index +=lowbit(index);
}
}
void del(int index){
while(index<N){
bit[index]--;
index +=lowbit(index);
}
}
int query(int index){ //查询前缀和
int ans=0;
while(index>0){
ans += bit[index];
index -= lowbit(index);
}
return ans;
}
int main(int argc, char const *argv[])
{
// #define DEBUG
#ifdef DEBUG
freopen("1.dat","r",stdin);
freopen("ans.dat","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
int t;
rep(i,0,n){
cin>>t;
update(t);
}
rep(i,0,m){
cin>>t;
if(t>0) update(t);
else{
t=-t;
int left=1,right=n;
int ans=0,mid=n;
while(left<=right){
mid=(left+right)>>1;
if(query(mid)>=t){
ans=mid;
right=mid-1;
}
else left=mid+1;
}
del(ans);
}
}
rep(i,1,n+1)
if(query(i)>0){
cout<<i<<endl;
return 0;
}
cout<<0<<endl;
return 0;
}

Educational Codeforces Round 87 (Rated for Div. 2) D树状数组加二分删除的值的更多相关文章

  1. Educational Codeforces Round 87 (Rated for Div. 2)

    比赛链接:https://codeforces.com/contest/1354 A - Alarm Clock 题意 一个人要睡够 $a$ 分钟,一开始睡 $b$ 分钟后闹钟响铃,之后每次设置 $c ...

  2. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

  3. Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)

    题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n  n个数,然后求有多少个区间[l,r] 满足    a[l]+a[r]=max([l, ...

  4. Educational Codeforces Round 88 (Rated for Div. 2) C. Mixing Water(数学/二分)

    题目链接:https://codeforces.com/contest/1359/problem/C 题意 热水温度为 $h$,冷水温度为 $c\ (c < h)$,依次轮流取等杯的热冷水,问二 ...

  5. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot(二分或者尺取)

    题目哦 题意:给出一个序列,序列有四个字母组成,U:y+1,D:y-1 , L:x-1 , R:x+1;   这是规则 . 给出(x,y) 问可不可以经过最小的变化这个序列可以由(0,0) 变到(x, ...

  6. Educational Codeforces Round 77 (Rated for Div. 2) - D. A Game with Traps(二分)

    题意:$m$个士兵,每个士兵都有一个灵敏度$a[i]$,起点为$0$,终点为$n + 1$,在路上有$k$个陷阱,每个陷阱有三个属性$l[i],r[i],d[i]$,$l[i]$表示陷阱的位置,如果你 ...

  7. Educational Codeforces Round 74 (Rated for Div. 2)E(状压DP,降低一个m复杂度做法含有集合思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[100005];int pos[ ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

随机推荐

  1. 【MyBatis】自定义 MyBatis

    自定义 MyBatis 文章源码 执行查询信息的分析 我们知道,MyBatis 在使用代理 DAO 的方式实现增删改查时只做两件事: 创建代理对象 在代理对象中调用 selectList() 配置信息 ...

  2. 【SpringMVC】SpringMVC 拦截器

    SpringMVC 拦截器 文章源码 拦截器的作用 SpringMVC 的处理器拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理. 谈到拦截器,还有另外一个概 ...

  3. pip不是内部或外部命令解决方法

    问题 已经配置好Python环境,但是安装依赖时,出现pip不是内部或外部命令. 解决方法 找到pip.exe文件所在的目录,将所在路径配置到环境变量path中. 再次输入pip

  4. 【Problems】MySQL5.7 datetime 默认值设为‘0000-00-00 00:00:00'值出错

    记录 MySQL5.7 datetime 默认值设为'0000-00-00 00:00:00'值出错 我的MySQL版本 mysql --version 5.7.28 C:\Users\x1c> ...

  5. Kafka 探险 - 生产者源码分析: 核心组件

    这个 Kafka 的专题,我会从系统整体架构,设计到代码落地.和大家一起杠源码,学技巧,涨知识.希望大家持续关注一起见证成长! 我相信:技术的道路,十年如一日!十年磨一剑! 往期文章 Kafka 探险 ...

  6. SVM 支持向量机算法-原理篇

    公号:码农充电站pro 主页:https://codeshellme.github.io 本篇来介绍SVM 算法,它的英文全称是 Support Vector Machine,中文翻译为支持向量机. ...

  7. SAPCAR使用说明

    1.首先看一下SAPCAR的功能usage:create a new archive:SAPCAR -c[vir][f archive] [-P] [-C directory]   [-A filen ...

  8. webapi Swagger 配置 services.BuildServiceProvider() 报警 ASP0000 问题处理

    问题起源 网上的常见配置 Swagger 配置 在Startup类的 ConfigureServices 使用 services.BuildServiceProvider() ,其中有段代码如下: v ...

  9. DNS是如何工作的?

    今天很多人都在讲域名系统和互联网作为一个整体是如何工作的,域名系统---也就是大家所熟知的DNS.不幸的是,对于天龙人和普通人来说,他们并不了解DNS到底是什么鬼.今天就来聊聊DNS,和那些想了解DN ...

  10. GStreamer各个包构建

    GStreamer按功能.维护的标准化程度.依赖库的版权差异等分了若干个包(package),如 gstreamer, gst-plugins-base, gst-plugins-good, gst- ...