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
 
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#include<stack>
#define maxn 50005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std; int n,m;
struct sair{
int Max,Min;
}tree[maxn<<]; void build(int l,int r,int rt){
if(l==r){
tree[rt].Max=;
tree[rt].Min=n+;
return;
}
int mid=(l+r)/;
build(lson);
build(rson);
tree[rt].Max=max(tree[rt<<].Max,tree[rt<<|].Max);
tree[rt].Min=min(tree[rt<<].Min,tree[rt<<|].Min); } void update_max(int L,int k,int l,int r,int rt){
if(l==r){
tree[rt].Max=k;
return;
}
int mid=(l+r)/;
if(L<=mid) update_max(L,k,lson);
else update_max(L,k,rson);
tree[rt].Max=max(tree[rt<<].Max,tree[rt<<|].Max);
} void update_min(int L,int k,int l,int r,int rt){
if(l==r){
tree[rt].Min=k;
return;
}
int mid=(l+r)/;
if(L<=mid) update_min(L,k,lson);
else update_min(L,k,rson);
tree[rt].Min=min(tree[rt<<].Min,tree[rt<<|].Min);
} int query_max(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return tree[rt].Max; }
int mid=(l+r)/;
int ans=;
if(L<=mid) ans=max(ans,query_max(L,R,lson));
if(R>mid) ans=max(ans,query_max(L,R,rson));
return ans;
} int query_min(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return tree[rt].Min;
}
int mid=(l+r)/;
int ans=0x3f3f3f3f;
if(L<=mid) ans=min(ans,query_min(L,R,lson));
if(R>mid) ans=min(ans,query_min(L,R,rson));
return ans;
} int main(){ std::ios::sync_with_stdio(false);
while(cin>>n>>m){
char pos;
int x;
stack<int>st;
build(,n,);
for(int i=;i<=m;i++){
cin>>pos;
if(pos=='D'){
cin>>x;
st.push(x);
update_max(x,x,,n,);
update_min(x,x,,n,);
}
else if(pos=='Q'){
cin>>x;
int L=query_min(x,n,,n,);
int R=query_max(,x,,n,);
if(R==L) cout<<<<endl;
else cout<<L-R-<<endl;
}
else if(pos=='R'){
x=st.top();
st.pop();
update_max(x,,,n,);
update_min(x,n+,,n,);
}
}
}
}

区间合并

 #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define maxn 50010
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std; int tree[maxn<<],lsum[maxn<<],rsum[maxn<<];
//总数,左节点向右的连续个数,右节点向左的连续个数 void pushup(int len,int rt){
lsum[rt]=lsum[rt<<];
if(lsum[rt]==(len-len/)) lsum[rt]+=lsum[rt<<|];
rsum[rt]=rsum[rt<<|];
if(rsum[rt]==len/) rsum[rt]+=rsum[rt<<];
tree[rt]=max(lsum[rt<<|]+rsum[rt<<],max(tree[rt<<],tree[rt<<|]));
} void build(int l,int r,int rt){
if(l==r){
tree[rt]=lsum[rt]=rsum[rt]=;
return;
}
int mid=(l+r)/;
build(lson);
build(rson);
pushup(r-l+,rt);
} void add(int L,int k,int l,int r,int rt){
if(l==r){
tree[rt]=lsum[rt]=rsum[rt]=k;
return;
}
int mid=(l+r)/;
if(L<=mid) add(L,k,lson);
else add(L,k,rson);
pushup(r-l+,rt);
} int query(int L,int l,int r,int rt){
if(l==r) return tree[rt];
int mid=(l+r)/;
if(L<=mid){
if(L+rsum[rt<<]>mid) return rsum[rt<<]+lsum[rt<<|];
//查询该点是否在范围内
return query(L,lson);
}
else{
if(mid+lsum[rt<<|]>=L) return rsum[rt<<]+lsum[rt<<|];
return query(L,rson);
}
} int main(){
std::ios::sync_with_stdio(false);
int n,m,x;
string pos;
while(cin>>n>>m){
stack<int>st;
build(,n,); for(int i=;i<=m;i++){
cin>>pos;
if(pos[]=='Q'){
cin>>x;
cout<<query(x,,n,)<<endl;
}
else if(pos[]=='D'){
cin>>x;
st.push(x);
add(x,,,n,);
}
else{
x=st.top();
st.pop();
add(x,,,n,);
}
}
} }

