Educational Codeforces Round 87 (Rated for Div. 2) D
题解
树状数组维护 1 ~ i 在队列中的数量的前缀和
二分查找位置进行增改
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef double db;
const int N = 1048576 + 5;
int n, m, _, k;
int c[N];
void add(int x, int k)
{
for (; x <= N; x += -x & x) c[x] += k;
}
int ask(int x)
{
int ans = 0;
for (; x; x -= -x & x) ans += c[x];
return ans;
}
int bfind(int t)
{
int l = 1, r = N;
bool flag = 0;
while (l < r)
{
int m = (l + r) >> 1;
if (ask(m) >= t) r = m, flag = 1;
else l = m + 1;
}
if (flag) return l;
return 0;
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
rep (i, 1, n) cin >> k, add(k, 1);
rep (i, 1, m)
{
cin >> k;
if (k > 0) { add(k, 1); continue; }
add(bfind(-k), -1);
}
cout << bfind(1);
return 0;
}