pb_ds平衡树-模板

版权声明:本文为博主原创文章,转载请联系博主。 https://blog.csdn.net/everlasting_20141622/article/details/78600430
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>

#define pr pair<int,int>
#define inf 1000000000

using namespace std;
using namespace __gnu_pbds;

typedef tree<pr,null_type,less<pr>,rb_tree_tag,tree_order_statistics_node_update> rbtree;
typedef rbtree::iterator ITER;

char c[10];
rbtree q;
ITER r;
int tot=0;

int main(){
    int n,x;
    scanf("%d",&n);
    while(n--){
		scanf("%s%d",c,&x);
		switch(c[0]){
			case 'I':q.insert(pr(x,tot++));break;
			case 'D':r=q.lower_bound(pr(x,0)),q.erase(r);break;
			case 'M':
				if(c[1]=='I')r=q.begin(),printf("%d\n",(*r).first);
				else r=q.end(),r--,printf("%d\n",(*r).first);
				break;
			case 'P':
				r=q.lower_bound(pr(x,0));
				if(r==q.begin())puts("0");
				else r--,printf("%d\n",*r);
				break;
			case 'S':
				r=q.upper_bound(pr(x,inf));
				if(r==q.end())puts("0");
				else printf("%d\n",*r);
				break;
			case 'K':printf("%d\n",(*q.find_by_order(x-1)).first);break;
			case 'R':printf("%d\n",q.order_of_key(pr(x,0))+1);break;
		}
    }
    
    return 0;
}

展开阅读全文

没有更多推荐了,返回首页