Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3293    Accepted Submission(s): 1272

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
 
Source
 
Recommend
LL
 
题意:
是一条线上的点,D x是破坏这个点,Q x是表示查询以x所在的最长的连续的点的个数,R是恢复上一次破坏的点。

思路:
这是一道典型的线段树区间统计类题目。用线段树维护两个值ml,mr。ml表示结点对应区间左连续点(即连通村子)的个数。mr区间右连续点的个数。对于D和R操作都很简单。单点修改递归更新就行了。对于R操作用一个栈记录摧毁村子的标号就行。有点难的是询问操作。就是询问一个点所在连续区间元素的个数。
一个点对应于线段树区间只有3种情况。
1.在左连续区间内。
2.在右连续区间内。
3.夹在这两个区间内。
分情况处理就好。
只是要注意。这题有点坑。discuss里面说
1)多case
(2)某个村庄可以被毁坏多次(必须全部入栈),但只需要一次就能将其恢复(下面的这组数据,,)
(3)D 3 D 2 D 1 D 1 D 2
R 恢复2
R 恢复1
R 恢复3

其实根本不用。这么做反而wa了。不用考虑那么多D就直接入栈。R就直接出栈就行了。

详细见代码:
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=50010;
int ml[maxn<<2],mr[maxn<<2],sta[maxn];//sta为栈
int flag,tail;
void btree(int L,int R,int k)
{
int ls,rs,mid;
ml[k]=mr[k]=R-L+1;
if(L==R)
return;
ls=k<<1;
rs=ls|1;
mid=(L+R)>>1;
btree(L,mid,ls);
btree(mid+1,R,rs);
}
void update(int L,int R,int k,int p,int op)//更新很简单
{
int ls,rs,mid;
if(L==R)
{
if(op)
ml[k]=mr[k]=0;
else
ml[k]=mr[k]=1;
return;
}
ls=k<<1;
rs=ls|1;
mid=(L+R)>>1;
if(p>mid)
update(mid+1,R,rs,p,op);
else
update(L,mid,ls,p,op);
ml[k]=ml[ls];
mr[k]=mr[rs];
if(ml[ls]==mid-L+1)
ml[k]+=ml[rs];
if(mr[rs]==R-mid)
mr[k]+=mr[ls];
}
int qu(int L,int R,int k,int p)
{
int ls,rs,mid,t;
if(mr[k]>=R-p+1)
{
flag=1;//加以标记。返回上层时加上扩展值
return mr[k];
}
if(ml[k]>=p-L+1)
{
flag=1;
return ml[k];
}
if(L==R)
return 0;
ls=k<<1;
rs=ls|1;
mid=(L+R)>>1;
if(p>mid)
{
t=qu(mid+1,R,rs,p);
if(flag)
{
flag=0;
t+=mr[ls];
}
}
else
{
t=qu(L,mid,ls,p);
if(flag)
{
flag=0;
t+=ml[rs];
}
}
return t;
}
int main()
{
int n,m,p,i;
char com[10]; while(~scanf("%d%d",&n,&m))
{
btree(1,n,1);
flag=tail=0;
for(i=0;i<m;i++)
{
scanf("%s",com);
switch(com[0])
{
case 'D':
scanf("%d",&p);
update(1,n,1,p,1);
sta[tail++]=p;
break;
case 'Q':
scanf("%d",&p);
flag=0;//注意flag初始化!
printf("%d\n",qu(1,n,1,p));
break;
case 'R':
update(1,n,1,sta[--tail],0);
}
}
}
return 0;
}
/*
3 100
D 3
D 1
D 2
R
Q 1
Q 2
Q 3
R
Q 1
Q 2
Q 3
*/

hdu 1540 Tunnel Warfare(线段树区间统计)的更多相关文章

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

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

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

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

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

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

  4. HDU 1540 Tunnel Warfare (线段树)

    Tunnel Warfare Problem Description During the War of Resistance Against Japan, tunnel warfare was ca ...

  5. HDU 1540 Tunnel Warfare (线段树)

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

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

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

  7. Tunnel Warfare 线段树 区间合并|最大最小值

    B - Tunnel WarfareHDU - 1540 这个有两种方法,一个是区间和并,这个我个人感觉异常恶心 第二种方法就是找最大最小值 kuangbin——线段树专题 H - Tunnel Wa ...

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

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

  9. HDU1540 Tunnel Warfare —— 线段树 区间合并

    题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...

随机推荐

  1. 【前端】CSS雪碧

    百度百科:http://baike.baidu.com/link?url=jblMCCF77bq7egbJ-9SudRmvXdwlQVVOq5D9MEEniQgJR-Lqanfrnjzwmgu7ato ...

  2. MVC 扩展 Html.ImageFor

    Asp.Net MVC 扩展 Html.ImageFor 方法详解 背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelpe ...

  3. 17.1.1 How to Set Up Replication

    17.1.1 How to Set Up Replication 17.1.1.1 Setting the Replication Master Configuration 17.1.1.2 Sett ...

  4. linux mount挂载设备(u盘,光盘,iso等 )使用说明

    对于新手学习,mount 命令,一定会有很多疑问.其实我想疑问来源更多的是对linux系统本身特殊性了解问题. linux是基于文件系统,所有的设备都会对应于:/dev/下面的设备.如: [cheng ...

  5. Nginx 因 Selinux 服务导致无法远程訪问

    文章来源:http://blog.csdn.net/johnnycode/article/details/41947581 2014-12-16日 昨天晚上处理好的网络訪问连接.早晨又訪问不到了. 现 ...

  6. linux BC命令行计算器

    1. 基本使用: $ bc <<< 5*4 20 $ bc <<< 5+4 9 $ bc <<< 5-4 1 或者 $ echo "5* ...

  7. c语言,链表

    #include "stdafx.h" #include <stdio.h> #include <stdlib.h> struct Node{ struct ...

  8. 介绍 32 位和 64 位版本的 Microsoft Office 2010

    在使用 64 位版本的 Office 2010 运行现有解决方案时存在两个基本问题: Office 2010 中的本机 64 位进程无法加载 32 位二进制文件.在使用现有 Microsoft Act ...

  9. nginx 解决400 bad request 的方法

    nginx的400错误比较难查找原因,因为此错误并不是每次都会出现的,另外,出现错误的时候,通常在浏览器和日志里看不到任何有关提示. 经长时间观察和大量试验查明,此乃request header过大所 ...

  10. PB数据管道

    数据管道提供了一种不同数据库之间传递数据和(或)表结构的方法. 数据管道对象 要完毕数据管道的功能须要提供例如以下内容: 须要数据源和目标数据库,并可以和这两个数据库正常联接 须要源数据库中的哪些表: ...