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的可重集合,两种操作

  • 插入一个数字
  • 删除一个第 K K K 大数字

多次操作,最后如果没有元素了就输出 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值