hdu 1540 Tunnel Warfare (区间线段树(模板))

http://acm.hdu.edu.cn/showproblem.php?pid=1540

Tunnel Warfare

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

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
 
【题解】:
区间线段树树:
节点:
struct Nod
{
    int l,r;   //左右
    int ll,ml,rl;   //左边开始连续的最大长度、中间的最大长度、右边开始最大的连续长度(对没摧毁的村庄来说)
}node[N<<2];
建树和更新应该是没问题的
下面我说下查询:
int query(int id,int p)  //传入参数
{
    if(node[p].l==node[p].r||node[p].ml==0||node[p].ml==node[p].r-node[p].l+1)
    {
        return node[p].ml;
    }
    int mid = (node[p].l+node[p].r)>>1;
    if(id<=mid)
    {
        if(id>=node[lson].r-node[lson].rl+1)   return query(id,lson)+query(mid+1,rson);  //1
        else query(id,lson);
    }
    else
    {
        if(id<=node[rson].l+node[rson].ll-1)   return query(mid,lson)+query(id,rson);  //2
        else query(id,rson);
    }
}
附上图形解释:
 
【code】:
  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<algorithm>
  4 #include<string.h>
  5 #include<stack>
  6 #define N 50005
  7 #define lson p<<1
  8 #define rson p<<1|1
  9 
 10 using namespace std;
 11 
 12 struct Nod
 13 {
 14     int l,r;   //左右
 15     int ll,ml,rl;   //左边开始连续的最大长度、中间的最大长度、右边开始最大的连续长度(对没摧毁的村庄来说)
 16 }node[N<<2];
 17 
 18 void building(int l,int r,int p)  //建树
 19 {
 20     node[p].l = l;
 21     node[p].r = r;
 22     node[p].ll=node[p].ml=node[p].rl=r-l+1;
 23     if(l==r)    return;
 24     int mid = (l+r)>>1;
 25     building(l,mid,lson);
 26     building(mid+1,r,rson);
 27 }
 28 
 29 void update(int id,int p,int ok)
 30 {
 31     if(node[p].l==node[p].r)
 32     {
 33         if(ok==0)   node[p].ll=node[p].ml=node[p].rl=0;
 34         else node[p].ll=node[p].ml=node[p].rl=1;
 35         return ;
 36     }
 37     int mid = (node[p].l+node[p].r)>>1;
 38     if(id<=mid)     update(id,lson,ok);
 39     else update(id,rson,ok);
 40 
 41     node[p].ml = max(node[lson].ml,node[rson].ml);
 42     node[p].ml = max(node[p].ml,node[lson].rl+node[rson].ll);  //最大中间值是 左右孩子的中间值 与 左边右最大+右边左最大 三者的最大值
 43 
 44     node[p].ll = node[lson].ll;  //左孩子的左最大
 45     node[p].rl = node[rson].rl;  //右孩子的右最大
 46 
 47     if(node[lson].ll==node[lson].r-node[lson].l+1)      node[p].ll+=node[rson].ll;
 48     if(node[rson].rl==node[rson].r-node[rson].l+1)      node[p].rl+=node[lson].rl;
 49 }
 50 
 51 int query(int id,int p)
 52 {
 53     if(node[p].l==node[p].r||node[p].ml==0||node[p].ml==node[p].r-node[p].l+1)
 54     {
 55         return node[p].ml;
 56     }
 57     int mid = (node[p].l+node[p].r)>>1;
 58     if(id<=mid)
 59     {
 60         if(id>=node[lson].r-node[lson].rl+1)   return query(id,lson)+query(mid+1,rson);  //1
 61         else query(id,lson);
 62     }
 63     else
 64     {
 65         if(id<=node[rson].l+node[rson].ll-1)   return query(mid,lson)+query(id,rson);  //2
 66         else query(id,rson);
 67     }
 68 }
 69 
 70 int main()
 71 {
 72     int n,m;
 73     while(~scanf("%d%d",&n,&m))
 74     {
 75         char op[5];
 76         int id;
 77         stack<int> stk;
 78         building(1,n,1);
 79         while(m--)
 80         {
 81             scanf("%s",op);
 82             if(op[0]=='D')
 83             {
 84                 scanf("%d",&id);
 85                 stk.push(id);
 86                 update(id,1,0);
 87             }
 88             else if(op[0]=='Q')
 89             {
 90                 scanf("%d",&id);
 91                 printf("%d
",query(id,1));
 92             }
 93             else if(op[0]=='R')
 94             {
 95                 id = stk.top();
 96                 stk.pop();
 97                 update(id,1,1);
 98             }
 99         }
100         while(!stk.empty()){stk.pop();}
101     }
102     return 0;
103 }
原文地址:https://www.cnblogs.com/crazyapple/p/3231958.html