Tunnel Warfare (区间合并|最大值最小值巧妙方法)

Tunnel Warfare

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

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


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
 
注意是多组数据。
这是用最大值最小值单点更新的方法,参考大佬的博客 https://blog.csdn.net/chudongfang2015/article/details/52133243
一开始设所有村庄最大值为0,最小值为n+1.
当村庄被摧毁时,它的最大值和最小值设为改村庄的编号,这样我们从左边区间查询最大值max,右边区间查询最小值min
当min==max时,就是该点被摧毁了,否则区间长度就是min-max-1
 
  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<algorithm>
  5 #include<queue>
  6 #include<vector>
  7 #include<string>
  8 #include<stack>
  9 #define maxn 50005
 10 #define lson l,mid,rt<<1
 11 #define rson mid+1,r,rt<<1|1
 12 using namespace std;
 13 
 14 int n,m;
 15 struct sair{
 16     int Max,Min;
 17 }tree[maxn<<3];
 18 
 19 void build(int l,int r,int rt){
 20     if(l==r){
 21         tree[rt].Max=0;
 22         tree[rt].Min=n+1;
 23         return;
 24     }
 25     int mid=(l+r)/2;
 26     build(lson);
 27     build(rson);
 28     tree[rt].Max=max(tree[rt<<1].Max,tree[rt<<1|1].Max);
 29     tree[rt].Min=min(tree[rt<<1].Min,tree[rt<<1|1].Min);
 30 
 31 }
 32 
 33 void update_max(int L,int k,int l,int r,int rt){
 34     if(l==r){
 35         tree[rt].Max=k;
 36         return;
 37     }
 38     int mid=(l+r)/2;
 39     if(L<=mid) update_max(L,k,lson);
 40     else update_max(L,k,rson);
 41     tree[rt].Max=max(tree[rt<<1].Max,tree[rt<<1|1].Max);
 42 }
 43 
 44 void update_min(int L,int k,int l,int r,int rt){
 45     if(l==r){
 46         tree[rt].Min=k;
 47         return;
 48     }
 49     int mid=(l+r)/2;
 50     if(L<=mid) update_min(L,k,lson);
 51     else update_min(L,k,rson);
 52     tree[rt].Min=min(tree[rt<<1].Min,tree[rt<<1|1].Min);
 53 }
 54 
 55 int query_max(int L,int R,int l,int r,int rt){
 56     if(L<=l&&R>=r){
 57         return tree[rt].Max;
 58 
 59     }
 60     int mid=(l+r)/2;
 61     int ans=0;
 62     if(L<=mid) ans=max(ans,query_max(L,R,lson));
 63     if(R>mid) ans=max(ans,query_max(L,R,rson));
 64     return ans;
 65 }
 66 
 67 int query_min(int L,int R,int l,int r,int rt){
 68     if(L<=l&&R>=r){
 69         return tree[rt].Min;
 70     }
 71     int mid=(l+r)/2;
 72     int ans=0x3f3f3f3f;
 73     if(L<=mid) ans=min(ans,query_min(L,R,lson));
 74     if(R>mid) ans=min(ans,query_min(L,R,rson));
 75     return ans;
 76 }
 77 
 78 int main(){
 79 
 80     std::ios::sync_with_stdio(false);
 81     while(cin>>n>>m){
 82         char pos;
 83         int x;
 84         stack<int>st;
 85         build(1,n,1);
 86         for(int i=1;i<=m;i++){
 87             cin>>pos;
 88             if(pos=='D'){
 89                 cin>>x;
 90                 st.push(x);
 91                 update_max(x,x,1,n,1);
 92                 update_min(x,x,1,n,1);
 93             }
 94             else if(pos=='Q'){
 95                 cin>>x;
 96                 int L=query_min(x,n,1,n,1);
 97                 int R=query_max(1,x,1,n,1);
 98                 if(R==L) cout<<0<<endl;
 99                 else cout<<L-R-1<<endl;
100             }
101             else if(pos=='R'){
102                 x=st.top();
103                 st.pop();
104                 update_max(x,0,1,n,1);
105                 update_min(x,n+1,1,n,1);
106             }
107         }
108     }
109 }
View Code

区间合并

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <cmath>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <stack>
 9 #include <vector>
10 #include <set>
11 #include <map>
12 #define maxn 50010
13 #define lson l,mid,rt<<1
14 #define rson mid+1,r,rt<<1|1
15 using namespace std;
16 
17 int tree[maxn<<3],lsum[maxn<<3],rsum[maxn<<3];
18 //总数,左节点向右的连续个数,右节点向左的连续个数
19 
20 void pushup(int len,int rt){
21     lsum[rt]=lsum[rt<<1];
22     if(lsum[rt]==(len-len/2)) lsum[rt]+=lsum[rt<<1|1];
23     rsum[rt]=rsum[rt<<1|1];
24     if(rsum[rt]==len/2) rsum[rt]+=rsum[rt<<1];
25     tree[rt]=max(lsum[rt<<1|1]+rsum[rt<<1],max(tree[rt<<1],tree[rt<<1|1]));
26 }
27 
28 void build(int l,int r,int rt){
29     if(l==r){
30         tree[rt]=lsum[rt]=rsum[rt]=1;
31         return;
32     }
33     int mid=(l+r)/2;
34     build(lson);
35     build(rson);
36     pushup(r-l+1,rt);
37 }
38 
39 void add(int L,int k,int l,int r,int rt){
40     if(l==r){
41         tree[rt]=lsum[rt]=rsum[rt]=k;
42         return;
43     }
44     int mid=(l+r)/2;
45     if(L<=mid) add(L,k,lson);
46     else add(L,k,rson);
47     pushup(r-l+1,rt);
48 }
49 
50 int query(int L,int l,int r,int rt){
51     if(l==r) return tree[rt];
52     int mid=(l+r)/2;
53     if(L<=mid){
54         if(L+rsum[rt<<1]>mid) return rsum[rt<<1]+lsum[rt<<1|1];
55         //查询该点是否在范围内
56         return query(L,lson);
57     }
58     else{
59         if(mid+lsum[rt<<1|1]>=L) return rsum[rt<<1]+lsum[rt<<1|1];
60         return query(L,rson);
61     }
62 }
63 
64 int main(){
65     std::ios::sync_with_stdio(false);
66     int n,m,x;
67     string pos;
68     while(cin>>n>>m){
69         stack<int>st;
70         build(1,n,1);
71 
72         for(int i=1;i<=m;i++){
73             cin>>pos;
74             if(pos[0]=='Q'){
75                 cin>>x;
76                 cout<<query(x,1,n,1)<<endl;
77             }
78             else if(pos[0]=='D'){
79                 cin>>x;
80                 st.push(x);
81                 add(x,0,1,n,1);
82             }
83             else{
84                 x=st.top();
85                 st.pop();
86                 add(x,1,1,n,1);
87             }
88         }
89     }
90 
91 }
92     
View Code
原文地址:https://www.cnblogs.com/Fighting-sh/p/9716233.html