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. 服务注册发现Eureka之一:Spring Cloud Eureka的服务注册与发现

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...

  2. 学习笔记之XML

    什么是QName - Benjieming_Wang的专栏 - CSDN博客 http://blog.csdn.net/Benjieming_Wang/article/details/5959961 ...

  3. php使用ffmpeg向视频中添加文字字幕

    这篇文章主要介绍了PHP使用ffmpeg给视频增加字幕显示的方法,实例分析了php操作ffmpeg给视频增加字母的技巧,具有一定参考借鉴价值,需要的朋友可以参考下. 本文实例讲述了PHP使用ffmpe ...

  4. 深度学习中的Normalization模型

    Batch Normalization(简称 BN)自从提出之后,因为效果特别好,很快被作为深度学习的标准工具应用在了各种场合.BN 大法虽然好,但是也存在一些局限和问题,诸如当 BatchSize ...

  5. 使用promise对象封装一个ajaxGet函数

    function promiseAjax(url,data){        var pro = new Promise(function(success,failed){           承诺一 ...

  6. tcp 大文件上传 ,切换目录 及登陆文件加盐处理

    实现大文件的传输 服务器 import socketimport jsonimport structsk = socket.socket()sk.bind(("127.0.0.1" ...

  7. redis存储对象

      redis主要存储类型最常用的五种数据类型: String Hash List Set Sorted set redis存储对象序列化和反序列化 首先来了解一下为什么要实现序列化 为什么要实现序列 ...

  8. 在IDEA下使用Spring Boot的热加载(Hotswap)

    你是否遇到过这样的困扰: 当你写完一段代码后,要看到效果,必须点击IDEA的停止按钮,然后再次重启启动项目,你是否觉得这样很烦呢? 如果你觉得很烦,本文就是用来解决你的问题的. 所谓热加载,就是让我们 ...

  9. uva-10054-欧拉回路

    题意:一个项链上面的每一个珠子有俩种颜色,前面一个珠子后面的颜色和后面珠子的前面颜色一样,有一天它断了, 一个人去搜集,问,搜集到的珠子能不能再次串成项链 原以为是链表,原来链表这组数据过不了. 71 ...

  10. UCenter 的目录结构

      以下关于文件的阐述以及代码的样例,均以 PHP 程序为例. UCenter 的目录结构 UCenter 分为服务端和客户端 2 个部分.服务端目录为“upload/”,客户端目录为“client/ ...