hdu 1540 Tunnel Warfare (线段树,维护当前最大连续区间)
Description
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
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
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 题目的大概意思就是地道战。 D x 表示摧毁x点,R 表示修复最后一个被摧毁的点,Q x 表示查询包含x点的最大连续区间的长度是多少。
这题主要是维护状态比较麻烦。
对于每个节点,我们要除了储存它的左右边界,还要储存从区间左端点开始的最大连续区间长度(ls),从右端点开始的最大连续区间长度(rs),
以及当前区间内的最大的连续长度(ms)。例如0100111,ls=0,rs=3,ms=3.状态更新的细节写到代码注释里了。
代码如下:
#include <bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
#define M 50050
struct segTree
{
int l,r;
int ls,rs,ms;
}tree[M<<];
void buildTree (int rt,int l,int r)
{
tree[rt].l=l,tree[rt].r=r;
tree[rt].ls=tree[rt].rs=tree[rt].ms=r-l+;
if (l==r)
return ;
int mid=(l+r)>>;
buildTree(lson,l,mid);
buildTree(rson,mid+,r);
}
void upDate(int rt,int x,int val)
{
if (tree[rt].l==tree[rt].r)
{
if (val==)
tree[rt].ls=tree[rt].rs=tree[rt].ms=;
else
tree[rt].ls=tree[rt].rs=tree[rt].ms=;
return ;
}
int mid=(tree[rt].l+tree[rt].r)>>;
if (x<=mid)
upDate(lson,x,val);
else
upDate(rson,x,val); tree[rt].ls=tree[lson].ls;//父节点先继承左儿子的ls
tree[rt].rs=tree[rson].rs;//父节点先继承右儿子的rs tree[rt].ms=max(tree[lson].ms,tree[rson].ms);
tree[rt].ms=max(tree[rt].ms,tree[lson].rs+tree[rson].ls);
/*
父节点内当前的连续最大值是当前区间ls(1),rs(2),
与左儿子的rs右儿子ls(两部分在中间拼起来,3)三者的最大值
*/
if (tree[lson].ls==tree[lson].r-tree[lson].l+)
/*如果父节点的左儿子的ls等于左儿子的长度(满了),
则父节点的ls在继承左儿子ls的基础上还要加上右儿子的ls
例:1111 1101
lson rson
父节点.ls=lson.ls(4)+rson.ls(2)=6;
*/
tree[rt].ls+=tree[rson].ls;
if (tree[rson].rs==tree[rson].r-tree[rson].l+)
//同理如果右儿子的rs满了父节点的rs还要加上左儿子的rs
tree[rt].rs+=tree[lson].rs;
}
int query (int rt,int x)
{
if (tree[rt].l==tree[rt].r||tree[rt].ms==||tree[rt].ms==tree[rt].r-tree[rt].l+)
{
return tree[rt].ms;
//当查询到叶子节点,或当前区间ms为0或区间是满的,return
}
int mid=(tree[rt].l+tree[rt].r)>>;
if (x<=mid)
{
if (x>=tree[lson].r-tree[lson].rs+)
/* x如果在左儿子的右连续区间内
lson 1111 1101 rson
x
答案等于左儿子内包含x的区间(4)+右儿子的ls长度(2)=6
*/
return query(lson,x)+ tree[rson].ls;
else//否则只需查询左儿子
return query(lson,x);
}
else
{
if (x<=tree[rson].l+tree[rson].ls-)//x在右儿子的左连续区间内
return query(rson,x)+ tree[lson].rs;//答案等于右儿子内包含x的区间+左儿子rs
else
return query(rson,x);
}
}
int s[M],top;//模拟一个栈,来处理最后一个被破坏的区间
int main()
{
int m,n;
char o[];
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&n,&m))
{
top=;
buildTree(,,n);
while (m--)
{
scanf("%s",o);
int x;
if (o[]=='D')
{
scanf("%d",&x);
s[top++]=x;
upDate(,x,);
}
else if (o[]=='Q')
{
scanf("%d",&x);
printf("%d\n",query(,x));
}
else
{
if (x>)
{
x=s[--top];
upDate(,x,);
}
}
}
}
return ;
}
hdu 1540 Tunnel Warfare (线段树,维护当前最大连续区间)的更多相关文章
- 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 (线段树或set水过)
题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 析:首先可以用set水过,set用来记录每个被破坏的村庄,然后查找时,只要查找左右两个端点好. 用线段 ...
- hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
Tunnel Warfare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 1540 Tunnel Warfare 线段树区间合并
Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...
- 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 线段树 区间合并
题意: 三个操作符 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) P ...
- HDU 1540 Tunnel Warfare(最长连续区间 基础)
校赛,还有什么途径可以申请加入ACM校队? Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/ ...
- hdu 1540 Tunnel Warfare (区间线段树(模板))
http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...
随机推荐
- 前端每日实战:99# 视频演示如何用纯 CSS 创作一个过山车 loader
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/KBxYZg/ 可交互视频 此视频是 ...
- 测开之路五十三:unittest运行参数
Fixture:进行测试前的准备工作和测试后的清理操作.例如创建临时或是代理数据库,目录,服务进程等.用例(Case):最小的测试单元,检车特定输入的响应.TestCase作为所有用例的基类,测试ca ...
- 并发数/QPS/PV/ 服务器响应时间公示
QPS:每秒查询率(Query Per Second) ,每秒的响应请求数,也即是最大吞吐能力.QPS = req/sec = 请求数/秒QPS统计方式 [一般使用 http_load 进行统计]QP ...
- (转)关于SimpleDateFormat安全的时间格式化线程安全问题
想必大家对SimpleDateFormat并不陌生.SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调 ...
- js日历算法
页面 <div class="un1"> <h2>服务档期</h2> <div class="date-panel" ...
- 实验报告(六)&第八周学习总结
班级 计科二班 学号 20188425 姓名 IM 完成时间2019/10/18 评分等级 实验六 Java异常 实验目的 理解异常的基本概念: 掌握异常处理方法及熟悉常见异常的捕获方法. 实验要求 ...
- Cocos2d Box2D之浮动刚体
| 版权声明:本文为博主原创文章,未经博主允许不得转载. b2_kinematicBody 运动学物体在模拟环境中根据自身的速度进行移动.运动学物体自身不受力的作用.虽然用户可以手动移动它,但是通 ...
- JNDI+Tomcat配置数据源的两种方式
非全局jndi配置步骤 :此种配置方式不需要在server.xml中配置数据源,而只在tomcat/conf/Catalina/localhost下的启动配置中配置即可.注意红色字体名称必须和相同. ...
- Java_3.Java帮助文档
1.Oracle的在线帮助文档 http://docs.oracle.com/javase/8/docs/api 2.jdk源码内容(包含5个部分) [以下内容参考至:https://blog.csd ...
- 使用phpexcel导入excel文件种的时期数据时数据导入格式
在使用phpexcel导入类似于 YYYY-MM-DD HH:ii:ss格式的数据时,导入成功以后会发现导入的数据其实是类似于42085.746516204格式的数据( excel在存储时间类型的数据 ...