记录编号 280708 评测结果 AAAAAAAAAAAAAAAAAAAAA
题目名称 [HAOI 2014]贴海报 最终得分 100
用户昵称 GravatarAntiLeaf 是否通过 通过
代码语言 C++ 运行时间 0.019 s
提交时间 2016-07-10 17:34:12 内存使用 1.50 MiB
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=50010;
struct node{//AVL Balanced Tree
	int data,size,h;
	node *lch,*rch,*prt;
	node(int d=0):data(d),size(1),h(1),lch(NULL),rch(NULL),prt(NULL){}
	void refresh(){
		size=h=1;
		if(lch){
			size+=lch->size;
			h=max(h,lch->h+1);
		}
		if(rch){
			size+=rch->size;
			h=max(h,rch->h+1);
		}
	}
	int bal(){
		if(lch&&rch)return lch->h-rch->h;
		if(lch&&!rch)return lch->h;
		if(!lch&&rch)return -rch->h;
		return 0;
	}
};
node *root=NULL;
node *newnode(int=0);
void insert(node*,node* =root);
node *find(int,node* =root);
void del(node*);
node *prev(node*);
node *succ(node*);
void lrot(node*);
void rrot(node*);
node *findmin(node* =root);
node *findmax(node* =root);
int rank(int,node* =root);
node *kth(int,node* =root);
int previous(int,node* =root);
int succeeding(int,node* =root);
struct Event{
	bool type;//true means in,false means out
	int pos,num;
	bool operator<(const Event& x)const{
		if(pos!=x.pos)return pos<x.pos;
		if(type!=x.type)return type;
		return num>x.num;
	}
}a[maxn<<1];
int n,m=0;
bool vis[maxn]={false};
int main(){
#define MINE
#ifdef MINE
	freopen("ha14d.in","r",stdin);
	freopen("ha14d.out","w",stdout);
#endif
	insert(new node(0));
	scanf("%*d%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].pos,&a[i+n].pos);
		a[i+n].pos++;
		a[i].num=a[i+n].num=i;
		a[i].type=true;
		a[i+n].type=false;
	}
	n<<=1;
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		if(a[i].type)insert(new node(a[i].num));
		else{
			del(find(a[i].num));
			while(i<=n&&!a[i+1].type&&a[i+1].pos==a[i].pos){
				del(find(a[++i].num));
			}
		}
		vis[findmax()->data]=true;
	}
	n>>=1;
	for(int i=1;i<=n;i++)if(vis[i])m++;
	printf("%d\n",m);
	return 0;
}
node *newnode(int d){
	return new node(d);
}
void insert(node *x,node *rt){
	if(!root){
		root=x;
		return;
	}
	for(;;){
		if(x->data<rt->data){
			if(rt->lch)rt=rt->lch;
			else{
				rt->lch=x;
				break;
			}
		}
		else{
			if(rt->rch)rt=rt->rch;
			else{
				rt->rch=x;
				break;
			}
		}
	}
	x->prt=rt;
	//Insert Balance
	while(rt){
		rt->refresh();
		if(rt->bal()==2){//L
			x=rt->lch;
			if(x->bal()==1){//LL
				rrot(rt);
			}
			else{//LR
				lrot(x);
				rrot(rt);
			}
			rt=rt->prt;
		}
		else if(rt->bal()==-2){//L
			x=rt->rch;
			if(x->bal()==-1){//RR
				lrot(rt);
			}
			else{//RL
				rrot(x);
				lrot(rt);
			}
			rt=rt->prt;
		}
		rt=rt->prt;
	}
}
node *find(int x,node *rt){
	while(rt){
		if(x==rt->data)return rt;
		if(x<rt->data)rt=rt->lch;
		else rt=rt->rch;
	}
	return NULL;
}
void del(node *x){
	if(x->lch&&x->rch){
		node *y=prev(x);
		x->data=y->data;
		del(y);
	}
	else{
		if(x->lch&&!x->rch){
			x->lch->prt=x->prt;
			if(x->prt){
				if(x==x->prt->lch)x->prt->lch=x->lch;
				else x->prt->rch=x->lch;
			}
			else root=x->lch;
		}
		else if(!x->lch&&x->rch){
			x->rch->prt=x->prt;
			if(x->prt){
				if(x==x->prt->lch)x->prt->lch=x->rch;
				else x->prt->rch=x->rch;
			}
			else root=x->rch;
		}
		else{
			if(x->prt){
				if(x==x->prt->lch)x->prt->lch=NULL;
				else x->prt->rch=NULL;
			}
			else root=NULL;
		}
		//Delete Balance
		node *rt=x->prt;
		delete x;
		for(;rt;rt=rt->prt){
			rt->refresh();
			if(rt->bal()==2){//L
				x=rt->lch;
				if(x->bal()==1){//LL
					rrot(rt);
				}
				else if(x->bal()==-1){//LR
					lrot(x);
					rrot(rt);
				}
				else{//LE
					rrot(rt);
				}
				rt=rt->prt;
			}
			else if(rt->bal()==-2){//R
				x=rt->rch;
				if(x->bal()==-1){//RR
					lrot(rt);
				}
				else if(x->bal()==1){//RL
					rrot(x);
					lrot(rt);
				}
				else{//RE
					lrot(rt);
				}
				rt=rt->prt;
			}
		}
	}
}
node *prev(node *x){
	if(!x->lch)return NULL;
	for(x=x->lch;x->rch;x=x->rch);
	return x;
}
node *succ(node *x){
	if(!x->rch)return NULL;
	for(x=x->rch;x->lch;x=x->lch);
	return x;
}
void lrot(node *x){
	if(!x)return;
	node *y=x->rch;
	if(!y)return;
	y->prt=x->prt;
	if(x->prt){
		if(x==x->prt->lch)x->prt->lch=y;
		else x->prt->rch=y;
	}
	else root=y;
	x->prt=y;
	x->rch=y->lch;
	if(y->lch)y->lch->prt=x;
	y->lch=x;
	x->refresh();
	y->refresh();
}
void rrot(node *x){
	if(!x)return;
	node *y=x->lch;
	if(!y)return;
	y->prt=x->prt;
	if(x->prt){
		if(x==x->prt->lch)x->prt->lch=y;
		else x->prt->rch=y;
	}
	else root=y;
	x->prt=y;
	x->lch=y->rch;
	if(y->rch)y->rch->prt=x;
	y->rch=x;
	x->refresh();
	y->refresh();
}
node *findmin(node *rt){
	if(!rt)return NULL;
	while(rt->lch)rt=rt->lch;
	return rt;
}
node *findmax(node *rt){
	if(!rt)return NULL;
	while(rt&&rt->rch)rt=rt->rch;
	return rt;
}
int rank(int x,node* rt){
	if(!rt)return -1;
	if(!rt->rch&&x>rt->data)return -1;
	if(rt->lch){
		if(x==rt->data){
			while(rt->lch&&rt->lch->data==rt->data)rt=rt->lch;
			if(rt->lch)return rt->lch->size+1;
			return 1;
		}
		if(x<rt->data)return rank(x,rt->lch);
		return rank(x,rt->rch)+rt->lch->size+1;
	}
	if(x==rt->data)return 1;
	if(x<rt->data)return -1;
	return rank(x,rt->rch)+1;
}
node *kth(int x,node *rt){
	if(x==1)return findmin(rt);
	if(rt->lch&&rt->lch->size==x-1)return rt;
	if(rt->lch&&rt->lch->size>=x)return kth(x,rt->lch);
	else if(rt->lch)return kth(x-rt->lch->size-1,rt->rch);
	else return kth(x-1,rt->rch);
}
int previous(int x,node *rt){//小于x,且最大的数
	if(!rt)return -0x7ffffff;
	if(x<=rt->data)return previous(x,rt->lch);
	else return max(rt->data,previous(x,rt->rch));//rt->data<x
}
int succeeding(int x,node *rt){//大于x,且最小的数
	if(!rt)return 0x7ffffff;
	if(x>=rt->data)return succeeding(x,rt->rch);
	else return min(rt->data,succeeding(x,rt->lch));//rt->data>x
}
/*int previous(int x,node *rt){
	if(!rt)return 0x7ffffff;
	if(rt->size==1)return rt->data;
	if(rt->data>=x)return previous(x,rt->lch);
	if(!rt->rch)return rt->data;
	node *y=findmin(rt->rch);
	return (y&&y->data<x?previous(x,rt->rch):rt->data);
}
int succeeding(int x,node *rt){
	if(!rt)return -0x7ffffff;
	if(rt->size==1)return rt->data;
	if(rt->data<=x)return succeeding(x,rt->rch);
	if(!rt->lch)return rt->data;
	node *y=findmax(rt->lch);
	return (y&&y->data>x)?succeeding(x,rt->lch):rt->data;
}*/