Tunnel Warfare (区间合并|最大值最小值巧妙方法)的更多相关文章

  1. Tunnel Warfare HDU 1540 区间合并+最大最小值

    Tunnel Warfare HDU 1540 区间合并+最大最小值 题意 D x是破坏这个点,Q x是表示查询以x所在的最长的连续的点的个数,R是恢复上一次破坏的点. 题解思路 参考的大佬博客 这里 ...

  2. Tunnel Warfare 线段树 区间合并|最大最小值

    B - Tunnel WarfareHDU - 1540 这个有两种方法,一个是区间和并,这个我个人感觉异常恶心 第二种方法就是找最大最小值 kuangbin——线段树专题 H - Tunnel Wa ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...

  4. 求最大值最小值的方法 时间复杂度O(n)

    #include<iostream> #include <iostream> #include <bitset> #include <ctime> us ...

  5. 【HDOJ】1540 Tunnel Warfare

    还不错的一道线段树区间合并.挺巧妙的用法. /* 1540 */ #include <iostream> #include <string> #include <map& ...

  6. hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

    Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  7. E - Tunnel Warfare HDU - 1540 F - Hotel G - 约会安排 HDU - 4553 区间合并

    E - Tunnel Warfare HDU - 1540 对这个题目的思考:首先我们已经意识到这个是一个线段树,要利用线段树来解决问题,但是怎么解决呢,这个摧毁和重建的操作都很简单,但是这个查询怎么 ...

  8. HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并

    Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Lim ...

  9. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

随机推荐

  1. JQuery基础(选择器、事件、DOM操作)

    一.选择器 1.基本选择器 ①id选择器       ②class选择器       ③标签名选择 ④并列选择          ⑤后代选择 代码用法展示: <title></tit ...

  2. java学习——构造类

    package my_project; public class my_first_class { public static void main(String[] args) { // TODO A ...

  3. Jquery判断IE6等浏览器的代码

    这好像是由几篇代码接在一起的,原文均来自网络,不记得出处了~ jquery中利用navigator.userAgent.indexOf来判断浏览器类型,并进行了一下处理,如果不想使用jquery,稍为 ...

  4. Python之函数——基础篇

    函数 函数,在BASIC中,叫subroutine(子过程或子程序),在Pascal中叫做procedure(过程)和function,在C中只有function,在Java中叫method. 定义: ...

  5. Windows下OpenCV 3.1.0 在 Qt Creator 4.0.2 (Qt 5.7.0 MinGW) 中的开发环境配置

    2017-2-23 Update: 修改并添加了部分细节 最近正在学习OpenCV ,为毕业设计做准备.Windows版本的OpenCV都默认提供对VS的支持,其在VS中的配置比较简单,网上也有大批教 ...

  6. 扯皮的cplex-感觉时间不对

    import ilog.cplex.CpxMult; import ilog.cplex.CpxNumExpr; import ilog.cplex.IloCplex; import ilog.con ...

  7. docker容器中搭建kafka集群环境

    Kafka集群管理.状态保存是通过zookeeper实现,所以先要搭建zookeeper集群 zookeeper集群搭建 一.软件环境: zookeeper集群需要超过半数的的node存活才能对外服务 ...

  8. HTML5 Canvas ( 径向渐变, 升级版的星空 ) fillStyle, createRadialGradient

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 10.Action中的method属性

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 在struts1.x中我们知道通过继承DispatchAction可以实现把 ...

  10. DataSnap高级技术(7)—TDSServerClass中Lifecycle生命周期三种属性说明

    From http://blog.csdn.net/sunstone/article/details/5282666 DataSnap高级技术(7)—TDSServerClass中Lifecycle生 ...