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. day37

    今日内容 1.线程池和进程池 2.利用线程池实现套接字并发通信 3.协程(利用模块gevent模块,实现单线程下套接字并发通信) 1.线程池与进程池 要用线程池与进程池,首先要导入concurrent ...

  2. day06--元组、字典、集合与关系运算

    今日内容: 1.元组 2.字典 3.集合与关系运算 元组: 用途:记录多个值,当多个值没有改的需求,此时用元组更适合. 定义方式:在()内用逗号分隔开多个任意类型的值. 变量名=tuple('') 切 ...

  3. 文理分科 BZOJ3894 & happiness BZOJ2127

    分析: 最小割(一开始我没看出来...后来经过提点,大致理解...),不选则割的思想. 我们先这样考虑,将和选理相关的和S相连,与选文相关的和T相连,如果没有第二问,那么建图就是简单的S连cnt,cn ...

  4. [Lydsy1805月赛]对称数 BZOJ5361

    分析: 这个题,还是蛮有趣的.考虑,如果l,r区间内的所有数出现奇数次,那么[l-1,r]的抑或和等于所得抑或和. 之后怎么维护呢,主席树维护区间抑或和,记得将每个点附上一个ull级别的随机数,之后抑 ...

  5. 20155202张旭 Exp5 MSF基础应用

    20155202张旭 Exp5 MSF基础应用 实践内容 本次实验我使用的攻击方式: 1.针对office软件的主动攻击:--MS10-087: 2.MS10-002漏洞对浏览器攻击 3.针对客户端的 ...

  6. 20155323刘威良 网络对抗 Exp2 后门原理与实践

    20155323 刘威良<网络攻防>Exp2后门原理与实践 实验内容 (1)使用netcat获取主机操作Shell,cron启动 (0.5分) (2)使用socat获取主机操作Shell, ...

  7. Git配置用户名与邮箱

    1.用户名和邮箱地址的作用 用户名和邮箱地址是本地git客户端的一个变量 每次commit都会用用户名和邮箱纪录. github的contributions统计就是按邮箱来统计的. 2.查看用户名和邮 ...

  8. [Oracle]如何为数据库设置Event(eg: ORA-00235)

    [Oracle]如何为数据库设置Event(eg: ORA-00235) ■ When you use SPFILE, Setting procedure: 1. Check the current ...

  9. Scala学习(一)--Scala基础学习

    Scala基础学习 摘要: 在篇主要内容:如何把Scala当做工业级的便携计算器使用,如何用Scala处理数字以及其他算术操作.在这个过程中,我们将介绍一系列重要的Scala概念和惯用法.同时你还将学 ...

  10. 双面间谍(spy)

    双面间谍 链接 分析: 戳这 代码: #include<cstdio> #include<algorithm> #include<cstdio> #include& ...