Tunnel Warfare (区间合并|最大值最小值巧妙方法)
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
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!
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.
#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 (区间合并|最大值最小值巧妙方法)的更多相关文章
- Tunnel Warfare HDU 1540 区间合并+最大最小值
Tunnel Warfare HDU 1540 区间合并+最大最小值 题意 D x是破坏这个点,Q x是表示查询以x所在的最长的连续的点的个数,R是恢复上一次破坏的点. 题解思路 参考的大佬博客 这里 ...
- Tunnel Warfare 线段树 区间合并|最大最小值
B - Tunnel WarfareHDU - 1540 这个有两种方法,一个是区间和并,这个我个人感觉异常恶心 第二种方法就是找最大最小值 kuangbin——线段树专题 H - Tunnel Wa ...
- hdu 1540 Tunnel Warfare (区间线段树(模板))
http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...
- 求最大值最小值的方法 时间复杂度O(n)
#include<iostream> #include <iostream> #include <bitset> #include <ctime> us ...
- 【HDOJ】1540 Tunnel Warfare
还不错的一道线段树区间合并.挺巧妙的用法. /* 1540 */ #include <iostream> #include <string> #include <map& ...
- hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
Tunnel Warfare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- E - Tunnel Warfare HDU - 1540 F - Hotel G - 约会安排 HDU - 4553 区间合并
E - Tunnel Warfare HDU - 1540 对这个题目的思考:首先我们已经意识到这个是一个线段树,要利用线段树来解决问题,但是怎么解决呢,这个摧毁和重建的操作都很简单,但是这个查询怎么 ...
- HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Lim ...
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
随机推荐
- red hat官方的rhel操作系统版本号与内核版本号的对应关系
原文在如下网址:https://access.redhat.com/articles/3078 The tables below list the major and minor Red Hat En ...
- linux中nmcli命令使用及网络配置
nmcli命令与配置文件对应关系 主机名: 如果说你没有设置主机名的话,默认是localhost.localdomain 修改配置文件的主机名 # hostnamectl set-hostname ...
- centos 7.5 安装mongodb
MongoDB安装和启动 从官网下载最新对应的版本然后解压,本文以3.6.9为例,将文件拷贝到opt目录下,然后解压: [root@localhost opt]# tar zxvf mongodb-l ...
- Android logcat命令详解
一.logcat命令介绍 1.android log系统 2.logcat介绍 logcat是android中的一个命令行工具,可以用于得到程序的log信息 log类是一个日志类,可以在代码中使用lo ...
- linux设置iptables防火墙的详细步骤(centos防火墙设置方法)
CentOS系统也是基于linux中的它的防火墙其实就是iptables了,下面我来介绍在CentOS防火墙iptables的配置教程,希望此教程对各位朋友会有所帮助. iptables是与Lin ...
- PHP学习方法总结
怎样快速学好PHP技术 PHP学习方法总结 怎样快速学好PHP技术?我想这应该是大多数参加PHP培训学习PHP的同学比较关心和想要知道的问题,今天扣丁学堂小编就给大家简单谈谈怎样快速学好PHP技 ...
- java的super和this关键字用法总结
------super关键字------ super用途:在子类中访问超类“被隐藏的成员变量(无论是否静态)和静态方法”以及“被重写的实例方法”.这里的超类必须是“直接 ...
- angularjs探秘<一>认识angularjs
首先聊聊angularjs是啥. 首先AngularJS 是一个 JavaScript 框架.(PS:其实就是外部引用的js文件) 所以AngularJS的使用依然是外部引用js文件. 附上引用地址 ...
- 杂: PYTHON上数据储存:推荐h5py
一篇很短的小短文,主要推荐下做科学计算是大量数据的储存问题 最近在做一个CNN的项目,文件夹里有20w张图片要读入并保存到一个data文件(不然每次都读20w文件太麻烦). 折腾了一个下午,发现了一个 ...
- C#中的Attribute详解(下)
原文地址:https://blog.csdn.net/xiaouncle/article/details/70229119 C#中的Attribute详解(下) 一.Attribute本质 从上篇里我 ...