• Tunnel Warfare HDU


    Tunnel Warfare HDU - 1540 

    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

    题解:解一:线段树区间合并
          通过数组记录各个节点左or右孩子的最长连续区间,再通过区间的合并将该节点区间的最大值更新到一个数组中,然后进行查询
       解二:记录节点左孩子区间的最右区间边界值L和右孩子区间的最左区间边界值R,查询的时候计算出的 R-L+1 即为最长区间值
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <string>
     5 #include <vector>
     6 #include <map>
     7 #include <set>
     8 #include <list>
     9 #include <deque>
    10 #include <queue>
    11 #include <stack>
    12 #include <cstdlib>
    13 #include <cstdio>
    14 #include <cmath>
    15 #include <iomanip>
    16 #define ull unsigned long long
    17 #define ll long long
    18 #define pb push_back
    19 #define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    20 using namespace std;
    21 const int mod = 998244353;
    22 const int mxn = 2e5 +7;
    23 int _,n,m,t,k,u,v,ans,cnt,ok,lim;
    24 ll w[mxn] , cost[mxn] ,  far[mxn] , siz[mxn];
    25 char ch;
    26 #define lc now<<1
    27 #define rc now<<1|1
    28 string str ;
    29 struct node {ll l,r,val,lazy,mx,mn;}p[mxn<<4];
    30 int lmx[mxn] , rmx[mxn] , num[mxn] ;/// 区间左端点起最长连续区间,区间右端点起最长连续区间,整个节点区间最长连续区间
    31 void pushup(int l,int r,int now)
    32 {
    33     lmx[now] = lmx[lc] ; rmx[now] = rmx[rc]; /// 继承区间左端点起的最长连续区间,区间右端点起的最长连续区间
    34     int mid = (l+r)>>1;
    35     if(lmx[now]==mid-l+1) lmx[now]+=lmx[rc]; /// 区间左端点继承的最长连续区间等于区间长度,那么就加上右孩子的左端点起的最长连续区间
    36     if(rmx[now]==r-mid)   rmx[now]+=rmx[lc]; /// 区间右端点继承的最长连续区间等于区间长度,那么就加上左孩子的右端点起的最长连续区间
    37     num[now] = max( rmx[lc]+lmx[rc] , max(num[lc],num[rc]) ); /// 更新整个区间的最长连续区间值
    38 }
    39 void build(int l,int r,int now)
    40 {
    41     if(l==r){
    42         lmx[now] = rmx[now] = num[now] = 1 ; return ;
    43     }
    44     int mid = (l+r)>>1;
    45     build(l,mid,lc);build(mid+1,r,rc);pushup(l,r,now);
    46 }
    47 void up(int l,int r,int k,int now,int lim)
    48 {
    49     if(l==r){
    50         if(l==k){
    51             num[now] = lmx[now] = rmx[now] = (lim?0:1);  /// 更新 K 城市的状态
    52         }
    53         return ;
    54     }
    55     int mid = (l+r)>>1;
    56     if(k<=mid) up(l,mid,k,lc,lim);
    57     if(k>mid)  up(mid+1,r,k,rc,lim);
    58     pushup(l,r,now);
    59 }
    60 int ask(int l,int r,int k,int now)
    61 {
    62     if(num[now] == r-l+1 || !num[now]) return num[now] ; /// 摧毁状态或刚好等于区间,return
    63     int mid = (l+r)>>1;
    64     if(k<=mid)
    65     {
    66         if(k>=mid-rmx[lc]+1) return ask(l,mid,k,lc) + ask(mid+1,r,mid+1,rc); /// K城市在 区间左孩子右端点的位置,那么要加上右孩子与mid连续的最左连续子区间
    67         else return ask(l,mid,k,lc); /// K 城市在左孩子非右端点区间中
    68     }
    69     else
    70     {
    71         if(k<=mid+1+lmx[rc]-1) return ask(l,mid,mid,lc) + ask(mid+1,r,k,rc); /// K城市在右孩子的最左连续区间内,可能位于mid+1位置,呢么要加上左区间与mid连续的最右连续子区间
    72         else return ask(mid+1,r,k,rc);
    73     }
    74 }
    75 int main()
    76 {
    77     while(cin>>n>>m)
    78     {
    79         stack<int>s;
    80         build(1,n,1);
    81         while(m--)
    82         {
    83             cin>>ch;
    84             if(ch=='D'){
    85                 cin>>k; s.push(k);
    86                 up(1,n,k,1,1);
    87             }
    88             else if(ch=='Q'){
    89                 cin>>k;
    90                 cout<<ask(1,n,k,1)<<endl;
    91             }
    92             else {
    93                 up(1,n,s.top(),1,0);s.pop();
    94             }
    95         }
    96     }
    97 }
    所遇皆星河
  • 相关阅读:
    cmake安装配置及入门指南
    【算法篇】栈和队列专题之广度优先遍历和深度优先遍历
    【算法篇】链表专题
    【Android】JDK8标准下计算两个日期的时间差
    【MatLab】图片的拼接、滤色
    【C#】Winform开发笔记(持续更新)
    【Java】解决中文在post/get请求乱码的问题
    【C#】基于TCP的简单通信系统
    【Java】IDEA创建Web项目以及Tomcat配置
    【Java】模拟登录教务网并获取数据
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/12828962.html
Copyright © 2020-2023  润新知