程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj2892 Tunnel Warface

poj2892 Tunnel Warface

編輯:C++入門知識

poj2892 Tunnel Warface


 

Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7434   Accepted: 3070

 

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:

D x: The x-th village was destroyed.Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.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

 

 

 

平衡樹的應用

平衡樹中保存所有被炸毀的節點。

對於炸毀和修復操作,直接在平衡樹中插入或刪除。

對於查詢操作,先判斷該節點是否被炸毀,如果被炸毀答案為0,否則在平衡樹中求出前驅x和後繼y,答案為y-x-1。

還有一種方法是二分+樹狀數組,感覺速度比這個慢就沒有寫…


 

 

 

#include
#include
#include
#include
#include
#include
#include
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define MAXN 50005
#define pa pair
#define INF 1000000000
using namespace std;
int n,m,x,tot=0,rt=0,l[MAXN],r[MAXN],rnd[MAXN],v[MAXN];
char op;
bool f[MAXN];
stack st;
inline int read()
{
	int ret=0,flag=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
	return ret*flag;
}
inline void rturn(int &k)
{
	int tmp=l[k];
	l[k]=r[tmp];r[tmp]=k;
	k=tmp;
}
inline void lturn(int &k)
{
	int tmp=r[k];
	r[k]=l[tmp];l[tmp]=k;
	k=tmp;
}
inline void ins(int &k,int x)
{
	if (!k){k=++tot;v[k]=x;l[k]=r[k]=0;rnd[k]=rand();return;}
	if (x=v[k]) return suc(r[k],x);
	else {int tmp=suc(l[k],x);return tmp==n+1?v[k]:tmp;}
}
inline int getans(int x)
{
	if (f[x]) return 0;
	return suc(rt,x)-pre(rt,x)-1;
}
int main()
{
	memset(f,false,sizeof(f));
	n=read();m=read();
	while (m--)
	{
		op=getchar();while (op<'A'||op>'Z') op=getchar();
		if (op=='D') {x=read();f[x]=true;st.push(x);ins(rt,x);}
		else if (op=='Q') {x=read();printf("%d\n",getans(x));}
		else {del(rt,st.top());f[st.top()]=false;st.pop();}
	}
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved