hdu 1540 Tunnel Warfare (线段树 区间合并)
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11180 Accepted Submission(s): 4389
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.
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
0
2
4
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 1e5+;
int lsum[M<<],rsum[M<<];
void pushup(int l,int r,int rt){
lsum[rt] = lsum[rt<<];
rsum[rt] = rsum[rt<<|];
mid;
if(lsum[rt] == m-l+) lsum[rt] += lsum[rt<<|];
if(rsum[rt] == r - m) rsum[rt] += rsum[rt<<];
} void update(int p,int c,int l,int r,int rt){
if(l == r){
lsum[rt] = rsum[rt] = c;
return ;
}
mid;
if(p <= m) update(p,c,lson);
else update(p,c,rson);
pushup(l,r,rt);
} void build(int l,int r,int rt){
if(l == r){
lsum[rt] = rsum[rt] = ;
return ;
}
mid;
build(lson);
build(rson);
pushup(l,r,rt);
}
int queryl(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return lsum[rt];
}
mid;
if(L > m) return queryl(L,R,rson);
if(R <= m) return queryl(L,R,lson);
int t1 = queryl(L,m,lson);
int t2 = queryl(m+,R,rson);
if(t1 == m-L+) t1+=t2;
return t1;
} int queryr(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return rsum[rt];
}
mid;
if(L > m) return queryr(L,R,rson);
if(R <= m) return queryr(L,R,lson);
int t1 = queryr(L,m,lson);
int t2 = queryr(m+,R,rson);
if(t2 == R - m) t2 += t1;
return t2;
}
stack<int>st;
int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
int n,m,x;
char op;
while(cin>>n>>m){
memset(lsum,,sizeof(lsum));
memset(rsum,,sizeof(rsum));
build(,n,);
while(m--){
cin>>op;
if(op == 'D'){
cin>>x;
update(x,,,n,);
st.push(x);
}
else if(op == 'R'){
x = st.top();
//cout<<x<<endl;
st.pop();
update(x,,,n,);
}
else{
cin>>x;
int num = queryr(,x,,n,) + queryl(x,n,,n,)-;
if(num < ) num = ;
//cout<<queryr(1,x,1,n,1)<<" "<<queryl(x,n,1,n,1)<<endl;
cout<<num<<endl;
}
}
}
return ;
}
hdu 1540 Tunnel Warfare (线段树 区间合并)的更多相关文章
- HDU 1540 Tunnel Warfare 线段树区间合并
Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...
- hdu 1540 Tunnel Warfare 线段树 区间合并
题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2. ...
- hdu 1540 Tunnel Warfare(线段树区间统计)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
Tunnel Warfare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- Tunnel Warfare 线段树 区间合并|最大最小值
B - Tunnel WarfareHDU - 1540 这个有两种方法,一个是区间和并,这个我个人感觉异常恶心 第二种方法就是找最大最小值 kuangbin——线段树专题 H - Tunnel Wa ...
- HDU 1540 Tunnel Warfare (线段树)
Tunnel Warfare Problem Description During the War of Resistance Against Japan, tunnel warfare was ca ...
- HDU 1540 Tunnel Warfare (线段树)
题目大意: n 个村庄排列在一条直线上,相邻的村庄有地道连接,除首尾两个村庄外,其余村庄都有两个相邻的村庄.其中有 3 中操作 D x :表示摧毁编号为 x 的村庄,Q x:表示求出包含村庄 x 的最 ...
- HDU 1540 Tunnel Warfare (线段树或set水过)
题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 析:首先可以用set水过,set用来记录每个被破坏的村庄,然后查找时,只要查找左右两个端点好. 用线段 ...
- HDU1540 Tunnel Warfare —— 线段树 区间合并
题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...
- HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...
随机推荐
- Convolutional Networks for Images,Speech,and Time-series
Convolutional Networks for Images,Speech,and Time-series Yann LeCun Yoshua Bengio 1995年的 1引言 多层BP网络 ...
- Kafka下的生产消费者模式与订阅发布模式
原文:https://blog.csdn.net/zwgdft/article/details/54633105 在RabbitMQ下的生产消费者模式与订阅发布模式一文中,笔者以“数据接入”和“事 ...
- Newtonsoft.Json.Linq对象读取DataSet数据
Newtonsoft.Json.Linq对象读取DataSet数据: private void button4_Click(object sender, EventArgs e) { ...
- Android Studio com.android.support:percent 导入错误 - 转
看第一行代码(第二版的)书,讲了一个关于PercentFrameLayout和PercentRelativeLayout的部分,书上在build.gradle中导入了com.android.suppo ...
- EZ 2018 05 01 NOIP2018 模拟赛(十一)
莫名其妙暴涨Rating 其实题目都挺好挺简单的,但是越简单就越容易ZZ 不理解问什么第一题这么多人找环 不过T2是真心细节题,T3太难了 题目戳这里 T1 仔细分析题意发现那个交换规则就是废话,如果 ...
- 为什么要进行阿里云云计算助理工程师认证(ACA)
阿里云助理工程师认证(ACA - Alibaba Cloud Certification Associate)是面向使用阿里云基础产品的专业技术认证,主要涉及阿里云的计算.存储.网络.安全类的核心产品 ...
- VS中为非控制台程序提供控制台输出窗口
/************************************************************************/ /* 模块名:ConsoleAdapter 文件名 ...
- [CF1019C]Sergey's problem[构造]
题意 找出一个集合 \(Q\),使得其中的点两两之间没有连边,且集合中的点可以走不超过两步到达其他所有不在集合中的点.输出任意一组解. \(n\leq 10^6\) 分析 考虑构造,先从 \(1\) ...
- Shiro安全框架学习笔记
一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和JavaEE项目中都可以使用.它主要用来处理身份认证,授权,企业会话管理 ...
- 虚拟机console基础环境部署——安全加固
1. 概述 安全是一个重要的课题.广义上可以总结为: 主机安全 网络安全 信息安全 数据安全 虽然console已经是最小化安装,但是这并不能说明console就已经安全了.之前的博客对console ...