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. 基于SVM的字母验证码识别

    基于SVM的字母验证码识别 摘要 本文研究的问题是包含数字和字母的字符验证码的识别.我们采用的是传统的字符分割识别方法,首先将图像中的字符分割出来,然后再对单字符进行识别.首先通过图像的初步去噪.滤波 ...

  2. python中列表的insert和append的效率对比

    python中insert和append方法都可以向列表中插入数据只不过append默认插入列表的末尾,insert可以指定位置插入元素. 我们来测试一下他俩插入数据的效率: 测试同时对一个列表进行插 ...

  3. 机器学习1-sklearn&字典特征抽取

    sklearn数据集 数据集API介绍 sklearn.datasets 加载获取流行数据集 datasets.load_*() 获取小规模数据集,数据包含在datasets里 datasets.fe ...

  4. Error: Could not request certificate: No route to host - connect(2)

    [root@puppetclient ~]# puppet agent --server 192.168.127.137 --testInfo: Creating a new SSL key for ...

  5. 【VNC】vnc远程连接的时候无法显示图像已解决

    介绍一个 VNC连接工具:iis7服务器管理工具 IIs7服务器管理工具可以批量连接并管理VNC服务器 作为服务器集成管理器,它最优秀的功能就是批量管理windows与linux系统服务器.vps.能 ...

  6. kubernets之headless

    一  认识headless服务 1服务以及服务的作用相信大家都已经耳熟能详了吗,服务接受请求,并且随机的将请求转发到相关联的任一pod来处理请求,但是考虑另外一种场景, 如果有客户端需要知道这个服务关 ...

  7. python无法连接mysql,有个小坑,需要注意一下!

    尝试用python链接mysql数据库,按照教程里链接了小半夜,就是没有链接上,找了各种文章,最后发现是版本的问题,我想使用的模块是MySQLdb,刚刚去他们的官网看了一下,最近一次更新是2014年, ...

  8. 1.8V转3V,1,8V转3.3V电源芯片的规格书参数

    1.8V电平如何稳压稳定输出3V或者3.3V,就需要用到1.8V转3V,1,8V转3.3V电源芯片,就PW5100(低功耗,外围简单),PW5200A是可调输出电压,可以输出电压根据外围电阻来设置命令 ...

  9. 【Redis系列】Spring boot实现监听Redis key失效事件

    talk is cheap, show me the code. 一.开启Redis key过期提醒 方式二:修改配置文件 redis.conf # 默认 notify-keyspace-events ...

  10. 将连续增长 N 次字符串所需的内存重分配次数从必定 N 次降低为最多 N 次 二进制安全

    SDS 与 C 字符串的区别 - Redis 设计与实现 http://redisbook.com/preview/sds/different_between_sds_and_c_string.htm ...