• Tunnel Warfare HDU


    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

    题解:每个区间有三个特征值:①以左端点为起始位置的最长连续段(全1),②以右端点为起始位置的最长连续段(全1),③该区间最长的连续段(全1);
    更新操作好做,注意一下当Tree[root].ls=(Tree[lson].r-Tree[lson].l+1)的时候表明,Tree[rson].ls也可以加在Tree[root].ls上。
    重点是查询,个人觉得不太好理解。
     1 #pragma warning(disable:4996)
     2 #include<cstdio>
     3 #include<stack>
     4 #include<cmath>
     5 #include<string>
     6 #include<cstring>
     7 #include<iostream>
     8 #include<algorithm>
     9 using namespace std;
    10 
    11 #define lson root<<1
    12 #define rson root<<1|1
    13 #define ll long long 
    14 
    15 const int maxn = 200005;
    16 
    17 int n, q;
    18 
    19 struct node {
    20     int l, r;
    21     int ls, rs, Max;
    22 }Tree[maxn];
    23 
    24 void Pushup(int root) {
    25     Tree[root].ls = Tree[lson].ls;
    26     Tree[root].rs = Tree[rson].rs;
    27     Tree[root].Max = max((Tree[lson].rs + Tree[rson].ls), max(Tree[lson].Max, Tree[rson].Max));
    28     if (Tree[lson].ls == (Tree[lson].r - Tree[lson].l + 1)) Tree[root].ls += Tree[rson].ls;
    29     if (Tree[rson].rs == (Tree[rson].r - Tree[rson].l + 1)) Tree[root].rs += Tree[lson].rs;
    30 }
    31 
    32 void Build(int l, int r, int root) {
    33     Tree[root].l = l;
    34     Tree[root].r = r;
    35     if (l == r) {
    36         Tree[root].Max = Tree[root].ls = Tree[root].rs = 1;
    37         return;
    38     }
    39     int mid = (l + r) >> 1;
    40     Build(l, mid, lson);
    41     Build(mid + 1, r, rson);
    42     Pushup(root);
    43 }
    44 
    45 void Update(int k, int l, int r, int root, int x) {
    46     if (l == r) {
    47         Tree[root].Max = Tree[root].ls = Tree[root].rs = x;
    48         return;
    49     }
    50     int mid = (l + r) >> 1;
    51     if (k <= mid) Update(k, l, mid, lson, x);
    52     else Update(k, mid + 1, r, rson, x);
    53     Pushup(root);
    54 }
    55 
    56 int Query(int k, int l, int r, int root) {
    57     if (l == r || Tree[root].Max == 0 || Tree[root].Max == (r - l + 1)) return Tree[root].Max;
    58     int mid = (l + r) >> 1;
    59     if (k <= mid) {
    60         if (Tree[lson].rs >= (mid - k + 1)) return Tree[lson].rs + Tree[rson].ls;
    61         else return Query(k, l, mid, lson);
    62     }
    63     else {
    64         if (Tree[rson].ls >= (k - mid)) return Tree[rson].ls + Tree[lson].rs;
    65         else return Query(k, mid + 1, r, rson);
    66     }
    67 }
    68 
    69 int main()
    70 {
    71     while (scanf("%d%d", &n, &q) != EOF) {
    72         Build(1, n, 1);
    73 
    74         stack<int> s;
    75 
    76         char op[2];
    77         for (int i = 1; i <= q; i++) {
    78             scanf("%s", op);
    79             if (op[0] == 'D') {
    80                 int x;
    81                 scanf("%d", &x);
    82                 s.push(x);
    83                 Update(x, 1, n, 1, 0);
    84             }
    85             else if (op[0] == 'R') {
    86                 if (s.empty()) continue;
    87                 int x = s.top();
    88                 s.pop();
    89                 Update(x, 1, n, 1, 1);
    90             }
    91             else {
    92                 int x;
    93                 scanf("%d", &x);
    94                 printf("%d
    ", Query(x, 1, n, 1));
    95             }
    96         }
    97     }
    98     return 0;
    99 }





  • 相关阅读:
    在存储过程中如何拼接字符串参数
    NHibernate学习(二)测试NHibernate如何工作
    GridView模板中用Checkbox实现全选删除
    自定义DataList,GridView通过DataBinder.Eval方法绑定的数据
    Asp.net中使用基于角色role的Forms验证
    GridView中对数据编辑打开一个新页面
    NHibernate学习()在NHibernate框架新建项目与配置文件
    Select for update nowait
    Oracle:Virtual Columns in Oracle Database 11g Release 1
    Oracle:PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/8857150.html
Copyright © 2020-2023  润新知