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. [转]连连看游戏 C#

    源代码下载地址 http://files.cnblogs.com/files/z5337/%E8%BF%9E%E8%BF%9E%E7%9C%8B%E6%B8%B8%E6%88%8F.rar 代码由 & ...

  2. 在Linux 系统 Latex安装 使用入门教程

    来源: http://blog.chinaunix.net/u/25605/showart_2100398.html 入门介绍好文:TeX.LaTeX.TeXLive 小结 笔记详情:http://v ...

  3. Openstack kvm win7镜像制作

    本文地址http://www.cnblogs.com/tcicy/p/7790956.html 网上找了很多为openstack制作win7镜像的文章,总是不成功 自己写一下,以便大家查看. 我使用c ...

  4. openVswitch(OVS)源代码分析之工作流程(数据包处理)

    上篇分析到数据包的收发,这篇开始着手分析数据包的处理问题.在openVswitch中数据包的处理是其核心技术,该技术分为三部分来实现:第一.根据skb数据包提取相关信息封装成key值:第二.根据提取到 ...

  5. 国内访问Google的方法(Google学术、Google香港、Twitter等)

    通过修改host文件达到访问Google等国外网址的目的 打开下面网址,里面会定期更新host文件,而且有详细的方法 https://laod.cn/hosts/2017-google-hosts.h ...

  6. JavaScript中的数组和字符串

    知识内容: 1.JavaScript中的数组 2.JavaScript中的字符串 一.JavaScript中的数组 1.JavaScript中的数组是什么 数组指的是数据的有序列表,每种语言基本上都有 ...

  7. 使用Larave5.6l提交POST请求出现The page has expired due to inactivity错误

    使用Larave5.6l提交POST请求出现The page has expired due to inactivity错误 一般是由于没有添加 csrf造成的 在表单下面的 第一个行 添加如下代码即 ...

  8. Tornado 中 PyMongo Motor MongoEngine 的性能测试

    最近在使用 Tornado 开发 API,数据库选择了 MongoDB,因为想使用 Geo 搜索的特性.Python 可供选择的 MongoDB Drivers 可以在官网查找. 在这些 Driver ...

  9. form表单提交参数封装

    function getFormValues(element,options) { var data = {}; if(element == null || element == undefined) ...

  10. JAVA JDBC 各大数据库的连接字符串和连接类

    oracle:     driverClass:oracle.jdbc.OracleDriver     url:jdbc:oracle:thin:@127.0.0.1:1521:dbname mys ...