Tunnel Warfare --HDU1540&POJ2892(簡單的暴力分塊替換線段樹區間合併)

Tunnel Warfare

Time Limit: 1000MS   Memory Limit: 131072K

題目鏈接http://poj.org/problem?id=2892

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

題目大意:給你n個村莊,讓有三種操作,D x代表毀滅第x個村莊,R代表重建最後一個毀滅的村莊,Q x代表一次詢問,問與x聯通的有多少個村莊。。。看Hint應該很容易理解了

博主一看到題目就想到了分塊。。。分塊的話就很好處理,果然線段樹還是不過關QAQ網上倒是有挺多區間合併的題解,我這裏就寫一下分塊的題解吧。

將n個村莊分完塊後,之後每一次毀滅操作,我們對x所處的塊進行標記+1,然後將a[x]=0,並用棧存下x這個位置。恢復操作的時候取出棧頂元素,將塊的標記-1,a[x]=1。重點就是查詢操作了,查詢的時候我們可以從x開始拓展,如果x所處的塊有標記的話,我們就再這個快裏面先找:

if (f[pos]) {
	for (int i=x-1; i>=L[pos]; i--) {
		if (!a[i]) {
			markl=1;//標記找到了左端點
			posl=i+1;//記錄左端點的位置
			break;
		}
	}
	for (int i=x+1; i<=R[pos]; i++) {
		if (!a[i]) {
			markr=1;//右端點找到了
			posr=i-1;//右端點的位置
			break;
		}
	}
}

接下來如果說左標記沒有的話就在pos(x所屬的塊)的前面找:

if (!markl) {
	for (int i=pos-1; i>=1; i--) {
		if (f[i]) {
			for (int j=R[i]; j>=L[i]; j--) {
				if (!a[j]) {markl=1;posl=j+1;break;}
			}
			break;
		}
	}
}

如果右標記沒有的話就在pos的後面找,代碼並沒有什麼改變。

最後特判兩個標記存在與否,然後按照每種情況返回不同的值就好了。。。注意HDU的需要多組輸入,真的非常坑QAQ

以下是AC代碼:

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

const int mac=5e4+10;
int t;

int L[1000],R[1000],id[mac],a[mac];
int f[1000],des[mac],tot=0,n;

void destroy(int x)
{
    int pos=id[x];
    if (a[x]) f[pos]++,des[++tot]=x,a[x]=0;
}

int query(int x)
{
    int pos=id[x];
    if (!a[x]) return 0;
    int posl=0,posr=0,markl=0,markr=0;
    if (f[pos]){
        for (int i=x-1; i>=L[pos]; i--){
            if (!a[i]) {markl=1;posl=i+1;break;}
        }
        for (int i=x+1; i<=R[pos]; i++){
            if (!a[i]) {markr=1;posr=i-1;break;}
        }
    }
    if (!markl){
        for (int i=pos-1; i>=1; i--){
            if (f[i]){
                for (int j=R[i]; j>=L[i]; j--){
                    if (!a[j]) {markl=1;posl=j+1;break;}
                }
                break;
            }
        }
    }
    if (!markr){
        for (int i=pos+1; i<=t; i++){
            if (f[i]){
                for (int j=L[i]; j<=R[i]; j++){
                    if (!a[j]) {markr=1;posr=j-1;break;}
                }
                break;
            }
        }
    }
    if (!markr) {
        if (!markl) return n;
        return n-posl+1;
    }
    else {
        if (markl) return posr-posl+1;
        return posr;
    }
}

void rebuild()
{
    if (!tot) return;
    int x=des[tot];
    tot--;
    a[x]=1;
    f[id[x]]--;
}

void in(int &x)
{
    int f=0;
    char ch=getchar();
    while (ch>'9' || ch<'0') ch=getchar();
    while (ch>='0' && ch<='9') f=(f<<3)+(f<<1)+ch-'0',ch=getchar();
    x=f;
}

int main()
{
    int m;
    in(n);in(m);
    t=sqrt(n);
    for (int i=1; i<=t; i++){
        L[i]=(i-1)*t+1;
        R[i]=i*t;
    }
    if (R[t]<n) t++,L[t]=R[t-1]+1,R[t]=n;
    for (int i=1; i<=t; i++)
        for (int j=L[i]; j<=R[i]; j++)
            id[j]=i,a[j]=1;
    for (int i=1; i<=m; i++){
        char ch[2];
        scanf ("%s",ch);
        int x;
        if (ch[0]=='D'){
            in(x);
            destroy(x);
        }
        else if (ch[0]=='Q'){
            in(x);
            int ans=query(x);
            printf ("%d\n",ans);
        }
        else rebuild();
    }
    return 0;
}
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章