D. Multiset(树状数组+二分)

int tree[N];
int n, q, x;
void add(int k, int num)  
{
	for (int i = k;i <= n; i += i & -i)
		tree[i] += num;
 
}
int read(int k)
{
	int sum = 0;
	for (int i = k;i > 0; i -= i & -i)
		sum += tree[i];
	return sum;
}
inline int in()
{
	char ch = getchar();
	int x = 0, f = 1;
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1;ch = getchar(); }
	while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0';ch = getchar(); }
	return x * f;
}
int main()
{
	while (cin >> n >> q)
	{
		f(i, 1, n) { x = in();add(x, 1); }
		f(i, 1, q)
		{
			x = in();
			if (x > 0)add(x, 1);
			else
			{
				int l = 1, r = n;x = -x;
				int ans = 2e9;
				while (l <= r)
				{
					int mid = (l + r) / 2;
					if (read(mid) >= x) { ans = min(ans, mid);r = mid - 1; }
					else l = mid + 1;
				}
				add(ans, -1);
			}
		}

			int l = 1, r = n;
			int ans = 2e9;
			while (l <= r)
			{
				int mid = (l + r) / 2;
				if (read(mid) >= 1) { ans = min(ans, mid);r = mid - 1; }
				else l = mid + 1;
			}
			if (ans == 2e9)ans = 0;
			cout << ans << endl;
		
	}
	return 0;
}

你可能感兴趣的:(Codeforces,pupil,高级数据结构)