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. 洛谷 P1706 全排列问题 :STL / dfs

    题目描述 输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字. 输入输出格式 输入格式: n(1≤n≤9) 输出格式: 由1-n组成的所有不重复的数字序列, ...

  2. 在本地电脑使用远程服务器的图形界面——包括 MATLAB、PyCharm 等各种软件

    在用本地电脑连接远程服务器的时候,大部分时候只能用命令行来操作.虽然可以 在本地电脑用 PyCharm 进行远程调试.在本地电脑远程使用服务器的 Jupyter Notebook.Ubuntu 和 W ...

  3. jira & analytics

    jira & analytics jira 代码有毒呀 http://jira.xgqfrms.xyz:8888/rest/analytics/1.0/publish/bulk { resou ...

  4. asp.netMVC中实现分页方法

    方法一:使用传统的sql语句实现分页,    public class UserprintDao如下 /// <summary> /// 取得用户申请记录列表(按分页) /// </ ...

  5. iOS开发UI篇—transframe属性(形变)

    iOS开发UI篇—transframe属性(形变) 1. transform属性 在OC中,通过transform属性可以修改对象的平移.缩放比例和旋转角度 常用的创建transform结构体方法分两 ...

  6. fzu1686-神龙的难题

    给出一个n\times m的01矩阵,以及\(h,w\),表示一次可以把矩阵的一个\(h\times w\)的小矩阵变为全0,问至少要多少次可以把整个矩阵变为全0.\(n,m\le 15\). 分析 ...

  7. 用css制作空心箭头(上下左右各个方向均有)

    平常在网页中,经常会有空心箭头,除了用图片外,可以用css来实现.基本思路是,用css绘制两个三角形,通过绝对定位让两三角形不完全重叠,例如制作向右的空心箭头,位于前面的三角形border颜色是需要的 ...

  8. bzoj 1877: [SDOI2009]晨跑 (网络流)

    明显拆点费用流: type arr=record toward,next,cap,cost:longint; end; const mm=<<; maxn=; maxm=; var edg ...

  9. CF724E Goods transportation 最小割 DP

    照惯例CF的题不放原题链接... 题意:一个序列上有n个点,每个点有权值pi和si.表示这个点一开始有pi个物品,最多可以卖出si个物品,每个点都可以把物品向编号更大的点运输,但是对于i < j ...

  10. P2672 推销员 优先队列 + 贪心

    ---题面--- 题解: 我会说我想这道普及组题都想了好久么.... 不熟练的普及组选手.jpg 最后随便猜了一个结论居然是对的... 放结论: 假设x = i的最优决策为f[i], 那么f[i + ...