一、题目

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

顺便附上原题链接→_→Problem-1540

二、题目分析

和普通的线段树没什么太大差别,只是每个区间加了两个变量——lsum和rsum,分别表示这个区间左起向右连续“1”串的长度和右起向左连续“1”串的长度。在维护树回溯时,一个区间的lsum等于其左区间的lsum,rsum等于其右区间的rsum。特别的,当一个区间左区间的lsum等于其左区间长度,即该区间左区间是一个连续的“1”串时,该区间lsum需额外加上其右区间的lsum;对于其rsum同理。

三、代码实现

这题有个天坑,题目完全没提到,但这题有多组数据_(:з」∠)_

 #include<cstdio>
#include<cstring>
const int MAXN=5e4+;
int n,m;
struct node
{
int l,r;
int lsum,rsum;
}tr[MAXN<<];
int stack[MAXN],top;
void build(int x,int y,int i)
{
tr[i].l=x,tr[i].r=y;
if(x==y)
{
tr[i].lsum=;
tr[i].rsum=;
return;
}
int mid=(tr[i].l+tr[i].r)>>;
build(x,mid,i<<);
build(mid+,y,i<<|);
tr[i].lsum=y-x+,tr[i].rsum=y-x+;
return;
}
void update(int x,int i,int val)
{
if(tr[i].l==x&&tr[i].r==x)
{
tr[i].lsum=val;
tr[i].rsum=val;
return;
}
int mid=(tr[i].l+tr[i].r)>>;
if(x<=mid)
{
update(x,i<<,val); }
else
{
update(x,i<<|,val);
}
tr[i].lsum=tr[i<<].lsum;
tr[i].rsum=tr[i<<|].rsum;
if(tr[i<<].lsum==tr[i<<].r-tr[i<<].l+)tr[i].lsum+=tr[i<<|].lsum;
if(tr[i<<|].rsum==tr[i<<|].r-tr[i<<|].l+)tr[i].rsum+=tr[i<<].rsum;
}
int query_left(int x,int i)
{
if(tr[i].r==x)
{
if(tr[i].rsum==tr[i].r-tr[i].l+&&tr[i].l!=)
{
return tr[i].rsum+query_left(tr[i].l-,);
}
return tr[i].rsum;
}
int mid=(tr[i].l+tr[i].r)>>;
if(x<=mid)
{
return query_left(x,i<<);
}
else
{
return query_left(x,i<<|);
}
}
int query_right(int x,int i)
{ if(tr[i].l==x)
{
if(tr[i].lsum==tr[i].r-tr[i].l+&&tr[i].r!=n)
{
return tr[i].lsum+query_right(tr[i].r+,);
}
return tr[i].lsum;
}
int mid=(tr[i].l+tr[i].r)>>;
if(x<=mid)
{
return query_right(x,i<<);
}
else
{
return query_right(x,i<<|);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(tr,,sizeof(tr));
memset(stack,,sizeof(stack));
top=;
build(,n,);
for(int i=;i<=m;++i)
{
char c;
int x;
scanf("\n%c",&c);
if(c=='D')
{
scanf("%d",&x);
stack[++top]=x;
update(x,,);
}
else if(c=='R')
{
update(stack[top--],,);
}
else
{
scanf("%d",&x);
int ans=query_left(x,)+query_right(x,)-;
if(ans<=)printf("0\n");
else printf("%d\n",ans);
}
}
}
return ;
}

HDU1540-Tunnel Warfare

弱弱地说一句,本蒟蒻码字也不容易,转载请注明出处http://www.cnblogs.com/Maki-Nishikino/p/6230606.html

【线段树区间合并】HDU1540-Tunnel Warfare的更多相关文章

  1. Tunnel Warfare(HDU1540+线段树+区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目: 题意:总共有n个村庄,有q次操作,每次操作分为摧毁一座村庄,修复一座村庄,和查询与询问的 ...

  2. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  3. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  4. HDU 3911 Black And White(线段树区间合并+lazy操作)

    开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...

  5. HYSBZ 1858 线段树 区间合并

    //Accepted 14560 KB 1532 ms //线段树 区间合并 /* 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[ ...

  6. poj3667 线段树 区间合并

    //Accepted 3728 KB 1079 ms //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...

  7. hdu3911 线段树 区间合并

    //Accepted 3911 750MS 9872K //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...

  8. 线段树(区间合并) POJ 3667 Hotel

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

  9. HDU 3308 LCIS (线段树区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...

随机推荐

  1. easyui 分页 MVC

    View <script type="text/javascript"> var dd; var grid; $(function () { var queryData ...

  2. 申请使用aws的一些笔记

    1. 申请可以使用asw.amazon.com/cn/,这个界面虽然是中文的,但是申请的是海外的aws. 2. 审核后会收到如下的一封邮件: 3. 剩下创建EC2和RDS的过程可以参考http://w ...

  3. 一个链接引发的血案---------服务器 IO及网络流量暴涨解决历程

    在这里介绍一次因为更改网站地址而引发服务器IO读取速度,网络流入流出速度暴涨10倍的解决经历. 环境:Ubuntu + Nginx + php-cgi + Wordpress 事情是这样的,现在网站使 ...

  4. CentOS7.2 编译安装SVN1.9.5客户端

    背景 原来想在Linux机上开Samba共享,在Windows机上把工作目录映射到网络驱动器,用Source Insight编辑代码后就不用来回同步文件了. 然而在使用中发现,Windows机用的SV ...

  5. linux配置tomcat以service方式启动(转)

    在/etc/init.d目录下新建文件,命名为tomcat 对tomcat文件进行编辑. cat /etc/init.d/tomcat #!/bin/bash # description: Tomca ...

  6. iostat 命令

    iostat -x 1 10 Linux 2.6.18-92.el5xen 02/03/2009 avg-cpu: %user %nice %system %iowait %steal %idle 1 ...

  7. centos 安装redis并加入系统服务

    1.安装redis wget http://download.redis.io/releases/redis-3.2.5.tar.gz 解压:tar -zxvf redis-3.2.5.tar.gz ...

  8. Trace-如何跟踪某个Job的开销

    1.背景 下面是从以往Profiler收集的跟踪文件中提取Job有关数据 ;with cte as( Duration_ms ,CPU CPU_ms,Reads,Writes,StartTime,En ...

  9. MWeb 1.5 发布!增加打字机滚动模式、发布到 Evernote、印象笔记、Wordpress.com、Blogger、编辑器内代码块语法高亮

    打字机滚动模式(Typewriter Scrolling) 快捷键:CMD + Option + T,菜单:View - Typewriter Scrolling ,效果如下图: 发布到 Everno ...

  10. UEFI引导在GPT分区下安装win2008——抓住那只傲娇的win2008

    上周遇到个客户DELL R520的服务器新采购了8块3T硬盘做备份服务器,raid配置5+1,一个磁21.8T.先用普通的装desktop OS的方法发现进去没raid盘,然后就按照官方的文档进入Li ...