码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-09-28 01:36:50      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:miss   monthly   图片   names   field   cat   单点更新   man   cte   

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

 

 

区间合并 。。。。明天补。。

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

标签:miss   monthly   图片   names   field   cat   单点更新   man   cte   

原文地址:https://www.cnblogs.com/Fighting-sh/p/9716233.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!