POJ Tunnel Warfare

Description

During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!

Input

The first line of the input contains two positive integers n and m (n, m 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

There are three different events described in different format shown below:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. R: The village destroyed last was rebuilt.

题目大意

给出直线上n(n 50,000) 个村庄,对于一个村庄,如果其相邻村庄没有被破坏,则这两个村庄是连接的。

题目给出m个(m 50,000) 破坏操作,对指定号码的村庄进行破坏,还有一系列的询问操作,询问与指定号码的村庄直接相连或间接相连的村庄有几个,还有一个修复操作,是对最后破坏的村庄进行修复。

Output

Output the answer to each of the Army commanders request in order on a separate line.

Sample Input

7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output

1
0
2
4

Hint

An illustration of the sample input:

      OOOOOOO

D 3   OOXOOOO

D 6   OOXOOXO

D 5   OOXOXXO

R     OOXOOXO

R     OOXOOOO

 

题解

treap的应用。可以把被摧毁的点放入treap中,然后对于每个询问,类似于找前驱后继一样,找到与询问村庄号码差距最小的两个点即可。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
int n,m,root,size,q,h;
int des[50002],zz=0;
struct shu
{int num,l,r,rnd,son;} tr[50002];
char c[3];
void renew(int &w)//更新字数节点个数 
{tr[w].son=tr[tr[w].l].son+tr[tr[w].r].son+1;}

void lturn(int &w)//左旋 
{
	int t=tr[w].r;
	tr[w].r=tr[t].l; tr[t].l=w; tr[t].son=tr[w].son; renew(w); w=t;
}

void rturn(int &w)//右旋 
{
	int t=tr[w].l;
	tr[w].l=tr[t].r; tr[t].r=w; tr[t].son=tr[w].son; renew(w); w=t;
}
void insert(int &w,int x)
{
	if(w==0)
	   {size++; w=size; tr[w].num=x; tr[w].son=1; tr[w].rnd=rand(); return ;}
	tr[w].son++;
	//if(tr[w].num==x) 
	if(tr[w].num<x)
	   {insert(tr[w].r,x); if(tr[tr[w].r].rnd<tr[w].rnd) lturn(w);}
	else
	   {insert(tr[w].l,x); if(tr[tr[w].l].rnd<tr[w].rnd) rturn(w);}
}
void del(int &w,int x)
{
	if(tr[w].num==x)
       {if(tr[w].l*tr[w].r==0) w=tr[w].l+tr[w].r;
        else if(tr[tr[w].l].rnd>=tr[tr[w].r].rnd)
           {rturn(w); del(w,x);}
        else if(tr[tr[w].l].rnd<tr[tr[w].r].rnd)
           {lturn(w); del(w,x);}
        return;
       }
    else if(tr[w].num<x)
        del(tr[w].r,x);
    else del(tr[w].l,x);
}
void find(int &w,int x)
{
    if(w==0)return;
    if(tr[w].num>=x&&tr[w].num<h) h=tr[w].num;
    if(tr[w].num<=x&&tr[w].num>q) q=tr[w].num;
    if(tr[w].num<x) find(tr[w].r,x);
    else find(tr[w].l,x);
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	   {int x;
		scanf("%s",c);
	    if(c[0]=='D')
		   {scanf("%d",&x); 
		    des[++zz]=x; insert(root,x);
		   }
		else if(c[0]=='Q')
		   {scanf("%d",&x); 
		    q=0; h=n+1;
			find(root,x);
			if(q==x&&h==x) printf("0\n");
		    else printf("%d\n",h-q-1);
		   }
		else
		   {del(root,des[zz]); zz--;}
	   }
	return 0;
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值