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

Tunnel Warfare

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


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
 

 

Recommend
LL
 题意:有从1到n成一条直线相邻的n个村庄,这些村庄通过地道与相邻的村庄连接起来。
   D x:摧毁第x个村庄。
   Q x:问与第x个村庄连通的总村庄个数,包括第x个村庄。
   R   :重建最后被摧毁的那个村庄。
解法:线段树(关键在区间判断与合并),也就是下面代码中的seachSum()函数。
代码如下:
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <stack>
  4 #define N 50010
  5 using namespace std;
  6 struct node{
  7     int Left,Right;
  8     bool Flag;
  9 }point[3*N];
 10 int startx,endy,index[N];
 11 void makeTree(int start,int end,int points)
 12 {
 13     point[points].Left=start;
 14     point[points].Right=end;
 15     point[points].Flag=true;
 16     if(start==end)
 17     {
 18         index[start]=points;
 19         return ;
 20     }
 21     int middle=(start+end)>>1;
 22     makeTree(start,middle,points<<1);
 23     makeTree(middle+1,end,points<<1|1);
 24 }
 25 void destroyed(int points)
 26 {
 27     while(points>0)
 28     {
 29         point[points].Flag=false;
 30         points>>=1;
 31     }
 32 }
 33 void rebuilt(int points)
 34 {
 35     while(points>0)
 36     {
 37         point[points].Flag=true;
 38         if(points%2==0&&point[points+1].Flag==true)
 39         {
 40             points>>=1;
 41         }
 42         else if(points%2&&point[points-1].Flag==true)
 43         {
 44             points>>=1;
 45         }
 46         else
 47         {
 48             break;
 49         }
 50     }
 51 }
 52 int seachSum(int ps_index,int &start,int &end,int points)
 53 {
 54     if(point[index[points]].Flag==false)
 55     {
 56         return 0;
 57     }
 58     if(point[ps_index].Flag==true)
 59     {
 60         start=point[ps_index].Left;
 61         end=point[ps_index].Right;
 62         return end-start+1;
 63     }
 64     int middle=(start+end)>>1;
 65     if(point[index[middle]].Flag==true&&point[index[middle+1]].Flag==true)
 66     {
 67         int x1,x2,y1,y2;
 68         x1=start;
 69         y2=end;
 70         y1=middle;
 71         x2=middle+1;
 72         seachSum(ps_index<<1,x1,y1,points);
 73         seachSum(ps_index<<1|1,x2,y2,points);
 74         if(x2==y1+1)
 75         {
 76             start=x1;
 77             end=y2;
 78         }
 79         else if(points>=x2)
 80         {
 81             start=x2;
 82             end=y2;
 83         }
 84         else if(points<=y1)
 85         {
 86             start=x1;
 87             end=y1;
 88         }
 89         return end-start+1;
 90     }
 91     else if(points<=middle)
 92     {
 93         seachSum(ps_index<<1,start,middle,points);
 94         end=middle;
 95         return end-start+1;
 96     }
 97     else if(points>middle)
 98     {
 99         int xn=middle+1;
100         seachSum(ps_index<<1|1,xn,end,points);
101         start=xn;
102         return end-start+1;
103     }
104 }
105 int main()
106 {
107     int n,m,p_index;
108     while(scanf("%d%d",&n,&m)!=EOF){
109         makeTree(1,n,1);
110         stack<int> sv;
111         while(m--)
112         {
113             char ch;
114             scanf("%*c%c",&ch);
115             if(ch=='D')
116             {
117                 scanf("%d",&p_index);
118                 sv.push(p_index);
119                 destroyed(index[p_index]);
120             }
121             else if(ch=='R'&&!sv.empty())
122             {
123                 p_index=sv.top();
124                 sv.pop();
125                 rebuilt(index[p_index]);
126             }
127             else
128             {
129                 scanf("%d",&p_index);
130                 startx=1;
131                 endy=n;
132                 printf("%d\n",seachSum(1,startx,endy,p_index));
133             }
134         }
135     }
136     return 0;
137 }

 

posted @ 2012-08-15 16:11  zyh123101  阅读(182)  评论(0编辑  收藏  举报