Wa on #5-#13
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5, maxm = 0;
int n, m;
ll d[maxn << 2];
int tp[maxn], num[maxn], x[maxn];
void Pushup(const int &p) {
d[p] = d[p << 1] + d[p << 1 | 1];
}
void Update(const int &x, const int &s = 1, const int &t = *num, const int &p = 1) {
if (s == t) {
++d[p];
return;
}
int mid = s + ((t - s) >> 1);
x <= mid ? Update(x, s, mid, p << 1) : Update(x, mid + 1, t, p << 1 | 1);
Pushup(p);
}
void Erase(const int &x, const int &s = 1, const int &t = *num, const int &p = 1) {
if (s == t) {
--d[p];
return;
}
int mid = s + ((t - s) >> 1);
x <= mid ? Update(x, s, mid, p << 1) : Update(x, mid + 1, t, p << 1 | 1);
Pushup(p);
}
ll Count_one(const int &x, const int &s = 1, const int &t = *num, const int &p = 1) {
if (s == t) return d[p];
int mid = s + ((t - s) >> 1);
return x <= mid ? Count_one(x, s, mid, p << 1) : Count_one(x, mid + 1, t, p << 1 | 1);
}
ll Count_more(const int &l, const int &r, const int &s = 1, const int &t = *num, const int &p = 1) {
if (l <= s && t <= r) return d[p];
int mid = s + ((t - s) >> 1), ans = 0;
if (l <= mid) ans = Count_more(l, r, s, mid, p << 1);
if (r > mid) ans += Count_more(l, r, mid + 1, t, p << 1 | 1);
return ans;
}
ll Get_kth_largest(const int &k, const int &s = 1, const int &t = *num, const int &p = 1) {
if (s == t) return s;
int mid = s + ((t - s) >> 1), r = d[p << 1 | 1];
return k > r ? Get_kth_largest(k - r, s, mid, p << 1) : Get_kth_largest(k, mid + 1, t, p << 1 | 1);
}
ll Get_kth_smallest(const int &k, const int &s = 1, const int &t = *num, const int &p = 1) {
if (s == t) return s;
int mid = s + ((t - s) >> 1), l = d[p << 1];
return k <= l ? Get_kth_smallest(k, s, mid, p << 1) : Get_kth_smallest(k - l, mid + 1, t, p << 1 | 1);
}
ll Get_x_rank_small(const int &x, const int &s = 1, const int &t = *num, const int &p = 1) {
if (s == t) return 1;
int mid = s + ((t - s) >> 1);
return x <= mid ? Get_x_rank_small(x, s, mid, p << 1) : d[p << 1] + Get_x_rank_small(x, mid + 1, t, p << 1 | 1);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", tp + i, x + i);
if (tp[i] != 4) num[++*num] = x[i];
}
sort(num + 1, num + *num + 1), *num = unique(num + 1, num + *num + 1) - num - 1;
for (int i = 1; i <= n; ++i) {
if (tp[i] != 4)
x[i] = lower_bound(num + 1, num + *num + 1, x[i]) - num;
switch (tp[i]) {
case 1:
Update(x[i]);
break;
case 2:
Erase(x[i]);
break;
case 3:
printf("%lld", Get_x_rank_small(x[i]));
putchar(10);
break;
case 4:
printf("%d", num[Get_kth_smallest(x[i])]);
putchar(10);
break;
case 5:
printf("%d", num[Get_kth_smallest(Get_x_rank_small(x[i]) - 1)]);
putchar(10);
break;
case 6:
printf("%d", num[Get_kth_smallest(Get_x_rank_small(x[i] + 1))]);
putchar(10);
break;
}
}
return 0;
}