Tunnel Warfare

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
题目大意:有n个村庄,初始的时候都连通,有m次操作,每一次不同的操作会改变村庄的连通情况或者是查询某个村庄的情况。
思路:啊!真的是蠢到家了,一开始想的是用线段树+二分来做,后头想了一想好像有点不对劲,明明线段树就是一次二分了,为啥还要加一重二分呢?。。。(我怕真是个傻子)
  后来借鉴了一下大佬的博客https://blog.csdn.net/libin56842/article/details/14105071 哎呀,用了肥虎之力,总算是把这个题给ac了。
  总的来说,在进行每一次点更新的时候我们维护三个值,对于当前的这个点,维护左边最大连续的区间,右边最大的连续区间,和当前最大的连续区间,在点更新的时候不断更新父亲节点的区间即可
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<stack> using namespace std;
const int maxn = ;
struct node{
int l,r;
int ls,rs,ms;//ls左端最大连续区间,rs右边最大连续区间,ms最大连续区间
}sum[maxn<<];
void build(int l,int r,int rt)
{
sum[rt].l=l;sum[rt].r=r;
sum[rt].ls=sum[rt].rs=sum[rt].ms=(r-l+);
if(l!=r){
int mid = (l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
}
}
void update(int L,int R,int rt)
{
if(sum[rt].l==sum[rt].r){//单点更新操作的村庄
if(R==)
sum[rt].ls = sum[rt].rs = sum[rt].ms = ;
else
sum[rt].ls = sum[rt].rs = sum[rt].ms = ;
return;
}
int mid = (sum[rt].l+sum[rt].r)>>;
if(L<=mid) update(L,R,rt<<);//向左边找
else update(L,R,rt<<|);//右边找
//更新子区间之后再更新当前的区间
sum[rt].ls = sum[rt<<].ls;
sum[rt].rs = sum[rt<<|].rs;
sum[rt].ms = max(max(sum[rt<<].ms,sum[rt<<|].ms),sum[rt<<].rs+sum[rt<<|].ls);
if(sum[rt<<].ls==sum[rt<<].r-sum[rt<<].l+)//如果左子树满了需要加上右子树的左区间
sum[rt].ls += sum[rt<<|].ls;
if(sum[rt<<|].rs==sum[rt<<|].r-sum[rt<<|].l+)//同理
sum[rt].rs += sum[rt<<].rs;
}
int query(int L,int rt)
{
if(sum[rt].l==sum[rt].r||sum[rt].ms==||sum[rt].ms==(sum[rt].r-sum[rt].l+))
return sum[rt].ms;//返回的条件
int mid = (sum[rt].l+sum[rt].r)>>;
if(L<=mid){//如果要查的点再mid的左子树
if(L>=sum[rt<<].r-sum[rt<<].rs+)return query(L,rt<<)+query(mid+,rt<<|);//左区间已满需要加上右子树的左边
else return query(L,rt<<);//左子树未满,只需要加左子树的值
}else{
if(L<=sum[rt<<|].l+sum[rt<<|].ls-)return query(L,rt<<|)+query(mid,rt<<);//同理
else query(L,rt<<|);
}
}
int main()
{
int n,m,x;char op;
while(scanf("%d%d",&n,&m)!=EOF){
stack<int>Q;
build(,n,);
while(m--){
scanf(" %c",&op);//去掉行末换行符的影响
if(op=='D'){
scanf("%d",&x);
Q.push(x);
update(x,,);
}else if(op=='Q'){
scanf("%d",&x);
printf("%d\n",query(x,));
}else if(op=='R'){
if(!Q.empty()&&x>){
update(Q.top(),,);
Q.pop();
}
}
}
}
return ;
}

HDU 1540 Tunnel Warfare (线段树)的更多相关文章

  1. hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

    Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  2. HDU 1540 Tunnel Warfare 线段树区间合并

    Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...

  3. hdu 1540 Tunnel Warfare(线段树区间统计)

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. HDU 1540 Tunnel Warfare (线段树)

    题目大意: n 个村庄排列在一条直线上,相邻的村庄有地道连接,除首尾两个村庄外,其余村庄都有两个相邻的村庄.其中有 3 中操作 D x :表示摧毁编号为 x 的村庄,Q x:表示求出包含村庄 x 的最 ...

  5. HDU 1540 Tunnel Warfare (线段树或set水过)

    题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 析:首先可以用set水过,set用来记录每个被破坏的村庄,然后查找时,只要查找左右两个端点好. 用线段 ...

  6. hdu 1540 Tunnel Warfare 线段树 区间合并

    题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2. ...

  7. hdu 1540 Tunnel Warfare 线段数区间合并

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) P ...

  8. HDU 1540 Tunnel Warfare(最长连续区间 基础)

    校赛,还有什么途径可以申请加入ACM校队?  Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/ ...

  9. hdu 1540 Tunnel Warfare (区间线段树(模板))

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...

随机推荐

  1. windows10 中微信(UWP)版本不显示通知消息

    前言: 前段时间笔者更换了升级了WINDOWS10系统,从应用商店安装微信后,使用期间不会推送消息通知,右下角的通知栏也无法添加微信图标.搜索百度和Google后,发现很多人都是这样,这是微信(UWP ...

  2. MySQL用户管理+MySQL权限管理

    我们现在默认使用的都是root用户,超级管理员,拥有全部的权限! 但是,一个公司里面的数据库服务器上面可能同时运行着很多个项目的数据库! 所以,我们应该可以根据不同的项目建立不同的用户,分配不同的权限 ...

  3. ansible Ansible Galaxy ansible-playbook 安装 使用 命令 笔记 生成密钥 管控机 被管控机 wget epel源

    笔记 ansible 安装 与salt对比 相同 都是为了同时在多台机器上执行相同的命令 都是python开发 不同 agent(saltstack需要安装.ansible不需要) 配置(salt配置 ...

  4. python爬虫:一些爬虫常用的技巧

    1.基本抓取网页 get方法 import urllib2 url = "http://www.baidu.com" response = urllib2.urlopen(url) ...

  5. 封装:PDO与MySQL之间的无缝切换

    以下的例子是将MySQL和PDO封装好,再无缝切换: 文件目录: config.php文件:   <?php return array( // 数据库配置 'DB' => array( ' ...

  6. python系列之(5)PyMySQL的使用

    简介 PyMySQL是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中是使用mysqldb. 安装 pip3 install pymysql 创建连接 #!/usr ...

  7. 【时光回溯】【JZOJ3568】【GDKOI2014】小纪的作业题

    题目描述 输入 输出 有M行,每个询问一行,输出结果mod 1,000,000,007的值. 样例输入 10 3 3 5 1 2 3 1 3 5 2 1 7 9 3 9 2 3 样例输出 10 19 ...

  8. springboot 自定义starter之AutoConfiguration【原】

    八.自定义starter AutoConfiguration: 1.这个场景需要使用到的依赖是什么? 没有特别依赖的配置 2.如何编写自动配置 @Configuration //指定这个类是一个配置类 ...

  9. hdu1403 后缀数组

    比较简单的应用. #include <stdio.h> #include <string.h> #define maxn 200002 int wa[maxn],wb[maxn ...

  10. oracle表内连接和外连接

    n  概述 表连接分为内连接和外连接 n  内连接 内连接实际上就是利用where子句对两张表形成的笛卡尔集进行筛选,我们前面学习的查询都是内连接,也是在开发过程中用的最多的连接查询. 基本语法: s ...