POJ 2892 Tunnel Warfare

本文介绍了一种模拟抗日战争期间地道战的网络模型。该模型使用了一种特殊的平衡树结构来快速更新和查询村庄间的连接状态,以反映地道被破坏或修复后的最新情况。输入包括村庄数量及发生的事件,输出则为特定查询时刻各村庄间连接的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 (nm  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

Source

POJ Monthly--2006.07.30, updog

用平衡树维护删去的节点,注意判断临界值。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<stack>
using namespace std;
const int N=100005;
int n,m,root,cnt,s,e;
stack<int>stk;
struct treap
{
	int l,r,val,rnd;
}t[N];
void lt(int &k)
{
	int now=t[k].r;
	t[k].r=t[now].l;
	t[now].l=k;
	k=now;
}
void rt(int &k)
{
	int now=t[k].l;
	t[k].l=t[now].r;
	t[now].r=k;
	k=now;
}
void insert(int &k,int x)
{
	if(!k)
	{
		k=++cnt;
		t[k].val=x;
		t[k].rnd=rand();
		return ;
	}
	if(x>t[k].val)
	{
		insert(t[k].r,x);
		if(t[t[k].r].rnd<t[k].rnd)
			lt(k);
	}
	else if(x<t[k].val)
	{
		insert(t[k].l,x);
		if(t[t[k].l].rnd<t[k].rnd)
			rt(k);
	}
}
void del(int &k,int x)
{
	if(x>t[k].val)
		del(t[k].r,x);
	else if(x<t[k].val)
		del(t[k].l,x);
	else
	{
		if(t[k].l*t[k].r==0)
			k=t[k].l+t[k].r;
		else
		{
			if(t[t[k].l].rnd<t[t[k].r].rnd)
				rt(k);
			else
				lt(k);
			del(k,x);
		}
	}
}
void find(int k,int x)
{
	if(!k)
		return ;
	if(t[k].val>=x&&t[k].val<e)
		e=t[k].val;
	if(t[k].val<=x&&t[k].val>s)
		s=t[k].val;
	if(t[k].val<x)
		find(t[k].r,x);
	else
		find(t[k].l,x);
}
int main()
{
	scanf("%d%d",&n,&m);
	while(m--)
	{
		char ch[11];
		int x;
		scanf("%s",ch);
		if(ch[0]=='D')
		{
			scanf("%d",&x);
			insert(root,x);
			stk.push(x);
		}
		if(ch[0]=='Q')
		{
			scanf("%d",&x);
			s=1,e=n+1;
			find(root,x);
			if(s==x&&e==x)
				printf("0\n");
			else
				printf("%d\n",e-s-1);
		}
		if(ch[0]=='R')
			if(!stk.empty())
			{
				del(root,stk.top());
				stk.pop();
			}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值