跳到正文

剪贴板

剪贴板 1cuak3fj

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<vector>
#include<random>
using namespace std;
template<typename INT>
class List{
	#define Pair pair<Node*,Node*>
	struct Node{
		int key;
		int rnk,lazy;
		INT val;
		Node *fa;
		Node *lc,*rc;
		int size;
		Node(int p,INT x){
			key=rand();
			rnk=p; val=x; lazy=0;
			fa=lc=rc=nullptr; size=1;
		}
		void push_down(){
			rnk+=lazy;
			if(lc!=nullptr) lc->lazy+=lazy;
			if(rc!=nullptr) rc->lazy+=lazy;
			lazy=0;
		}
		void update(){
			size=(lc==nullptr?0:lc->size)+(rc==nullptr?0:rc->size)+1;
		}
		Node* Plus(){
			Node* t=this;
			t->push_down();
			if(t->rc==nullptr){
				while(t->fa!=nullptr&&t->fa->rc==t) t=t->fa;
				t=t->fa;
				return t;
			}else{
				t=t->rc; t->push_down();
				while(t->lc!=nullptr) t=t->lc,t->push_down();
				return t;
			}
		}
		Node* Minus(){
			Node* t=this;
			t->push_down();
			if(t->lc==nullptr){
				while(t->fa!=nullptr&&t->fa->lc==t) t=t->fa;
				t=t->fa;
				return t;
			}else{
				t=t->lc; t->push_down();
				while(t->rc!=nullptr) t=t->rc,t->push_down();
				return t;
			}
		}
	};
	int Size;
	Node* root;
	Node* create(int p,INT x){
		Node* ans=new Node(p,x);
		return ans;
	}
	Pair split(int x,Node* t){
		if(t==nullptr) return {nullptr,nullptr};
		t->push_down();
		if(t->rnk<=x){
			Pair tmp=split(x,t->rc);
			t->rc=tmp.first;
			if(tmp.first!=nullptr) tmp.first->fa=t;
			t->update(); return {t,tmp.second};
		}else{
			Pair tmp=split(x,t->lc);
			t->lc=tmp.second;
			if(tmp.second!=nullptr) tmp.second->fa=t;
			t->update(); return {tmp.first,t};
		}
	}
	Node* merge(Node* t1,Node* t2){
		if(t1==nullptr) return t2;
		if(t2==nullptr) return t1;
		t1->push_down(); t2->push_down();
		if(t1->rnk>t2->rnk){
			swap(t1,t2);
		}
		if(t1->key<t2->key){
			t1->rc=merge(t1->rc,t2);
			if(t1->rc!=nullptr) t1->rc->fa=t1;
			t1->update(); return t1;
		}else{
			t2->lc=merge(t2->lc,t1);
			if(t2->lc!=nullptr) t2->lc->fa=t2;
			t2->update(); return t2;
		}
	}
	Node* rnk(int x){
		Node* tmp=root;
		while(tmp!=nullptr){
			tmp->push_down();
			if(tmp->rnk==x) return tmp;
			if(tmp->rnk>x) tmp=tmp->lc;
			else tmp=tmp->rc;
		}
		return nullptr;
	}
	void release(Node*& t){
		if(t==nullptr) return;
		release(t->lc); release(t->rc); delete t;
	}
	public:
	class iterator{
		Node* ptr;
		public:
		iterator(Node* p=nullptr){
			ptr=p;
		}
		void operator=(const iterator &it){
			ptr=it.ptr;
		}
		void operator=(Node* t){
			ptr=t;
		}
		bool operator!=(const iterator &it){
			return ptr!=it.ptr;
		}
		bool operator==(const iterator &it){
			return ptr==it.ptr;
		}
		iterator& operator++(){
			ptr=(*ptr).Plus(); return *this;
		}
		iterator operator++(int){
			iterator tmp=*this; ++tmp;
			*this=tmp; return tmp;
		}
		iterator& operator--(){
			ptr=(*ptr).Minus(); return *this;
		}
		iterator operator--(int){
			iterator tmp=*this; --tmp;
			*this=tmp; return tmp;
		}
		int rnk(){
			return ptr->rnk;
		}
		INT& val(){
			return ptr->val;
		}
	};
	iterator find(int x){
		return iterator(rnk(x));
	}
	iterator begin(){
		return find(0);
	}
	iterator end(){
		return nullptr;
	}
	int size(){
		return Size;
	}
	bool empty(){
		return Size==0;
	}

	INT& operator[](int x){
		return rnk(x)->val;
	}
	iterator ins(int p,int q,INT x){
		iterator ans;
		Size+=(q-p+1);
		Node *tr=nullptr,*s=nullptr,*lst=nullptr;
		for(int i=p;i<=q;i++){
			Node* tmp=create(i,x); if(i==p) ans=tmp;
			while(s!=nullptr&&s->key>tmp->key) lst=s,s=s->fa;
			if(lst!=nullptr) tmp->lc=lst,lst->fa=tmp;
			if(s==nullptr) tr=tmp;
			else s->rc=tmp,tmp->fa=s;
			s=tmp; lst=nullptr;
		}
		Pair tmp=split(p-1,root); if(tmp.second!=nullptr) tmp.second->lazy+=(q-p+1);
		root=merge(merge(tmp.first,tr),tmp.second);
		return ans;
	}
	iterator ins(int p,INT x){
		return ins(p,p,x);
	}
	iterator del(int p,int q){
		Size-=(q-p+1);
		Pair tmp1=split(q,root);
		Pair tmp2=split(p-1,tmp1.first);
		Node *Lroot=tmp2.first,*Rroot=tmp1.second;
		if(Rroot!=nullptr) Rroot->lazy-=(q-p+1);
		root=merge(Lroot,Rroot);
		release(tmp2.second);
		return find(p);
	}
	iterator del(int p){
		return del(p,p);
	}
	iterator del(iterator it){
		return del(it.rnk());
	}
	void push_back(INT x){
		ins(Size,x);
	}
	void pop_back(){
		del(Size);
	}
	void push_front(INT x){
		ins(0,x);
	}
	void pop_front(){
		del(0);
	}
	void resize(int k,INT x=INT()){
		if(Size<k) ins(Size,k-1,x);
		if(Size>k) del(k,Size-1);
	}
	void clear(){
		del(0,Size-1);
	}
	iterator rand_elem(){
		return find(rand()%Size);
	}
	List(){
		Size=0;
	}
};