Splay TLE一堆 求调
查看原帖
Splay TLE一堆 求调
790188
bsdsdb楼主2024/7/25 11:53
#include <bits/stdc++.h>
typedef int ll;
using namespace std;

const ll MAXN = 1.1e6;

ll read() {
	ll r = 0;
	char c;
	c = getchar();
	while (c < '0' || c > '9') {
		c = getchar();
	}
	while ('0' <= c && c <= '9') {
		r = r * 10 + c - '0';
		c = getchar();
	}
	return r;
}

struct SplayNode {
	ll val, l, r, p, sz;
	SplayNode() {
		val = l = r = p = sz = 0;
	}
	SplayNode(ll v) {
		val = v;
		l = r = p = 0;
		sz = 1;
	}
};
struct Splay {
	ll size, rt, opn;
	SplayNode node[MAXN + 5];
	void pushup(ll x) {
		node[0] = SplayNode();
		node[x].sz = node[node[x].l].sz + 1 + node[node[x].r].sz;
	}
	bool isl(ll x) {
		return node[node[x].p].l == x;
	}
	void rotate(ll x) {
		ll p = node[x].p;
		ll g = node[p].p;
		if (isl(x)) {
			node[p].l = node[x].r;
			node[node[x].r].p = p;
			node[x].r = p;
			node[p].p = x;
		} else {
			node[p].r = node[x].l;
			node[node[x].l].p = p;
			node[x].l = p;
			node[p].p = x;
		}
		pushup(p);
		pushup(x);
		node[x].p = g;
		if (g) {
			if (node[g].l == p) {
				node[g].l = x;
			} else {
				node[g].r = x;
			}
		}
	}
	void splay(ll x) {
		while (node[x].p != 0 && node[node[x].p].p != 0) {
			ll p = node[x].p;
			ll g = node[p].p;
			if (isl(x) ^ isl(p)) {
				rotate(x);
				rotate(x);
			} else {
				rotate(p);
				rotate(x);
			}
		}
		if (node[x].p != 0) {
			rotate(x);
		}
		rt = x;
	}
	void insert(ll& pos, ll v, ll pr) {
		if (pos == 0) {
			node[pos = ++size] = SplayNode(v);
			node[pos].p = pr;
			opn = pos;
			return;
		}
		if (v <= node[pos].val) {
			insert(node[pos].l, v, pos);
		} else {
			insert(node[pos].r, v, pos);
		}
		pushup(pos);
	}
	void del(ll v) {
		ll pos = rt;
		while (node[pos].val != v) {
			if (v < node[pos].val) {
				pos = node[pos].l;
			} else {
				pos = node[pos].r;
			}
		}
		ll p = node[pos].p, L = node[pos].l, R = node[pos].r;
		node[pos] = SplayNode();
		node[L].p = node[R].p = 0;
		ll lmx = L;
		while (node[lmx].r) {
			lmx = node[lmx].r;
		}
		splay(lmx);
		node[lmx].r = R;
		pushup(lmx);
		node[R].p = lmx;
		node[lmx].p = p;
		if (!p) {
			rt = lmx;
		}
		opn = 0;
	}
	ll fnd(ll& pos, ll x) {
		if (pos == 0) {
			return 0;
		}
		if (x == node[pos].val) {
			return pos;
		} else if (x < node[pos].val) {
			return fnd(node[pos].l, x);
		} else {
			return fnd(node[pos].r, x);
		}
	}
	ll fndrk(ll& pos, ll x) {
		if (pos == 0) {
			return 0;
		}
		opn = pos;
		if (node[pos].val <= x) {
			return node[node[pos].l].sz + 1 + fndrk(node[pos].r, x);
		} else {
			return fndrk(node[pos].l, x);
		}
	}
	ll fndval(ll& pos, ll x) {
		if (x <= node[node[pos].l].sz) {
			return fndval(node[pos].l, x);
		} else if (x > node[node[pos].l].sz + 1) {
			return fndval(node[pos].r, x - node[node[pos].l].sz - 1);
		} else {
			opn = pos;
			return node[pos].val;
		}
	}
	ll pre(ll x) {
		ll lx = fndrk(rt, x - 1);
		splay(opn);
		return fndval(rt, lx);
	}
	ll suf(ll x) {
		ll lex = fndrk(rt, x);
		splay(opn);
		return fndval(rt, lex + 1);
	}
	void deb() {
		cerr << "rt=" << rt << endl;
		for (ll i = 0; i <= size; i++) {
			cerr << i << " val=" << node[i].val << " l=" << node[i].l << " r=" << node[i].r << " p=" << node[i].p << " sz=" << node[i].sz << endl;
		}
	}
} tr;

ll n, q, last = 0, ans = 0;

int main() {
	n = read();
	q = read();
	while (n--) {
		ll tmp;
		tmp = read();
		tr.insert(tr.rt, tmp, 0);
	}
	while (q--) {
		ll o, x;
		o = read();
		x = read();
		x ^= last;
		if (o == 1) {
			tr.insert(tr.rt, x, 0);
		} else if (o == 2) {
			tr.del(x);
		} else if (o == 3) {
			last = tr.fndrk(tr.rt, x - 1) + 1;
			ans ^= last;
		} else if (o == 4) {
			last = tr.fndval(tr.rt, x);
			ans ^= last;
		} else if (o == 5) {
			last = tr.pre(x);
			ans ^= last;
		} else {
			last = tr.suf(x);
			ans ^= last;
		}
		if (tr.opn) {
			tr.splay(tr.opn);
		}
	}
	cout << ans << endl;
	return 0;
}
2024/7/25 11:53
加载中...