Weight line segment tree, each leaf node is regarded as the weight of the point, and it should be discrete when operating
You need to write a data structure (refer to the title of the topic) to maintain some numbers, which needs to provide the following operations:
1. Insert x number
2. Delete x number (if there are multiple identical numbers, only one is deleted)
3 . Query the rank of the number of x (if there are multiple identical numbers, because the smallest rank is output)
4. Query the number with the rank of x
5. Find the predecessor of x (the predecessor is defined as the number less than x and the largest)
6. Find Successor of x (successor is defined as the smallest number greater than x)
The first line is n, which represents the number of operations. The following n lines each have two numbers opt and x, and opt represents the sequence number of the operation (1<=opt<=6)
For operations 3, 4, 5, and 6, each line outputs a number, indicating the corresponding answer
Sample Input
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output
106465
84185
492737
Hint 1.
The data range of n: n<=100000
2. The data range of each number: [-2e9,2e9]
Code sample :
#define ll long long const ll maxn = 1e5+5; const ll mod = 1e9+7; const double eps = 1e-9; const double pi = acos(-1.0); const ll inf = 0x3f3f3f3f; #define lson k<<1 #define rson k<<1|1 struct pp { ll pt, x; } for [maxn]; ll s[maxn]; ll k = 1; struct node { ll l, r; ll num; // ge shu }t[maxn<<2]; void build(ll l, ll r, ll k){ t[k].l = l, t[k].r = r; t[k].num = 0; if (l == r) return; ll m = (l+r)>>1; build(l, m, lson); build(m+1, r, rson); } void pushup(ll k){ t[k]num = t[lson].num + t[rson]num; } void update(ll pos, ll pt, ll k){ if (t[k].l == t[k].r){ //prllf("l = %d r = %d pos = %d \n", t[k].l, t[k].r, pos); if (t[k].num + pt >= 0) t[k].num += pt; return; } ll m = (t[k].l+t[k].r)>>1; if (pos <= m) update(pos, pt, lson); else update(pos, pt, rson); pushup(k); } ll ans = 0; void query3(ll l, ll r, ll k){ if (l <= t[k].l && t[k].r <= r){ ans += t[k].num; return; } ll m = (t[k].l+t[k].r)>>1; if (l <= m) query3(l, r, lson); if (r > m) query3(l, r, rson); } void query4(ll x, ll k){ //printf("^^ %lld %lld \n", t[k].l, t[k].r); if (t[k].l == t[k].r){ ans = t[k].l; return; } ll m = (t[k].l+t[k].r)>>1; if (t[lson].num >= x) query4(x, lson); else query4(x-t[lson].num, rson); } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); ll n; cin >> n; for(ll i = 1; i <= n; i++){ scanf("%lld%lld", &pre[i].pt, &pre[i].x); if (pre[i].pt != 4) s[k++] = pre[i].x; } sort(s+1, s+k); k = unique(s+1, s+k)-s; //for(int i = 1; i < k; i++) printf("%lld ", s[i]); build(1, n, 1); for(ll i = 1; i <= n; i++){ ll x = lower_bound(s+1, s+k, pre[i].x)-s; //printf("____ %lld %lld\n", x, pre[i].x); years = 0; if (pre[i].pt == 1){ update(x, 1, 1); } else if (pre[i].pt == 2) { update(x, -1, 1); } else if (pre[i].pt == 3){ query3(1, x-1, 1); printf("%lld\n", ans+1); } else if (pre[i].pt == 4){ query4(pre[i].x, 1); printf("%lld\n", s[ans]); } else if (pre[i].pt == 5){ query3(1, x-1, 1); query4(years, 1); printf("%lld\n", s[ans]); } else { query3(1, x, 1); //printf("--- %d\n", ans); query4(years+1, 1); printf("%lld\n", s[ans]); } } return 0; }