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. laravel学习文档

    https://github.com/barryvdh/laravel-debugbar Laravel 精选资源大全 http://laravelacademy.org/post/153.html ...

  2. uml设计之多重性

    ---------------------------------------------------------------------------------------------------- ...

  3. phonegap支付宝2.0移动快捷支付插件IOS版

    坑爹的支付宝,一两年都没有更新sdk了,这两天突然更新sdk,而且更新的变化特别大,所以只能对之前的支付宝快捷支付插件重新写了一遍. 这样既顺应了支付宝的更新,同时也支持了ios8. 废话少说,集成过 ...

  4. bzoj1191 超级英雄

    Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回 ...

  5. Servlet身份验证过滤器

    文件:index.html 文件:MyFilter.java 文件:MyServlet.java 文件:web.xml

  6. @RequestBody对象为空,异常Required request body is missing错误解决

    1.异常 org.springframework.http.converter.HttpMessageNotReadableException: Required request body is mi ...

  7. APP上线前,如何做运营推广工作?

    http://www.cocoachina.com/market/20150723/12731.html 一 竞品分析 1.选择竞品,做好定位(选择两个产品最好,最多三个). 如何获取竞品? A 百度 ...

  8. 如何创建一个非常酷的3D效果菜单

    http://www.cocoachina.com/ios/20150603/11992.html 原文地址在这里.原文 去年,读者们投票选出了Top5的iOS7最佳动画,当然也很想看到有关这些动画如 ...

  9. Python基础:09函数式编程

    Python支持一些函数式编程的特性.比如lambda. map().reduce().filter()函数. 一:匿名函数与lambda Python可以用lambda 关键字创造匿名函数.匿名函数 ...

  10. HZOJ 赤(CF739E Gosha is hunting)

    本来没有打算写题解的,时间有点紧.但是这个wqs二分看了好久才明白还是写点东西吧. 题解就直接粘dg的了: 赤(red) 本题来自codeforces 739E,加大了数据范围. 首先对一只猫不会扔两 ...