一、题目

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. Spring Data JPA

    转自: http://www.cnblogs.com/WangJinYang/p/4257383.html Spring 框架对 JPA 的支持 Spring 框架对 JPA 提供的支持主要体现在如下 ...

  2. wordpress multisite functions

    The <?php echo esc_html( get_site_option( 'site_name' ) ); ?> network currently powers <?ph ...

  3. mongoperf用法

    mongoperf是mongoDB自带工具,用于评估磁盘随机IO性能. 官方文档 使用方法 作用:用于部署前,评估mongodb所在存储的IO性能 用法:mongoperf <conffile, ...

  4. ACM集训的Day3 B。。。盲目搜索之DFS。。。

    milk 一.题目描述: gzp有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数, 最初,A和B桶都是空的,而C桶是装满牛奶的.有时,农民把牛奶从一个桶倒到 另一个桶中,直到被灌 ...

  5. Python实现简单的记账本功能

    目标: 1.使用序列化cPickle 2.账户中钱要大于花费的钱,否则提示请存钱 2.编写函数,实现存钱,花钱,查询及退出功能 1.序列化 pickle是python实现序列化的模块,次模块存在使用C ...

  6. nginx 配置优化(简单)

    配置文件     正常运行的必备配置:         1.user username [groupname]:(推荐nginx)         以那个用户身份运行,以在configure指定的用户 ...

  7. Android 基于Android的手机邮件收发(JavaMail)之三(邮件接收)

    初次做这个程序的时候,是仿照着网上别人的程序做的.因为本人比较菜,是一个新手,以前的基础知识没有学好,所以尽管有了别人的代码但是还是不知道怎么在界面上显示出它的效果来,废话不多少,现在就贴出我的参考程 ...

  8. nginx1.8安装nginx_concat_module及400错误解决办法

    nginx安装concat模块可以合并js,css等静态资源,减少http请求 在nginx源码目录执行命令: ./configure --user=www --group=www --prefix= ...

  9. TP框架主要文件夹注释

    TP框架主要文件夹注释 common -> 函数库目录 conf -> 配置文件目录lang -> 语言包librang -> 核心资源库 behacior -> 行为目 ...

  10. XPath 节点

    在 XPath 中,有七种类型的节点:元素.属性.文本.命名空间.处理指令.注释以及文档节点(或称为根节点). XPath 术语 节点(Node) 在 XPath 中,有七种类型的节点:元素.属性.文 ...