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. NHibernate3快速上手教程FluentNHibernate配置与DBHelper(已过期,有更好的)

    很多学习NHibernate的新手很容易卡在配置文件这一关,正所谓万事开头难,上手后再配合官方文档就比较容易了. 网上关于配置文件的资料非常多,但由于版本的问题,许多老的教程中都没有明确指出类库的版本 ...

  2. opencv-学习笔记(6)图像梯度Sobel以及canny边缘检测

    opencv-学习笔记(6)图像梯度Sobel以及canny边缘检测 这章讲了 sobel算子 scharr算子 Laplacion拉普拉斯算子 图像深度问题 Canny检测 图像梯度 sobel算子 ...

  3. HADOOP docker(十):hdfs 结构体系

    1.简介2.namenode和datanode3.The File System Namespace 文件系统命名空间4.Data Replication 数据复制5.Replica Placemen ...

  4. Centos7 下nginx nginx-1.13.4 安装

    环境:CentOS Linux release 7.3.1611 (Core)  Linux localhost.localdomain 3.10.0-514.26.2.el7.x86_64 #1 S ...

  5. 软件工程 part4 评价3作品

    作品1 抢答器 地址: https://modao.cc/app/ylGTXobcMU7ePNi6tY53gG4iraLl0md评价: 挺好玩,但是字体大小是个缺陷,简单大方. 作品2:连连看 软件工 ...

  6. 最多水容器(M)

    题目 给定n个非负整数a 1,a 2,...,a n,其中每个代表坐标(i,a i)处的一个点.绘制n条垂直线,使得线i的两个端点处于(i,a i)和(i,0)处.找到两条线,它们与x轴一起形成一个容 ...

  7. Java常用类之Math类

    Java 的常用类Math类: java.lang.Math 提供了系列的静态方法用于科学计算,其方法的参数和返回值类型一般为 double 类型. 如: 1. public static final ...

  8. pfx 证书怎么打开

    其实双击就能够自动运行导入向导的 不行的话使用我的办法: 单击开始--运行--里输入mmc 然后单击文件--选择添加删除管理单元--再选择添加--拉动滚动条找到证书一项,点击添加再点击完成(不用做任何 ...

  9. kafka启动出现:Unsupported major.minor version 52.0 错误

    具体的错误输出: Exception in thread "main" java.lang.UnsupportedClassVersionError: kafka/Kafka : ...

  10. 线程同步(使用了synchronized)和线程通讯(使用了wait,notify)

    线程同步 什么是线程同步? 当使用多个线程来访问同一个数据时,非常容易出现线程安全问题(比如多个线程都在操作同一数据导致数据不一致),所以我们用同步机制来解决这些问题. 实现同步机制有两个方法:1.同 ...