HDU1540 Tunnel Warfare —— 线段树 区间合并

Posted h_z_cong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1540 Tunnel Warfare —— 线段树 区间合并相关的知识,希望对你有一定的参考价值。

题目链接:https://vjudge.net/problem/HDU-1540

 

uring 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 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 using namespace std;
 13 typedef long long LL;
 14 const double EPS = 1e-8;
 15 const int INF = 2e9;
 16 const LL LNF = 2e18;
 17 const int MAXN = 5e4+10;
 18 
 19 int n, m;
 20 int sum[MAXN<<2];
 21 
 22 void push_up(int u)
 23 {
 24     sum[u] = sum[u*2] + sum[u*2+1];
 25 }
 26 
 27 void build(int u, int l, int r)
 28 {
 29     if(l==r)
 30     {
 31         sum[u] = 1;
 32         return;
 33     }
 34 
 35     int mid = (l+r)>>1;
 36     build(u*2, l, mid);
 37     build(u*2+1, mid+1, r);
 38     push_up(u);
 39 }
 40 
 41 void set_val(int u, int l, int r, int x, int val)
 42 {
 43     if(l==r)
 44     {
 45         sum[u] = val;
 46         return;
 47     }
 48 
 49     int mid = (l+r)>>1;
 50     if(x<=mid) set_val(u*2, l, mid, x, val);
 51     else set_val(u*2+1, mid+1, r, x, val);
 52     push_up(u);
 53 }
 54 
 55 int query_left(int u, int l, int r, int x, int y)
 56 {
 57     if( x<=l && r<=y && (sum[u]==(r-l+1) || sum[u]==0) )
 58         return sum[u];
 59 
 60     int mid = (l+r)/2;
 61     if(y<=mid) return query_left(u*2, l, mid, x, y);
 62     else if(x>=mid+1) return query_left(u*2+1, mid+1, r, x, y);
 63     else
 64     {
 65         int t1 = query_left(u*2, l, mid, x, mid);
 66         int t2 = query_left(u*2+1, mid+1, r, mid+1, y);
 67         if(t2==y-mid) return t1+t2;
 68         else return t2;
 69     }
 70 }
 71 
 72 int query_right(int u, int l, int r, int x, int y)
 73 {
 74     if( x<=l && r<=y && (sum[u]==(r-l+1) || sum[u]==0) )
 75         return sum[u];
 76 
 77     int mid = (l+r)/2;
 78     if(y<=mid) return query_right(u*2, l, mid, x, y);
 79     else if(x>=mid+1) return query_right(u*2+1, mid+1, r, x, y);
 80     else
 81     {
 82         int t1 = query_right(u*2, l, mid, x, mid);
 83         int t2 = query_right(u*2+1, mid+1, r, mid+1, y);
 84         if(t1==mid-x+1) return t1+t2;
 85         else return t1;
 86     }
 87 }
 88 
 89 int main()
 90 {
 91     stack<int>S;
 92     while(scanf("%d%d", &n, &m)!=EOF)
 93     {
 94         while(!S.empty()) S.pop();
 95         build(1, 1, n);
 96         for(int i = 1; i<=m; i++)
 97         {
 98             char op[2]; int x;
 99             scanf("%s", op);
100             if(op[0]==D)
101             {
102                 scanf("%d", &x);
103                 set_val(1, 1, n, x, 0);
104                 S.push(x);
105             }
106             else if(op[0]==R)
107             {
108                 if(S.empty()) continue;
109                 set_val(1, 1, n, S.top(), 1);
110                 S.pop();
111             }
112             else
113             {
114                 scanf("%d", &x);
115                 int ans = query_left(1, 1, n, 1, x) + query_right(1, 1, n, x, n);
116                 printf("%d\n", ans?ans-1:0);
117             }
118         }
119     }
120 }
View Code

 

写法二:

