您的位置:首页 > 其它

hdu 1540 Tunnel Warfare 线段树--区间合并

2012-08-15 16:11 232 查看

Tunnel Warfare

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


[align=left]Problem Description[/align]
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!

[align=left]Input[/align]
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.

[align=left]Output[/align]
Output the answer to each of the Army commanders’ request in order on a separate line.

[align=left]Sample Input[/align]

7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

[align=left]Sample Output[/align]

1
0
2
4

[align=left]Source[/align]
POJ Monthly

[align=left]Recommend[/align]
LL
题意:有从1到n成一条直线相邻的n个村庄,这些村庄通过地道与相邻的村庄连接起来。
   D x:摧毁第x个村庄。
   Q x:问与第x个村庄连通的总村庄个数,包括第x个村庄。
   R :重建最后被摧毁的那个村庄。
解法:线段树(关键在区间判断与合并),也就是下面代码中的seachSum()函数。
代码如下:

#include <iostream>
#include <cstdio>
#include <stack>
#define N 50010
using namespace std;
struct node{
int Left,Right;
bool Flag;
}point[3*N];
int startx,endy,index
;
void makeTree(int start,int end,int points)
{
point[points].Left=start;
point[points].Right=end;
point[points].Flag=true;
if(start==end)
{
index[start]=points;
return ;
}
int middle=(start+end)>>1;
makeTree(start,middle,points<<1);
makeTree(middle+1,end,points<<1|1);
}
void destroyed(int points)
{
while(points>0)
{
point[points].Flag=false;
points>>=1;
}
}
void rebuilt(int points)
{
while(points>0)
{
point[points].Flag=true;
if(points%2==0&&point[points+1].Flag==true)
{
points>>=1;
}
else if(points%2&&point[points-1].Flag==true)
{
points>>=1;
}
else
{
break;
}
}
}
int seachSum(int ps_index,int &start,int &end,int points)
{
if(point[index[points]].Flag==false)
{
return 0;
}
if(point[ps_index].Flag==true)
{
start=point[ps_index].Left;
end=point[ps_index].Right;
return end-start+1;
}
int middle=(start+end)>>1;
if(point[index[middle]].Flag==true&&point[index[middle+1]].Flag==true)
{
int x1,x2,y1,y2;
x1=start;
y2=end;
y1=middle;
x2=middle+1;
seachSum(ps_index<<1,x1,y1,points);
seachSum(ps_index<<1|1,x2,y2,points);
if(x2==y1+1)
{
start=x1;
end=y2;
}
else if(points>=x2)
{
start=x2;
end=y2;
}
else if(points<=y1)
{
start=x1;
end=y1;
}
return end-start+1;
}
else if(points<=middle)
{
seachSum(ps_index<<1,start,middle,points);
end=middle;
return end-start+1;
}
else if(points>middle)
{
int xn=middle+1;
seachSum(ps_index<<1|1,xn,end,points);
start=xn;
return end-start+1;
}
}
int main()
{
int n,m,p_index;
while(scanf("%d%d",&n,&m)!=EOF){
makeTree(1,n,1);
stack<int> sv;
while(m--)
{
char ch;
scanf("%*c%c",&ch);
if(ch=='D')
{
scanf("%d",&p_index);
sv.push(p_index);
destroyed(index[p_index]);
}
else if(ch=='R'&&!sv.empty())
{
p_index=sv.top();
sv.pop();
rebuilt(index[p_index]);
}
else
{
scanf("%d",&p_index);
startx=1;
endy=n;
printf("%d\n",seachSum(1,startx,endy,p_index));
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: