【POJ2892】Tunnel Warfare

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38083668/article/details/82940524

                                                    Tunnel Warfare

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 9475   Accepted: 3926

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.

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 <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;

const int Max=500005;
int n,m,root,tot,x,pre[Max],head;
struct shu{int l,r,num,data;};
shu a[Max];
char ch[2];

inline int get_int()
{
	int x=0,f=1;
	char c;
	for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
	if(c=='-') f=-1,c=getchar();
	for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
	return x*f;
}
inline void print(int x)
{
	if(x>9) print(x/10);
	putchar('0'+x%10);
}

inline int NEW(int x){a[++tot].num=x,a[tot].data=rand();return tot;}
inline void zig(int &p)
{
	int q=a[p].l;
	a[p].l=a[q].r,a[q].r=p,p=q;
}
inline void zag(int &p)
{
	int q=a[p].r;
	a[p].r=a[q].l,a[q].l=p,p=q;
}

inline void Insert(int &p,int x)
{
	if(!p){p=NEW(x);return;}
	if(x<a[p].num)
	{
	  Insert(a[p].l,x);
	  if(a[a[p].l].data>a[p].data) zig(p);
	}
	else
	{
	  Insert(a[p].r,x);
	  if(a[a[p].r].data>a[p].data) zag(p);
	}
}

inline void Remove(int &p,int x)
{
	if(a[p].num==x)
	{
	  if(a[p].l||a[p].r)
	  {
	  	if(!a[p].r||a[a[p].l].data>a[a[p].r].data) zig(p),Remove(a[p].r,x);
	  	else zag(p),Remove(a[p].l,x);
	  }
	  else p=0;
	  return;
	}
	if(x<a[p].num) Remove(a[p].l,x);
	else Remove(a[p].r,x);
}

inline int Pre(int &p,int x)
{
	if(!p) return 0;
	if(a[p].num==x) return a[p].num;
	if(a[p].num<x) return max(Pre(a[p].r,x),a[p].num);
	return Pre(a[p].l,x);
}

inline int Next(int &p,int x)
{
	if(!p) return n+1;
	if(a[p].num==x) return a[p].num;
	if(a[p].num>x) return min(Next(a[p].l,x),a[p].num);
	return Next(a[p].r,x);
}

int main()
{
	n=get_int(),m=get_int();
	NEW(0),NEW(n+1);
	for(int i=1;i<=m;i++)
	{
	  scanf("%s",ch);
	  if(ch[0]=='D') x=get_int(),Insert(root,pre[++head]=x);
	  if(ch[0]=='Q')
	  {
		x=get_int();
		int l=Pre(root,x),r=Next(root,x);
		if(l==x) printf("0\n");
		else print(r-l-1),putchar('\n');
	  }
	  if(ch[0]=='R') Remove(root,pre[head--]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38083668/article/details/82940524