﻿ hdu1540 Tunnel Warfare（线段树）-C 语言_软件编程-脚本宝典

# hdu1540 Tunnel Warfare（线段树）

## hdu1540 Tunnel Warfare（线段树）

Tunnel WarfareTime Limit: 4000 2000 MS (Java Others) Memory Limit: 65536 32768 K (Java Others)Total Submission(s): 6052 Accepted Submission(s): 2340Problem DescriptionDurin

# Tunnel Warfare

Time Limit: 4000/2000 MS (/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6052 Accepted Submission(s): 2340

Problem 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```

Source POJ Monthly

```#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
const int MAXN = 50000+10;

struct node
{
int l,r;
int ls,rs,ms;//ls为左边最大连续区间，rs为右，ms为区间内最大连续长度
}t[MAXN<<2];
int n,m;
int s[MAXN],top;//模拟栈

void build(int x, int y, int num)
{
t[num].l = x;
t[num].r = y;
t[num].ls = t[num].rs = t[num].ms = y-x+1;
if(x == y) return ;
int mid = (x+y)>>1;
build(x, mid, num<<1);
build(mid+1, y, num<<1|1);
}

void update(int x, int num, int ok)
{
if(t[num].l == t[num].r)
{
if(ok) //修复
t[num].ls = t[num].rs = t[num].ms = 1;
else //破坏
t[num].ls = t[num].rs = t[num].ms = 0;
return ;
}
int mid = (t[num].l+t[num].r)>>1;
if(x <= mid)
update(x, num<<1, ok);
else
update(x, num<<1|1, ok);
t[num].ls = t[num<<1].ls;
t[num].rs = t[num<<1|1].rs;
t[num].ms = max(max(t[num<<1].ms, t[num<<1|1].ms), t[num<<1].rs+t[num<<1|1].ls);
if(t[num<<1].ls == t[num<<1].r-t[num<<1].l+1)//如果左子树满了，父亲左区间要加上右孩子的左区间
t[num].ls += t[num<<1|1].ls;
if(t[num<<1|1].rs == t[num<<1|1].r-t[num<<1|1].l+1)//同理
t[num].rs += t[num<<1].rs;
}

int query(int x, int num)
{
if(t[num].l==t[num].r || t[num].ms==0 || t[num].ms==t[num].r-t[num].l+1)
return t[num].ms;
int mid = (t[num].l+t[num].r)>>1;
if(x <= mid)
{
if(x >= t[num<<1].r-t[num<<1].rs+1)//因为x<=mid，看左子树，t[num<<1].r-t[num<<1].rs+1代表左子树右边连续区间的左边界值，如果t在左子树的右区间内，则要看右子树的左区间有多长并返回
return query(x, num<<1)+query(mid+1, num<<1|1);
else
return query(x, num<<1);//如果不在左子树的右边界区间内，则只需要看左子树
}
else
{
if(x <= t[num<<1|1].l+t[num<<1|1].ls-1)//同理
return query(x, num<<1|1)+query(mid, num<<1);
else
return query(x, num<<1|1);
}
}

int main()
{
char ch[2];//其实这里我也不懂为什么用单个的字符就不行，会RE
int x;
while(scanf("%d%d",&n,&m)==2)
{
top = 0;
build(1, n, 1);
while(m--)
{
//getchar();
scanf("%s",ch);
if(ch[0] == 'D')
{
scanf("%d",&x);
s[top++] = x;
update(x, 1, 0);
}
else if(ch[0] == 'Q')
{
scanf("%d",&x);
printf("%d\n",query(x, 1));
}
else
{
if(top > 0)
{
x = s[--top];
update(x, 1, 1);
}
}
}
}
return 0;
}
```

Tags：

﻿
<