zoukankan      html  css  js  c++  java
  • HDU 1540 Tunnel Warfare

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1540

    解题思路:线段树节点增加一个ld----区间左端点起始的最大长度,rd----区间右端点起始的最大长度,md----覆盖区间中点的最大长度,那么对于每次更新,更新到点然后更新父节点值;对于每次查询,若为中点,则返回md,否则从左右孩子中选择。需要注意的是ld,rd与md可能会有重叠部分,因此更新与查询的时候需要判断一下。

    代码:

     1 const int maxn = 5e4 + 5;
     2 
     3 int ld[maxn * 4], rd[maxn * 4], md[maxn * 4];
     4 int ok[maxn];
     5 int n, m;
     6 stack<int> s;
     7 
     8 void build(int l, int r, int k){
     9     ld[k] = rd[k] = md[k] = (r - l + 1);
    10     if(l == r) return;
    11     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    12     build(l, mid, lc);
    13     build(mid + 1, r, rc);
    14 }
    15 void update(int u, int x, int l, int r, int k){
    16     if(u == l && l == r){
    17         if(x == 0)
    18             ld[k] = rd[k] = md[k] = 0;
    19         else
    20             ld[k] = rd[k] = md[k] = 1;
    21         return;
    22     }
    23     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    24     if(u <= mid) update(u, x, l, mid, lc);
    25     else update(u, x, mid + 1, r, rc);
    26     ld[k] = (ld[lc] >= mid - l + 1)? ld[lc] + ld[rc]: ld[lc];
    27     rd[k] = (rd[rc] >= r - mid)? rd[rc] + rd[lc]: rd[rc];
    28     md[k] = rd[lc] == 0? 0: ld[rc] + rd[lc];
    29 }
    30 int query(int u, int l, int r, int k){
    31     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    32     if(u == mid) return md[k];
    33     if(u <= mid) {
    34         int q1 = query(u, l, mid, lc);
    35         if(rd[lc] >= mid - u + 1) q1 += ld[rc];
    36         return q1;
    37     }
    38     else{
    39         int q2 = query(u, mid + 1, r, rc);
    40         if(ld[rc] >= u - mid) q2 += rd[lc];
    41         return q2;
    42     }
    43 }
    44 int main(){
    45     while(scanf("%d %d", &n, &m) != EOF){
    46         while(!s.empty()) s.pop();
    47         build(1, n, 1);
    48         for(int i = 1; i <= n; i++) ok[i] = 1;
    49         while(m--){
    50             char ch;
    51             scanf(" %c", &ch);
    52             if(ch == 'D'){
    53                 int u;
    54                 scanf("%d", &u);
    55                 if(ok[u]) update(u, 0, 1, n, 1);
    56                 ok[u] = 0;
    57                 s.push(u);
    58             }
    59             else if(ch == 'Q'){
    60                 int u;
    61                 scanf("%d", &u);
    62                 int tmans = query(u, 1, n, 1);
    63                 printf("%d
    ", tmans);
    64             }
    65             else{
    66                 if(!s.empty()){
    67                     int u = s.top();
    68                     s.pop();
    69                     if(!ok[u]) update(u, 1, 1, n, 1);
    70                     ok[u] = 1;
    71                 }
    72             }
    73         }
    74     }
    75 }

    题目:

    Tunnel Warfare

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


    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

     

  • 相关阅读:
    jquery选择器 之 获取父级元素、同级元素、子元素
    Jquery知识小点备注
    如何使用strace+pstack利器分析程序性能
    Mysql 多列形成主键(复合主键 )
    Javascript 查找元素
    .gitignore规则不生效的解决办法
    Qt之模型/视图(自定义按钮)
    Qt之QTableView显示富文本
    CSS3之创建透明边框三角
    Qt之QHeaderView自定义排序(获取正确的QModelIndex)
  • 原文地址:https://www.cnblogs.com/bolderic/p/7302645.html
Copyright © 2011-2022 走看看