跳到正文

个人记录

d

Xudongning约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年11月18日
收录
2026年11月18日
更新
2026年11月18日
篇幅
约 7 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 3e5 + 5;
class Splay_tree {
public:
	int ch[N][2], fa[N], cnt[N], siz[N], num, root;
	long double v[N];
	inline void rotate(int x) {
		int y = fa[x], z = fa[y], k = (ch[y][1] == x);
		ch[z][ch[z][1] == y] = x;
		fa[x] = z;
		ch[y][k] = ch[x][k ^ 1];
		fa[ch[x][k ^ 1]] = y;
		ch[x][k ^ 1] = y;
		fa[y] = x;
		siz[y] = siz[ch[y][0]] + siz[ch[y][1]] + cnt[y];
		siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + cnt[x];
	}
	inline void splay(int x, int pos) {
		while (fa[x] != pos) {
			int y = fa[x];
			if (fa[y] != pos) (ch[fa[x]][1] == x) == (ch[fa[y]][1] == y) ? rotate(y) : rotate(x);
			rotate(x);
		}
		if (!pos) root = x;
	}
	inline void insert(long double x) {
		int cur = root, p = 0;
		while (cur && v[cur] != x) p = cur, cur = ch[cur][x > v[cur]];
		if (cur) ++cnt[cur];
		else {
			cur = ++num;
			if (p) ch[p][x > v[p]] = cur;
			ch[cur][0] = ch[cur][1] = 0, fa[cur] = p, v[cur] = x, cnt[cur] = 1, siz[cur] = 1;
		}
		splay(cur, 0);
	}
	inline void find(long double x) {
		int cur = root;
		while (ch[cur][x > v[cur]] && x - v[cur]) cur = ch[cur][x > v[cur]];
		splay(cur, 0);
	}
	inline int kth(int k) {
		int cur = root;
		if (!k) return 0;
		while (true) {
			if (ch[cur][0] && k <= siz[ch[cur][0]]) cur = ch[cur][0];
			else if (k > siz[ch[cur][0]] + cnt[cur]) k -= siz[ch[cur][0]] + cnt[cur], cur = ch[cur][1];
			else {
				splay(cur, 0);
				return cur;
			}
		}
	}
	inline int pre(long double x) {
		find(x);
		if (v[root] < x) return root;
		int cur = ch[root][0];
		while (ch[cur][1]) cur = ch[cur][1];
		return cur;
	}
	inline int suc(long double x) {
		find(x);
		if (v[root] > x) return root;
		int cur = ch[root][1];
		while (ch[cur][0]) cur = ch[cur][0];
		return cur;
	}
	inline void remove(long double x) {
		int lst = pre(x), nxt = suc(x);
		splay(lst, 0);
		splay(nxt, lst);
		if (cnt[ch[nxt][0]] > 1) --cnt[ch[nxt][0]], splay(ch[nxt][0], 0);
		else ch[nxt][0] = 0;
	}
} t[2];
int m;
pair<long double, int> q[N];
bitset<N> flag;
int cnt, tot, add;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> m;
    t[0].insert(-1e10);
    t[0].insert(1e10);
	t[1].insert(-1e10);
    t[1].insert(1e10);
    for (int i = 1; i <= m; ++i) {
		string op;
        cin >> op;
        if (op[0] == 'A') {
			int x, y, z;
			cin >> x >> y >> z;
			if (x == 0) {
				if (y > z) ++add;
				q[++cnt] = {(y > z), 2};
			}
			else if (x > 0) {
				t[0].insert(1.0L * (z - y) / x);
				q[++cnt] = {1.0L * (z - y) / x, 0};
				++tot;
			}
			else {
				t[1].insert(1.0L * (z - y) / x);
				q[++cnt] = {1.0L * (z - y) / x, 1};
			}
		}
        else if (op[0] == 'D') {
			int x;
			cin >> x;
			if (x > cnt) continue;
			if (flag[x]) continue;
			if (q[x].second == 2) {
				add -= q[x].first;
			}
			else if (q[x].second == 0) {
				t[0].remove(q[x].first);
				--tot;
			}
			else {
				t[1].remove(q[x].first);
			}
			flag[x] = true;
		}
        else {
			long double x;
			cin >> x;
			t[0].insert(x);
			t[0].find(x);
			cout << t[0].v[t[0].kth(t[0].siz[t[0].ch[t[0].root][0]])] << '\n';
			int res0 = tot - (t[0].siz[t[0].ch[t[0].root][0]] - 1);
			t[0].remove(x);

			t[1].insert(x);
			t[1].find(x);
			int res1 = t[1].siz[t[1].ch[t[1].root][0]];
			t[1].remove(x);

			int ans = res0 + res1 + add;
			cout << tot << ' ' << res0 << ' ' << res1 << ' ' << add << '\n';
		}
		//cout << fixed << setprecision(2) << t[0].v[t[0].kth(1 + 1)] << ' ' << t[1].v[t[1].kth(1 + 1)] << '\n';
    }
	for (int i = 1; i <= cnt; ++i) {
		cout << q[i].first << ' ' << q[i].second << '\n';
	}
    return 0;
}
原文