技术分享
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 using namespace std;
 13 typedef long long LL;
 14 const double EPS = 1e-8;
 15 const int INF = 2e9;
 16 const LL LNF = 2e18;
 17 const int MAXN = 5e4+10;
 18 
 19 int n, m;
 20 int le[MAXN<<2], ri[MAXN<<2];
 21 
 22 void push_up(int u, int l, int r)
 23 {
 24     le[u] = le[u*2]; ri[u] = ri[u*2+1];
 25     if(le[u*2]==(r-l+1+1)/2) le[u] += le[u*2+1];
 26     if(ri[u*2+1]==(r-l+1)/2) ri[u] += ri[u*2];
 27 }
 28 
 29 void build(int u, int l, int r)
 30 {
 31     if(l==r)
 32     {
 33         le[u] = ri[u] = 1;
 34         return;
 35     }
 36 
 37     int mid = (l+r)>>1;
 38     build(u*2, l, mid);
 39     build(u*2+1, mid+1, r);
 40     push_up(u, l, r);
 41 }
 42 
 43 void set_val(int u, int l, int r, int x, int val)
 44 {
 45     if(l==r)
 46     {
 47         le[u] = ri[u] = val;
 48         return;
 49     }
 50 
 51     int mid = (l+r)>>1;
 52     if(x<=mid) set_val(u*2, l, mid, x, val);
 53     else set_val(u*2+1, mid+1, r, x, val);
 54     push_up(u,l,r);
 55 }
 56 
 57 int query_left(int u, int l, int r, int x, int y)
 58 {
 59     if(x<=l && r<=y)
 60         return ri[u];
 61 
 62     int mid = (l+r)/2;
 63     if(y<=mid) return query_left(u*2, l, mid, x, y);
 64     else if(x>=mid+1) return query_left(u*2+1, mid+1, r, x, y);
 65     else
 66     {
 67         int t1 = query_left(u*2, l, mid, x, mid);
 68         int t2 = query_left(u*2+1, mid+1, r, mid+1, y);
 69         if(t2==y-mid) return t1+t2;
 70         else return t2;
 71     }
 72 }
 73 
 74 int query_right(int u, int l, int r, int x, int y)
 75 {
 76     if(x<=l && r<=y)
 77         return le[u];
 78 
 79     int mid = (l+r)/2;
 80     if(y<=mid) return query_right(u*2, l, mid, x, y);
 81     else if(x>=mid+1) return query_right(u*2+1, mid+1, r, x, y);
 82     else
 83     {
 84         int t1 = query_right(u*2, l, mid, x, mid);
 85         int t2 = query_right(u*2+1, mid+1, r, mid+1, y);
 86         if(t1==mid-x+1) return t1+t2;
 87         else return t1;
 88     }
 89 }
 90 
 91 int main()
 92 {
 93     stack<int>S;
 94     while(scanf("%d%d", &n, &m)!=EOF)
 95     {
 96         while(!S.empty()) S.pop();
 97         build(1, 1, n);
 98         for(int i = 1; i<=m; i++)
 99         {
100             char op[2]; int x;
101             scanf("%s", op);
102             if(op[0]==D)
103             {
104                 scanf("%d", &x);
105                 set_val(1, 1, n, x, 0);
106                 S.push(x);
107             }
108             else if(op[0]==R)
109             {
110                 if(S.empty()) continue;
111                 set_val(1, 1, n, S.top(), 1);
112                 S.pop();
113             }
114             else
115             {
116                 scanf("%d", &x);
117                 int ans = query_left(1, 1, n, 1, x) + query_right(1, 1, n, x, n);
118                 printf("%d\n", ans?ans-1:0);
119             }
120         }
121     }
122 }
View Code

 













以上是关于HDU1540 Tunnel Warfare —— 线段树 区间合并的主要内容,如果未能解决你的问题,请参考以下文章

HDU1540 Tunnel Warfare —— 线段树 区间合并

hdu1540 Tunnel Warfare

hdu1540 Tunnel Warfare 线段树/树状数组

hdu--1540 Tunnel Warfare(线段树+区间合并)

线段树区间合并HDU1540-Tunnel Warfare

HDU 1540 Tunnel Warfare(线段树,单点更新,区间查询)