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. AJAX XML 实例

    AJAX XML 实例 下面的例子将演示网页如何使用 AJAX 来读取来自 XML 文件的信息 <!DOCTYPE html> <html> <head> < ...

  2. ROS-RouterOS 的license注册级别

    原文: https://wiki.mikrotik.com/wiki/Manual:CHR#CHR_Licensing https://wiki.mikrotik.com/wiki/Manual:Li ...

  3. Redis 密码设置和查看密码

    Redis 密码设置和查看密码 redis没有实现访问控制这个功能,但是它提供了一个轻量级的认证方式,可以编辑redis.conf配置来启用认证. 1.初始化Redis密码: 在配置文件中有个参数: ...

  4. CSS源码之纯css3制作的哆啦a梦图片

    本文章向大家介绍一个纯css3制作的哆啦a梦图像,主要巧妙的使用了css3的border-radius属性,需要的朋友介意参考一下本文章的源码. 效果图: 源码 <!doctype html&g ...

  5. Java反射 - 简单的给Bean赋值和取值

    由于项目的实际需要,所以利用java反射原理写了一个简单给bean赋值和取值通用的类,在此记录下方便自己日后用到,也为需要的兄弟提供个参考例子. 工具类BeanRefUtil:   package c ...

  6. 内置锁(二)synchronized下的等待通知机制

    一.等待/通知机制的简介 线程之间的协作:   为了完成某个任务,线程之间需要进行协作,采取的方式:中断.互斥,以及互斥上面的线程的挂起.唤醒:如:生成者--消费者模式.或者某个动作完成,可以唤醒下一 ...

  7. 20165233 2017-2018-2 《Java程序设计》第八周学习总结

    20165233 2017-2018-2 <Java程序设计>第八周学习总结 教材学习内容总结 基础:Java中的线程,Thread类与线程的创建 - 线程是比进程更小的单位. - JVM ...

  8. UVA548

    题意: 根据二叉树中序和后序建立二叉树,从根结点开始计算和到叶子结点,输出总和最小的叶子结点,如果有俩个和一样大,输出叶子结点最小的 AC:80ms #include<stdio.h> # ...

  9. chrome浏览器控制台 console不打印信息问题解决办法。

    转自:https://blog.csdn.net/wang17866603359/article/details/79083776 最近换了安装chrome,想按F12调试下代码,发现控制台什么信息都 ...

  10. myeclipse 保存失败

    Save FailedCompilation unit name must end with .java, or one of the registered Java-like extensions ...