Educational Codeforces Round 87 (Rated for Div. 2) D. Multiset 树状数组+二分

Note that the memory limit is unusual.

You are given a multiset consisting of n integers. You have to process queries of two types:

add integer k into the multiset;
find the k-th order statistics in the multiset and remove it.
k-th order statistics in the multiset is the k-th element in the sorted list of all elements of the multiset. For example, if the multiset contains elements 1, 4, 2, 1, 4, 5, 7, and k=3, then you have to find the 3-rd element in [1,1,2,4,4,5,7], which is 2. If you try to delete an element which occurs multiple times in the multiset, only one occurence is removed.

After processing all queries, print any number belonging to the multiset, or say that it is empty.

Input
The first line contains two integers n and q (1≤n,q≤106) — the number of elements in the initial multiset and the number of queries, respectively.

The second line contains n integers a1, a2, …, an (1≤a1≤a2≤⋯≤an≤n) — the elements of the multiset.

The third line contains q integers k1, k2, …, kq, each representing a query:

if 1≤ki≤n, then the i-th query is “insert ki into the multiset”;
if ki<0, then the i-th query is “remove the |ki|-th order statistics from the multiset”. For this query, it is guaranteed that |ki| is not greater than the size of the multiset.
Output
If the multiset is empty after all queries, print 0.

Otherwise, print any integer that belongs to the resulting multiset.

Examples
inputCopy
5 5
1 2 3 4 5
-1 -1 -1 -1 -1
outputCopy
0
inputCopy
5 4
1 2 3 4 5
-5 -1 -3 -1
outputCopy
3
inputCopy
6 2
1 1 1 2 3 4
5 6
outputCopy
6
Note
In the first example, all elements of the multiset are deleted.

In the second example, the elements 5, 1, 4, 2 are deleted (they are listed in chronological order of their removal).

In the third example, 6 is not the only answer.



题意

初始给定一个大小为 N N N的可重集合,两种操作

  • 插入一个数字
  • 删除一个数字

多次操作,最后如果没有元素了就输出 0 0 0,如果还有就输出任意一个元素
注意内存只能用28 M B ( 2 N , N < = 1 e 6 ) MB(大概是2N的大小,N<=1e6) MB(2N,N<=1e6)

首先推荐_heyuhhh_ 的B站讲题视频:https://www.bilibili.com/video/BV1A5411s7ta?p=4


备忘 : 树状数组的 q u e r y ( i ) query(i) query(i)可以求区间 [ 1 , i 1 ] [1,i-1] [1,i1]的前缀和

  • 内存较小,使用树状数组+二分
  • 树状数组第 i i i个位置存当前数字 i i i出现的次数
  • 查询 K K K小的值在树状数组上表示为 [ 1 , i d x ] [1,idx] [1,idx]的前缀和为 K K K,
    所以我们二分这个位置 i d x idx idx即可找到第 K K K小的值
//#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif


#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e6+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define QAQ (0)

using namespace std;

#ifdef debug
#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)
void err() { cout << "\033[39;0m" << endl; }
#endif

template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

#define lowbit(x) (x & (-x))
int n, m, Q, K, tree[MAXN], tot;

void update(int i, int val=1) {
	tot += val; //multiset里的元素个数+1
	while(i <= n) {
		tree[i] += val;
		i += lowbit(i);
	}
}

int query(int i) {
	int ret = 0;
	while(i > 0) {
		ret += tree[i];
		i -= lowbit(i);
	}
	return ret;
}

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	scanf("%d %d ", &n, &m);
	int x;
	for(int i=1; i<=n; i++) {
		scanf("%d ", &x); //我们并不需要把a[]数组存起来
		update(x);
	}
	while(m--) {
		scanf("%d ", &x);
		if(x < 0) { //delete
			x = -x;
			int lef = 1, rig = n, mid, idx = -1;
			//二分找K小值在树状数组里的位置
			while(lef <= rig) {
				mid = (lef + rig) >> 1;
				int tmp = query(mid);
				if(tmp < x) {
					lef = mid + 1;
				} else {
					idx = mid;
					rig = mid - 1;
				}
			}
			if(~idx) //找到以后更新
				update(idx, -1);
		} else { //insert
			update(x);
		}
	}
	if(tot <= 0) 
		printf("0\n");
	else {
		for(int i=1; i<=n; i++)
			if(tree[i]) {
				printf("%d\n", i);
				break;
			}
	}
#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}


全部评论

相关推荐

沉淀一会:1.同学你面试评价不错,概率很大,请耐心等待; 2.你的排名比较靠前,不要担心,耐心等待; 3.问题不大,正在审批,不要着急签其他公司,等等我们! 4.预计9月中下旬,安心过节; 5.下周会有结果,请耐心等待下; 6.可能国庆节前后,一有结果我马上通知你; 7.预计10月中旬,再坚持一下; 8.正在走流程,就这两天了; 9.同学,结果我也不知道,你如果查到了也告诉我一声; 10.同学你出线不明朗,建议签其他公司保底! 11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务