hdu 1540 神奇的线段树

来源:互联网 发布:免费手机记账软件 编辑:程序博客网 时间:2024/06/07 05:45
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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
 

Sample Output
1024


 

 

题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少

思路:代码中的注释已经比较详细了,所以不多解释什么,在线段树的区间内,我们要用三个变量记录左边连续区间,右边连续区间和最大连续区间

 

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <algorithm>  
  4. #include <math.h>  
  5. #include <stdlib.h>  
  6. using namespace std;  
  7.   
  8. const int maxn = 50000+10;  
  9.   
  10. int n,m;  
  11. int s[maxn],top;//s为模拟栈  
  12.   
  13. struct node  
  14. {  
  15.     int l,r;  
  16.     int ls,rs,ms;//ls,左端最大连续区间,rs右端最大连续区间,ms区间内最大连续区间  
  17. } a[maxn<<2];  
  18.   
  19. void init(int l,int r,int i)  
  20. {  
  21.     a[i].l = l;  
  22.     a[i].r = r;  
  23.     a[i].ls = a[i].rs = a[i].ms = r-l+1;  
  24.     if(l!=r)  
  25.     {  
  26.         int mid = (l+r)>>1;  
  27.         init(l,mid,i*2);  
  28.         init(mid+1,r,2*i+1);  
  29.     }  
  30. }  
  31.   
  32. void insert(int i,int t,int x)  
  33. {  
  34.     if(a[i].l == a[i].r)  
  35.     {  
  36.         if(x==1)  
  37.             a[i].ls = a[i].rs = a[i].ms = 1;//修复  
  38.         else  
  39.             a[i].ls = a[i].rs = a[i].ms = 0;//破坏  
  40.         return ;  
  41.     }  
  42.     int mid = (a[i].l+a[i].r)>>1;  
  43.     if(t<=mid)  
  44.         insert(2*i,t,x);  
  45.     else  
  46.         insert(2*i+1,t,x);  
  47.     a[i].ls = a[2*i].ls;//左区间  
  48.     a[i].rs = a[2*i+1].rs;//右区间  
  49.     a[i].ms = max(max(a[2*i].ms,a[2*i+1].ms),a[2*i].rs+a[2*i+1].ls);//父亲区间内的最大区间必定是,左子树最大区间,右子树最大区间,左右子树合并的中间区间,三者中最大的区间值  
  50.     if(a[2*i].ls == a[2*i].r-a[2*i].l+1)//左子树区间满了的话,父亲左区间要加上右孩子的左区间  
  51.         a[i].ls += a[2*i+1].ls;  
  52.     if(a[2*i+1].rs == a[2*i+1].r-a[2*i+1].l+1)//同理  
  53.         a[i].rs += a[2*i].rs;  
  54. }  
  55.   
  56. int query(int i,int t)  
  57. {  
  58.     if(a[i].l == a[i].r || a[i].ms == 0 || a[i].ms == a[i].r-a[i].l+1)//到了叶子节点或者该访问区间为空或者已满都不必要往下走了  
  59.         return a[i].ms;  
  60.     int mid = (a[i].l+a[i].r)>>1;  
  61.     if(t<=mid)  
  62.     {  
  63.         if(t>=a[2*i].r-a[2*i].rs+1)//因为t<=mid,看左子树,a[2*i].r-a[2*i].rs+1代表左子树右边连续区间的左边界值,如果t在左子树的右区间内,则要看右子树的左区间有多长并返回  
  64.             return query(2*i,t)+query(2*i+1,mid+1);  
  65.         else  
  66.             return query(2*i,t);//如果不在左子树的右边界区间内,则只需要看左子树  
  67.     }  
  68.     else  
  69.     {  
  70.         if(t<=a[2*i+1].l+a[2*i+1].ls-1)//同理  
  71.             return query(2*i+1,t)+query(2*i,mid);  
  72.         else  
  73.             return query(2*i+1,t);  
  74.     }  
  75. }  
  76.   
  77. int main()  
  78. {  
  79.     int i,j,x;  
  80.     char ch[2];  
  81.     while(~scanf("%d%d",&n,&m))  
  82.     {  
  83.         top = 0;  
  84.         init(1,n,1);  
  85.         while(m--)  
  86.         {  
  87.             scanf("%s",ch);  
  88.             if(ch[0] == 'D')  
  89.             {  
  90.                 scanf("%d",&x);  
  91.                 s[top++] = x;  
  92.                 insert(1,x,0);  
  93.             }  
  94.             else if(ch[0] == 'Q')  
  95.             {  
  96.                 scanf("%d",&x);  
  97.                 printf("%d\n",query(1,x));  
  98.             }  
  99.             else  
  100.             {  
  101.                 if(x>0)  
  102.                 {  
  103.                     x = s[--top];  
  104.                     insert(1,x,1);  
  105.                 }  
  106.             }  
  107.         }  
  108.     }  
  109.   
  110.     return 0;  
  111. }  



0 0
原创粉丝点击