Tunnel Warfare

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

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
 题意:
n个村庄连在一起,最初都完好,D a表示a村庄被破坏,Q a表示询问与a村庄直接或间接相连的有几个,R表示修复了最后破坏的一个村庄。
代码:
//线段树维护pre最大前缀,suf最大后缀,最后求某一点的答案就是其
//前缀加上后缀。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int maxn=;
int suf[maxn*],pre[maxn*];
void pushup(int i,int l,int r)
{
int m=(l+r)>>;
pre[i]=pre[i<<];
if(pre[i]==m-l+) pre[i]+=pre[i<<|];
suf[i]=suf[i<<|];
if(suf[i]==r-m) suf[i]+=suf[i<<];
}
void build(int i,int l,int r)
{
if(l==r){
suf[i]=pre[i]=;
return ;
}
int m=(l+r)>>;
build(i<<,l,m);
build(i<<|,m+,r);
pushup(i,l,r);
}
void update(int id,int c,int i,int l,int r)
{
if(l==r){
suf[i]=pre[i]=c;
return ;
}
int m=(l+r)>>;
if(id<=m) update(id,c,i<<,l,m);
else update(id,c,i<<|,m+,r);
pushup(i,l,r);
}
int query_pre(int ql,int qr,int i,int l,int r)
{
if(ql<=l&&qr>=r) return pre[i];
int m=(l+r)>>,res;
if(qr<=m) return query_pre(ql,qr,i<<,l,m);
if(ql>m) return query_pre(ql,qr,i<<|,m+,r);
res=query_pre(ql,qr,i<<,l,m);
if(res==m-max(l,ql)+) res+=query_pre(ql,qr,i<<|,m+,r);
//注意记住写上max函数
return res;
}
int query_suf(int ql,int qr,int i,int l,int r)
{
if(ql<=l&&qr>=r) return suf[i];
int m=(l+r)>>,res;
if(qr<=m) return query_suf(ql,qr,i<<,l,m);
if(ql>m) return query_suf(ql,qr,i<<|,m+,r);
res=query_suf(ql,qr,i<<|,m+,r);
if(res==min(r,qr)-m) res+=query_suf(ql,qr,i<<,l,m);
return res;
}
int main()
{
int n,m,x;
char ch[];
while(scanf("%d%d",&n,&m)==){
build(,,n);
stack<int>s;
while(m--){
scanf("%s",ch);
if(ch[]=='D'){
scanf("%d",&x);
update(x,,,,n);
s.push(x);
}
else if(ch[]=='R'){
if(!s.empty()){
int x=s.top();s.pop();
update(x,,,,n);
}
}
else if(ch[]=='Q'){
scanf("%d",&x);
int ans=query_suf(,x,,,n);
if(ans==) printf("%d\n",ans);
else{
ans+=query_pre(x,n,,,n)-;
printf("%d\n",ans);
}
}
}
}
return ;
}

HDU1540 区间合并的更多相关文章

  1. hdu1540 区间合并+询问某点的最大连续块

    询问操作需要搞一下 今天被区间合并降智了 /* D a: 摧毁第a个点 Q a:询问a所在的点的块大小 R :修复最后被破坏的点 对于所有的点需要进行一次更新 更新比较容易,tag用来表示区间是否是完 ...

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

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

  3. kb-09-线段树--区间合并比较繁

    /* hdu-1540 题意:一个线段,长度为n,三种操作,Dx,挖掉某个点:R,恢复最近被挖掉的点:Qx查询该点所在的连续区间的长度: 树的节点维护三个变量,该节点左边界开始连续的个数ll,右边界开 ...

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

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

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

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

  6. HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并

    Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Lim ...

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

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

  8. POJ 2750 Potted Flower (线段树区间合并)

    开始懵逼找不到解法,看了网上大牛们的题解才发现是区间合并...  给你n个数形成一个数列环,然后每次进行一个点的修改,并输出这个数列的最大区间和(注意是环,并且区间最大只有n-1个数) 其实只需要维护 ...

  9. ACM: Hotel 解题报告 - 线段树-区间合并

    Hotel Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description The ...

随机推荐

  1. redis 面试

    Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet. 如果你是Redis中高级用户,还需要加上下面几种数据结构HyperLogLog.G ...

  2. 最短路径算法(II)

    什么??你问我为什么不在一篇文章写完所有方法?? Hmm…其实我是想的,但是博皮的加载速度再带上文章超长图片超多的话… 可能这辈子都打不开了吧… 上接https://www.cnblogs.com/U ...

  3. def语句和参数

    如果调用print()或len()函数,你会传入一些值,放在括号内,在这里成为“参数”.也可以自己定义接受参数的函数.在文件编辑器中输入这个例子: def hello(name): print('He ...

  4. 理解Python中的__builtin__和__builtins__

    以Python 2.7为例,__builtin__模块和__builtins__模块的作用在很多情况下是相同的. 但是,在Python 3+中,__builtin__模块被命名为builtins. 所 ...

  5. Matlab提速方法

    1. 向量化. 尽量少用for循环. 2. 循环竖着走比横着走快. 3. 内置函数也有优化的空间 不少内置函数都有大量的error check.直接用profiler找出真正干活的.不少内置函数在网上 ...

  6. 第一届"进化论杯"月赛 解题报告

    Problem A: derivative 思路:水题.算出二阶导数,直接 printf 结果. 在求出二阶导数后可以不立刻化简,此时式中带有大量 e^(-x) 项.此时直接可以代入 ln|x0|,把 ...

  7. 软工冲刺-Alpha 冲刺 (3/10)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 很胖,刚学,照猫画虎做了登录与注册界面. 展示GitHub ...

  8. 关闭win7/Server 2008非正常关机启动自动修复功能

    命令提示符下输入 bcdedit /set {default} bootstatuspolicy ignoreallfailures bcdedit /set {current} recoveryen ...

  9. 新建maven工程问题001

    这周一直在研究SpringMVC+Mybatis,有些心得,记录一下. Ⅰ:建maven遇到的问题. 1.1 新建maven时选中[Create a simple project]这样,后面[Pack ...

  10. shit antd & Merry Christmas bug

    shit antd & Merry Christmas bug https://github.com/ant-design/ant-design/issues/13098 antd 玩大了? ...