Tunnel Warfare

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

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
 
思路:
这道题主要难点在于求出Q操作,Q  x要求x左右两边连续村庄的个数,之前一直没想到怎么把一个点转成一个区间,其实我们可以直接将这个区间分为两部分:
1 - x,x - n,求出1 - x从最右边开始连续区间的长度,和x - n,从最左边开始连续区间的长度,这样就转换成了很简单的区间合并问题了。最后因为中间重复求
了x的值,当x未被摧毁时相当于多加了1,减掉1就好了,如果x是被摧毁的点,那么他左右区间相加必为0,减一的话就变成-1了,我们把他变成0就好了
 
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 1e5+;
int lsum[M<<],rsum[M<<];
void pushup(int l,int r,int rt){
lsum[rt] = lsum[rt<<];
rsum[rt] = rsum[rt<<|];
mid;
if(lsum[rt] == m-l+) lsum[rt] += lsum[rt<<|];
if(rsum[rt] == r - m) rsum[rt] += rsum[rt<<];
} void update(int p,int c,int l,int r,int rt){
if(l == r){
lsum[rt] = rsum[rt] = c;
return ;
}
mid;
if(p <= m) update(p,c,lson);
else update(p,c,rson);
pushup(l,r,rt);
} void build(int l,int r,int rt){
if(l == r){
lsum[rt] = rsum[rt] = ;
return ;
}
mid;
build(lson);
build(rson);
pushup(l,r,rt);
}
int queryl(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return lsum[rt];
}
mid;
if(L > m) return queryl(L,R,rson);
if(R <= m) return queryl(L,R,lson);
int t1 = queryl(L,m,lson);
int t2 = queryl(m+,R,rson);
if(t1 == m-L+) t1+=t2;
return t1;
} int queryr(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return rsum[rt];
}
mid;
if(L > m) return queryr(L,R,rson);
if(R <= m) return queryr(L,R,lson);
int t1 = queryr(L,m,lson);
int t2 = queryr(m+,R,rson);
if(t2 == R - m) t2 += t1;
return t2;
}
stack<int>st;
int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
int n,m,x;
char op;
while(cin>>n>>m){
memset(lsum,,sizeof(lsum));
memset(rsum,,sizeof(rsum));
build(,n,);
while(m--){
cin>>op;
if(op == 'D'){
cin>>x;
update(x,,,n,);
st.push(x);
}
else if(op == 'R'){
x = st.top();
//cout<<x<<endl;
st.pop();
update(x,,,n,);
}
else{
cin>>x;
int num = queryr(,x,,n,) + queryl(x,n,,n,)-;
if(num < ) num = ;
//cout<<queryr(1,x,1,n,1)<<" "<<queryl(x,n,1,n,1)<<endl;
cout<<num<<endl;
}
}
}
return ;
}

hdu 1540 Tunnel Warfare (线段树 区间合并)的更多相关文章

  1. HDU 1540 Tunnel Warfare 线段树区间合并

    Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...

  2. hdu 1540 Tunnel Warfare 线段树 区间合并

    题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2. ...

  3. hdu 1540 Tunnel Warfare(线段树区间统计)

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

    Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  5. Tunnel Warfare 线段树 区间合并|最大最小值

    B - Tunnel WarfareHDU - 1540 这个有两种方法,一个是区间和并,这个我个人感觉异常恶心 第二种方法就是找最大最小值 kuangbin——线段树专题 H - Tunnel Wa ...

  6. HDU 1540 Tunnel Warfare (线段树)

    Tunnel Warfare Problem Description During the War of Resistance Against Japan, tunnel warfare was ca ...

  7. HDU 1540 Tunnel Warfare (线段树)

    题目大意: n 个村庄排列在一条直线上,相邻的村庄有地道连接,除首尾两个村庄外,其余村庄都有两个相邻的村庄.其中有 3 中操作 D x :表示摧毁编号为 x 的村庄,Q x:表示求出包含村庄 x 的最 ...

  8. HDU 1540 Tunnel Warfare (线段树或set水过)

    题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 析:首先可以用set水过,set用来记录每个被破坏的村庄,然后查找时,只要查找左右两个端点好. 用线段 ...

  9. HDU1540 Tunnel Warfare —— 线段树 区间合并

    题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...

  10. HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举

    HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...

随机推荐

  1. HDU 1285 经典拓扑排序入门题

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. 5-51单片机ESP8266学习-AT指令(8266TCP服务器--用手机TCP调试助手发信息给单片机控制小灯的亮灭)

    http://www.cnblogs.com/yangfengwu/p/8759294.html 源码链接:https://pan.baidu.com/s/1wT8KAOIzvkOXXNpkDI7E8 ...

  3. iscsi target tgt架构

    tgt是用户态实现的iscsi target,而iet(iscsi enterprise target)是在内核态实现的target,tgt相比于iet来说,因为其用户态实现,方便调试,新加入一些功能 ...

  4. Sign in with the app-specific password you generated. If you forgot the app-specific password or need to create a new one, go to appleid.apple.com

    iOS打包报错信息如下:Sign in with the app-specific password you generated. If you forgot the app-specific pas ...

  5. 20155338 《网络攻防》Exp5 MSF基础应用

    20155338 <网络攻防>Exp5 MSF基础应用 基础问题回答 1. 用自己的话解释什么是exploit,payload,encode? exploit将真正要负责攻击的代码传送到靶 ...

  6. springboot的热部署和dubug

    采用了项目聚合,产生一些不同,遇到的问题和解决方法分享下. 项目结构: rebuilder2 -htran 主项目 -htran-api 1.htran.pom <parent> < ...

  7. CS50.4

    1, PDF,portable document format 便携式文档格式 2, 关于文本编辑器(文字编辑器)和文档编辑器(文字处理器),前者可用来写程序的源代码?名字挺难分辨的. *3, “-o ...

  8. python list的一个面试题

    面试题''' 一个list,里面的数字偶数在左边,奇数在右边,不借助其他列表 ''' def userlist(add_list): if type(add_list)==list: if len(a ...

  9. Hexo站点之域名配置【2】

    该系列博客列表请访问:http://www.cnblogs.com/penglei-it/category/934299.html 摘要 因为Hexo个人博客是托管在github之上,每次访问都要使用 ...

  10. 4星|《流量池》:Luckin Coffee营销操盘手经验谈

    流量池:“急功近利”的流量布局.营销转化 作者是一线营销操盘手,全书是作者的经验总结,这样的作者在营销类图书中比较罕见,因此这本书非常有价值. 全书是写给巨头之外的企业营销人员看的,这样的企业的流量来 ...