关闭

ACM 数据结构 线段树 HDU 1540 Tunnel Warfare

184人阅读 评论(0) 收藏 举报
  1. 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


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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:46679次
    • 积分:3088
    • 等级:
    • 排名:第12060名
    • 原创:270篇
    • 转载:10篇
    • 译文:0篇
    • 评论:8条