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. Convolutional Networks for Images,Speech,and Time-series

    Convolutional Networks for Images,Speech,and Time-series Yann LeCun  Yoshua Bengio 1995年的 1引言 多层BP网络 ...

  2. Kafka下的生产消费者模式与订阅发布模式

    原文:https://blog.csdn.net/zwgdft/article/details/54633105   在RabbitMQ下的生产消费者模式与订阅发布模式一文中,笔者以“数据接入”和“事 ...

  3. Newtonsoft.Json.Linq对象读取DataSet数据

    Newtonsoft.Json.Linq对象读取DataSet数据: private void button4_Click(object sender, EventArgs e)        {   ...

  4. Android Studio com.android.support:percent 导入错误 - 转

    看第一行代码(第二版的)书,讲了一个关于PercentFrameLayout和PercentRelativeLayout的部分,书上在build.gradle中导入了com.android.suppo ...

  5. EZ 2018 05 01 NOIP2018 模拟赛(十一)

    莫名其妙暴涨Rating 其实题目都挺好挺简单的,但是越简单就越容易ZZ 不理解问什么第一题这么多人找环 不过T2是真心细节题,T3太难了 题目戳这里 T1 仔细分析题意发现那个交换规则就是废话,如果 ...

  6. 为什么要进行阿里云云计算助理工程师认证(ACA)

    阿里云助理工程师认证(ACA - Alibaba Cloud Certification Associate)是面向使用阿里云基础产品的专业技术认证,主要涉及阿里云的计算.存储.网络.安全类的核心产品 ...

  7. VS中为非控制台程序提供控制台输出窗口

    /************************************************************************/ /* 模块名:ConsoleAdapter 文件名 ...

  8. [CF1019C]Sergey's problem[构造]

    题意 找出一个集合 \(Q\),使得其中的点两两之间没有连边,且集合中的点可以走不超过两步到达其他所有不在集合中的点.输出任意一组解. \(n\leq 10^6\) 分析 考虑构造,先从 \(1\) ...

  9. Shiro安全框架学习笔记

    一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和JavaEE项目中都可以使用.它主要用来处理身份认证,授权,企业会话管理 ...

  10. 虚拟机console基础环境部署——安全加固

    1. 概述 安全是一个重要的课题.广义上可以总结为: 主机安全 网络安全 信息安全 数据安全 虽然console已经是最小化安装,但是这并不能说明console就已经安全了.之前的博客对console ...