洛谷P1503 鬼子进村

8 篇文章 0 订阅
3 篇文章 0 订阅

题目背景

小卡正在新家的客厅中看电视。电视里正在播放放了千八百次依旧重播的《亮剑》,剧中李云龙带领的独立团在一个县城遇到了一个鬼子小队,于是独立团与鬼子展开游击战。

题目描述

描述 县城里有n个用地道相连的房子,第i个只与第i-1和第i+1个相连。这是有m个消息依次传来

1、消息为D x:鬼子将x号房子摧毁了,地道被堵上。

2、消息为R :村民们将鬼子上一个摧毁的房子修复了。

3、消息为Q x:有一名士兵被围堵在x号房子中。

李云龙收到信息很紧张,他想知道每一个被围堵的士兵能够到达的房子有几个。

输入输出格式

输入格式:

第一行2个整数n,m(n,m<=50000)。

接下来m行,有如题目所说的三种信息共m条。

输出格式:

对于每一个被围堵的士兵,输出该士兵能够到达的房子数。

输入输出样例

输入样例#1:  复制
7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
输出样例#1:  复制
1
0
2
4

说明

若士兵被围堵在摧毁了的房子中,那只能等死了。。。。。。







题解:

很简单的一道平衡树,但不知道为什么。


代码:(70分splay):

#include<bits/stdc++.h>
using namespace std;
int g[100001],fa[100001],ch[100001][2],val[100001],root,tot,ha[100001];
void clear(){
	root=tot=0;
}
void rotate(int x,int kind){
	int y=fa[x],z=fa[y];
	ch[y][!kind]=ch[x][kind];if(ch[x][kind])fa[ch[x][kind]]=y;
	fa[x]=z;if(z)ch[z][ch[z][1]==y]=x;
	fa[y]=x;ch[x][kind]=y;
}
void splay(int &rot,int x){
	int y,z;
	if(!fa[x])return;
	while(fa[x]){
		y=fa[x];z=fa[y];
		//printf("%d %d %d\n",x,y,z);
		if(z){
			if(ch[z][0]==y&&ch[y][0]==x)rotate(x,1),rotate(x,1);
			 else if(ch[z][1]==y&&ch[y][1]==x)rotate(x,0),rotate(x,0);
			  else if(ch[z][0]==y&&ch[y][1]==x)rotate(x,0),rotate(x,1);
			   else rotate(x,1),rotate(x,0);
		}
		 else if(ch[y][0]==x)rotate(x,1);
		  else rotate(x,0);
	}
	rot=x;
}
int find(int now,int x){
	while(now){
		if(val[now]==x)break;
		now=x<val[now]?ch[now][0]:ch[now][1];
	}
	if(now)splay(root,now);
	return now;
}
int getmax(int now){
	while(ch[now][1])now=ch[now][1];
	return now;
}
int findpre(int k,int key){
	int tmp;
	if(!k)return 0;
	if(key<val[k])return findpre(ch[k][0],key);
	 else {
	  tmp=findpre(ch[k][1],key);
	  return tmp?tmp:val[k];
}
}
int findnxt(int k,int key){
	if(!k)return 0;
	if(key>val[k])return findnxt(ch[k][1],key);
	 else{
	 	int tmp=findnxt(ch[k][0],key);
	 	return tmp?tmp:val[k];
	 }
}
void insert(int &now,int x,int p){
	if(!now){
		val[now=++tot]=x;
		ch[now][0]=ch[now][1]=0;
		fa[now]=p;
		splay(root,now);
		return;
	}
	if(x<val[now])insert(ch[now][0],x,now);
	 else if(x>val[now])insert(ch[now][1],x,now);
}  
void ins(int x){
	if(!root){
		val[root=++tot]=x;
		ch[root][0]=ch[root][1]=0;
		return;
	}
	insert(root,x,0);
}
void del(int key){
	int ls,rs,x,lson,now;
	x=find(root,key);
	ls=ch[x][0];rs=ch[x][1];
	//printf("%d %d %d\n",x,ls,rs);
	if(!ls&&!rs){
		clear();
		return;
	}
	if(!ls)root=rs,fa[rs]=0;
	 else if(!rs)root=ls,fa[ls]=0;
	  else{
	  	lson=getmax(ls);
	  	//printf("%d %d\n",ls,lson);
	  	swap(lson,ls);
	  	fa[lson]=0;splay(root,ls);
	  	ch[ls][1]=rs;fa[rs]=ls;
	  }
}
int main(){
	int i,j,t,l,r,n1=0,n,m;
	char S[10];
	scanf("%d%d",&n,&m);
	for(i=1;i<=m;i++){
		//printf("%d\n",i);
		scanf("%s",&S);
		if(S[0]=='D'){
			scanf("%d",&t);
			if(!g[t])
			//printf("%d\n",t);
			ins(t);
			ha[++n1]=t;
			g[t]++;
		}
		 else if(S[0]=='R'){
		 	if(g[ha[n1]])del(ha[n1]);
		 	g[ha[n1]]=0;
		 	n1--;
		 }
		  else{
		  	scanf("%d",&t);
		  	if(g[t])printf("0\n");
		  	 else{
		  	 	l=findpre(root,t);r=findnxt(root,t);
		  	 	//printf("%d %d\n",l,r);
		  	 	if(!r)r=n+1;
		  	 	printf("%d\n",r-l-1);
			   }
		  }
		 //for(j=1;j<=tot;j++)printf("%d ",val[j]);
		 //puts(""); 
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值