社区讨论

怎么会TLE?帮忙康康怎么优化?

P3369【模板】普通平衡树参与者 4已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@loditko3
此快照首次捕获于
2023/10/31 07:18
2 年前
此快照最后确认于
2023/11/06 22:34
2 年前
查看原帖
rt
multiset打的,超时了#8 #9 #10 #12
CPP
#include <bits/stdc++.h>
using namespace std;
multiset<int> q;
int n;

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	while(n -- ) {
		int opt, x;
		cin >> opt >> x;
		if(opt == 1) {
			q.insert(x);
		}
		else if(opt == 2) {
			multiset<int>::iterator it = q.lower_bound(x);
			q.erase(it);
		}
		else if(opt == 3) {
			int num = 0;
			multiset<int>::iterator it = q.lower_bound(x);
			for (multiset<int>::iterator i = q.begin(); i != it; ++ i, ++ num);
			cout << num + 1 << endl;
		}
		else if(opt == 4) {
			int num = 1;
			for (multiset<int>::iterator i = q.begin(); i != q.end(); ++ i, ++ num)
				if(num == x) {
					cout << *i << endl;
					break;
				}
		}
		else if(opt == 5) {
			multiset<int>::iterator it = q.lower_bound(x);
			-- it;
			cout << *it << endl;
		}
		else {
			multiset<int>::iterator it = q.upper_bound(x);
			cout << *it << endl;
		}
	}
	return 0;
}

回复

5 条回复,欢迎继续交流。

正在加载回复...