#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5, lim = 1e7 + 5;
int ls[N << 5], rs[N << 5], sum[N << 5];
int rt, cnt;
void upd (int &p, int l, int r, int k, int v) {
if (!p) p = ++ cnt;
sum[p] += v;
if (l == r) return;
int mid = l + r >> 1;
if (k <= mid) upd(ls[p], l, mid, k, v);
else upd(rs[p], mid + 1, r, k, v);
}
int rnk (int p, int l, int r, int k) {
if (l == r) return 0;
int mid = l + r >> 1;
if (k <= mid) return rnk(ls[p], l, mid, k);
else return sum[ls[p]] + rnk(rs[p], mid + 1, r, k);
}
int kth (int p, int l, int r, int k) {
if (l == r) return l;
int mid = l + r >> 1;
if (k <= sum[ls[p]]) return kth(ls[p], l, mid, k);
else return kth(rs[p], mid + 1, r, k - sum[ls[p]]);
}
int n, t, x, m;
signed main () {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
// freopen("set.in", "r", stdin);
// freopen("set.out", "w", stdout);
cin >> n >> m;
for (int i = 1 ; i <= n ; i ++) {
int x;
cin >> x;
upd (rt, -lim, lim, x, 1);
}
while (m --) {
int k;
cin >> k;
if (k > 0) upd (rt, -lim, lim, k, 1);
else {
k = -k;
upd (rt, -lim, lim, kth(rt, -lim, lim, k), -1);
}
}
int ans = kth (rt, -lim, lim, 1);
if (ans > 1e6) cout << 0;
else cout << ans;
return 0;
}
是二叉搜索树吗?
是内存炸了
T4卡空间,只有28MB
开小一点有80