zoukankan      html  css  js  c++  java
  • Tunnel Warfare (线段树的区间合并)

    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! 

    InputThe 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. 
    OutputOutput 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
    题意:输入n,m:n表示有n个城市相连,m表示下面有m行指令。D X表示破坏X这个城市,R表示修复最近一次破坏的城市,Q X表示查询以X点为中心的整体连续中最大的连续个数并输出。
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 using namespace std;
      6 const int maxn=500010;
      7 struct node{
      8     int l;
      9     int r;
     10     int ls,rs,ms;//分别表示一个区间里面的左最大连续,右最大连续,总最大连续 
     11 }e[maxn<<2];
     12 int n,m;
     13 int op[maxn<<1];
     14 void build(int l,int r,int cur)
     15 {
     16     e[cur].l=l;
     17     e[cur].r=r;
     18     e[cur].ls=e[cur].rs=e[cur].ms=r-l+1;//将每一个区间的左右和总最大连续初始化为该区间的长度 
     19     if(l==r)
     20         return ;
     21     int mid=(l+r)/2;
     22     build(l,mid,cur*2);
     23     build(mid+1,r,cur*2+1); 
     24 }
     25 void update(int x,int flag,int cur)//x表示修复或破坏的数字,flag用来记录是修复还是破坏
     26 {
     27     if(e[cur].l==e[cur].r)
     28     {
     29         if(flag==1)//修复 
     30         {
     31             e[cur].ls=e[cur].ms=e[cur].rs=1; 
     32         }
     33         else       //破坏 
     34         {
     35             e[cur].ls=e[cur].ms=e[cur].rs=0;
     36         }
     37         return;
     38     }
     39     int mid=(e[cur].l+e[cur].r)/2;
     40     if(x<=mid)
     41     {
     42         update(x,flag,cur*2);
     43     }
     44     else
     45     {
     46         update(x,flag,cur*2+1);
     47     }
     48     if(e[2*cur].ls==e[2*cur].r-e[2*cur].l+1)//如果一个区间的左子树的左连续等于左子树的区间长度, 
     49         e[cur].ls=e[2*cur].ls+e[2*cur+1].ls;//那么该区间的左连续就等于该区间的左子树的左连续(区间长度)加上右子树的左连续。 
     50     else
     51     {
     52         e[cur].ls=e[2*cur].ls;//否则该区间的左连续就等于左子树的左连续 
     53     }
     54     if(e[2*cur+1].rs==e[2*cur+1].r-e[2*cur+1].l+1)//同理 
     55     {
     56         e[cur].rs=e[2*cur+1].rs+e[2*cur].rs;
     57     }
     58     else
     59     {
     60         e[cur].rs=e[2*cur+1].rs;
     61     }
     62     e[cur].ms=max(max(e[2*cur].ms,e[2*cur+1].ms),e[2*cur].rs+e[2*cur+1].ls); //一个区间的最大连续长度就等于其左子树的最大连续长度、右子树的最大连续长度和左子树的右连续+右子树的左连续三者中取最大 
     63 } 
     64 int query(int x,int cur)
     65 {
     66     if(e[cur].l==e[cur].r||e[cur].ms==0||e[cur].ms==e[cur].r-e[cur].l+1)//如果是叶子节点或者该节点为空或为满都不必往下走了 
     67          return e[cur].ms;
     68      int mid=(e[cur].l+e[cur].r)/2;
     69      if(x<=mid)
     70      {
     71          if(x>=(e[2*cur].r-e[2*cur].rs+1))//判断这个数是否在左区间的右连续上 
     72          {
     73              return e[2*cur].rs+e[2*cur+1].ls;
     74         }
     75         else
     76         {
     77             return query(x,2*cur);
     78         }
     79     }
     80     else
     81     {
     82         if(x<=(e[2*cur+1].l+e[2*cur+1].ls-1))//判断这个数是否右区间的左连续上 
     83         {
     84             return e[2*cur].rs+e[2*cur+1].ls;
     85         }
     86         else
     87         {
     88             return query(x,2*cur+1);
     89         }
     90     }
     91 }
     92 int main()
     93 {
     94     int n,m,t;
     95     char ch;
     96     while(~scanf("%d%d",&n,&m))
     97     {
     98         int id=0;
     99         //printf("!233");
    100         build(1,n,1);
    101         while(m--)
    102         {
    103             cin>>ch;
    104             if(ch=='D')
    105             {
    106                 scanf("%d",&t);
    107                 op[id++]=t;
    108                 update(t,0,1);
    109             }
    110             if(ch=='Q')
    111             {
    112                 scanf("%d",&t);
    113                 printf("%d
    ",query(t,1));
    114             }
    115             if(ch=='R')
    116             {
    117                 if(t>0)
    118                 {
    119                     t=op[--id];
    120                     update(t,1,1);
    121                 }
    122             }
    123         }
    124     }
    125 return 0;
    126 }
     
  • 相关阅读:
    (一)Python入门-2编程基本概念:18字符串-驻留机制-内存分析-字符串同一判断-值相等判断
    (一)Python入门-2编程基本概念:07内置数据类型-基本算数运算符
    (一)Python入门-2编程基本概念:08整数-不同进制-其他类型转换成整数
    (一)Python入门-2编程基本概念:09浮点数-自动转换-强制转换-增强赋值运算符
    (一)Python入门-2编程基本概念:10时间表示-unix时间点-毫秒和微妙-time模块
    (一)Python入门-2编程基本概念:11布尔值-比较运算符-逻辑运算符及短路问题
    (一)Python入门-2编程基本概念:12同一运算符-整数缓存问题
    (一)Python入门-2编程基本概念:01程序的构成
    (一)Python入门-2编程基本概念:02对象的基本组成和内存示意图
    有关位运算的基础知识总结
  • 原文地址:https://www.cnblogs.com/1013star/p/9439579.html
Copyright © 2011-2022 走看看