萌新刚学OI求助fhq-treap
查看原帖
萌新刚学OI求助fhq-treap
253433
XeRnHe楼主2021/9/12 16:14

RT,全MLE,数组应该开不炸吧?

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

const int MAXN = 100005;

int n, tot1, tot2, root1, root2, cnt;
int size1[MAXN], size2[MAXN], son1[MAXN][2], son2[MAXN][2];
double val1[MAXN], val2[MAXN];
int pri1[MAXN], pri2[MAXN];
double num[MAXN];
int type[MAXN];
bool del[MAXN];

int makeNode1(double k) {
	tot1++;
	size1[tot1] = 1;
	val1[tot1] = k;
	pri1[tot1] = rand();
	return tot1; 
}

int makeNode2(double k) {
	tot2++;
	size2[tot2] = 1;
	val2[tot2] = k;
	pri2[tot2] = rand();
	return tot2;
}

inline void pushUp1(int p) {
	size1[p] = size1[son1[p][0]] + size1[son1[p][1]] + 1;
}

inline void pushUp2(int p) {
	size2[p] = size2[son2[p][0]] + size2[son2[p][1]] + 1;
}

void split1(int p, double k, int &x, int &y) {
	if (p == 0) {
		x = y = 0;
		return;
	}
	if (val1[p] < k) {
		x = p;
		split1(son1[p][1], k, son1[p][1], y);
	} else {
		y = p;
		split1(son1[p][0], k, x, son1[p][0]);
	}
	pushUp1(p);
}

void split2(int p, double k, int &x, int &y) {
	if (p == 0) {
		x = y = 0;
		return;
	}
	if (val2[p] > k) {
		x = p;
		split2(son2[p][1], k, son2[p][1], y);
	} else {
		y = p;
		split2(son2[p][0], k, x, son2[p][0]);
	}
	pushUp2(p);
}

int merge1(int x, int y) {
	if (!x || !y) {
		return x + y;
	}
	if (pri1[x] < pri1[y]) {
		son1[x][1] = merge1(son1[x][1], y);
		pushUp1(x);
		return x;
	} else {
		son1[y][0] = merge1(x, son1[y][0]);
		pushUp1(y);
		return y;
	}
}

int merge2(int x, int y) {
	if (!x || !y) {
		return x + y;
	}
	if (pri2[x] < pri2[y]) {
		son2[x][1] = merge2(son2[x][1], y);
		pushUp2(x);
		return x;
	} else {
		son2[y][0] = merge2(x, son2[y][0]);
		pushUp2(y);
		return y;
	}
}

void insert1(double k) {
	int x, y;
	split1(root1, k, x, y);
	root1 = merge1(merge1(x, makeNode1(k)), y);
}

void insert2(double k) {
	int x, y;
	split2(root2, k, x, y);
	root2 = merge2(merge2(x, makeNode2(k)), y);
}

void delete1(double k) {
	int x, y, z;
	split1(root1, k, x, z);
	split1(x, k + 1e-7, x, y);
	y = merge1(son1[y][0], son1[y][1]);
	root1 = merge1(merge1(x, y), z);
}

void delete2(double k) {
	int x, y, z;
	split2(root2, k, x, z);
	split2(x, k - 1e-7, x, y);
	y = merge2(son2[y][0], son2[y][1]);
	root2 = merge2(merge2(x, y), z);
}

int query1(double k) {
	int x, y;
	split1(root1, k, x, y);
	int result = size1[x];
	root1 = merge1(x, y);
	return result;
}

int query2(double k) {
	int x, y;
	split2(root2, k, x, y);
	int result = size2[x];
	root2 = merge2(x, y);
	return result;
}

int main() {
	srand(time(0));
	int n, plu = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		char opt[MAXN];
		scanf("%s", opt);
		if (opt[0] == 'A') {
			cnt++;
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			b = c - b;
			if (a > 0) {
				num[cnt] = b * 1.0 / a;
				insert1(num[cnt]);
				type[cnt] = 1;
			} else if (a < 0) {
				num[cnt] = b * 1.0 / a;
				insert2(num[cnt]);
				type[cnt] = 2;
			} else {
				type[cnt] = 3;
				if (b < 0) {
					plu++;
					num[cnt] = 1;
				} else {
					num[cnt] = 0;
				}
			}
		} else if (opt[0] == 'D') {
			int x;
			scanf("%d", &x);
			if (del[x]) {
				continue;
			}
			del[x] = true;
			if (type[x] == 1) {
				delete1(num[x]);
			} else if (type[x] == 2) {
				delete2(num[x]);
			} else {
				plu -= num[x];
			}
		} else {
			int x;
			scanf("%d", &x);
			printf("%d\n", plu + query1(x) + query2(x) - 1);
		}
	}
	return 0;
}
2021/9/12 16:14
加载中...