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. 开源自动驾驶仿真平台 AirSim (3) - 运行 AirSim

    AirSim 的官方 Github: https://github.com/Microsoft/AirSim 之前配置了很多,终于要让 AirSim 自己跑起来了. 我们需要把 AirSim 这个插件 ...

  2. kaldi - Online Audio Server(服务器客户端建立方法-旧版在线解码)

    目录 一.服务器客户端识别系统建立方法 1. Command line to start the server(服务器端启动方式): 2. Command line to start the clie ...

  3. 冥冥中转到了mac 上进行开发

    2013年愚人节前我的开发环境情况 我是一个有着15年windows使用经历的老programer,如果算上dos那还可以加两年.当过小企业网管,做过十二年的开发工作(直到老死,~_~).这期间当然也 ...

  4. Microservices with Spring Boot

    找到一套比较不错的Spring Boot/Cloud入门教程,推荐一下. https://dzone.com/users/1041459/ranga_pec.html

  5. ZOJ 3686 A Simple Tree Problem(线段树)

    Description Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the ...

  6. Check the string

    A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend ...

  7. IE中的activex控件

    1.tree控件 DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HE ...

  8. Hadoop 版本 生态圈 MapReduce模型

    忘的差不多了, 先补概念, 然后开始搭建集群实战 ... . 一 Hadoop版本 和 生态圈 1. Hadoop版本 (1) Apache Hadoop版本介绍 Apache的开源项目开发流程 : ...

  9. LintCode-66.二叉树的前序遍历

    二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,2,3]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 非递归 c ...

  10. arcgis api for javascript 各个版本的SDK下载

    1.首先,进入下载网站,需要登录才能下载.下载链接 2.选择需要下载的版本,进行下载.