救!!!全MLE
查看原帖
救!!!全MLE
988362
National_Studying楼主2024/1/18 14:48
#include <bits/stdc++.h>
typedef int intt;
#define int long long

using namespace std;

const int M = 2e6 + 10;
int cnt, root;
char str[M] = {0};

struct Node {
	int fa, ls, rs, size;
	char val;
} t[M];

void Update(int u) {
	t[u].size = t[t[u].ls].size + t[t[u].rs].size + 1;
}

int build(int L, int R, int f) {
	if (L > R) {
		return 0;
	}
	int mid = (L + R) >> 1;
	int cur = ++cnt;
	t[cur].fa = f;
	t[cur].val = str[mid];
	t[cur].ls = build(L, mid - 1, cur);
	t[cur].rs = build(mid + 1, R, cur);
	Update(cur);
	return cur;
}

int get(int x) {
	return t[t[x].fa].rs == x;
}

void rotate(int x) {
	int f = t[x].fa, g = t[f].fa, son = get(x);
	if (son == 1) {
		t[f].rs = t[x].ls;
		if (t[f].rs) {
			t[t[f].rs].fa = f;
		}
	} else {
		t[f].ls = t[x].rs;
		if (t[f].ls) {
			t[t[f].ls].fa = f;
		}
	}
	t[f].fa = x;
	if (son == 1) {
		t[x].ls = f;
	} else {
		t[x].rs = f;
	}
	t[x].fa = g;
	if (g) {
		if (t[g].rs == f) {
			t[g].rs = x;
		} else {
			t[g].ls = x;
		}
	}
	Update(f);
	Update(x);
}

void Splay(int x, int goal) {
	if (goal == 0) {
		root = x;
	}
	while (1) {
		int f = t[x].fa, g = t[f].fa;
		if (f == goal) {
			break;
		}
		if (g != goal) {
			if (get(x) == get(f)) {
				rotate(f);
			} else {
				rotate(x);
			}
		}
		rotate(x);
	}
	Update(x);
}

int kth(int k, int u) {
	if (k == t[t[u].ls].size + 1) {
		return u;
	}
	if (k <= t[t[u].ls].size) {
		return kth(k, t[u].ls);
	}
	if (k >= t[t[u].ls].size + 1) {
		return kth(k - t[t[u].ls].size - 1, t[u].rs);
	}
}

void Insert(int L, int len) {
	int x = kth(L, root), y = kth(L + 1, root);
	Splay(x, 0);
	Splay(y, x);
	t[y].ls = build(1, len, y);
	Update(y);
	Update(x);
}

void del(int L, int R) {
	int x = kth(L, root), y = kth(R + 1, root);
	Splay(x, 0);
	Splay(y, x);
	t[y].ls = 0;
	Update(y);
	Update(x);
}

void inorder(int u) {
	if (u == 0) {
		return ;
	}
	inorder(t[u].ls);
	cout << t[u].val;
	inorder(t[u].rs);
}

intt main() {
	ios::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);

	t[1].size = 2;
	t[1].ls = 2;
	t[2].size = 1;
	t[2].fa = 1;
	root = 1;
	cnt = 2;
	int pos = 1;
	int n;
	cin >> n;
	while (n--) {
		int len;
		char opt[10];
		cin >> opt;
		if (opt[0] == 'I') {
			cin >> len;
			for (int i = 1; i <= len; i++) {
				char ch = getchar();
				while (ch < 32 || ch > 126) {
					ch = getchar();
				}
				str[i] = ch;
			}
			Insert(pos, len);
		}
		if (opt[0] == 'D') {
			cin >> len;
			del(pos, pos + len);
		}
		if (opt[0] == 'G') {
			cin >> len;
			int x = kth(pos, root), y = kth(pos + len + 1, root);
			Splay(x, 0);
			Splay(y, x);
			inorder(t[y].ls);
			cout << "\n";
		}
		if (opt[0] == 'M') {
			cin >> len;
			pos = len + 1;
		}
		if (opt[0] == 'P') {
			pos--;
		}
		if (opt[0] == 'N') {
			pos++;
		}
	}
	return 0;
}
2024/1/18 14:48
加载中...