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. day07--字符编码、文件处理

    今日内容: 字符编码 文件处理 字符编码: 把字符编码成二进制 各个国家拥有各自的字符编码,这样会导致交流产生问题.所以后面推出了内存使用unicode,硬盘使用UTF-8这个模式 unicode有两 ...

  2. jQuery的extend和fn.extend理解

    参考网址:http://www.cnblogs.com/yuanyuan/archive/2011/02/23/1963287.html http://www.cnblogs.com/xuxiuyu/ ...

  3. CAN总线学习系列之二——CAN总线与RS485的比较

    CAN总线学习系列之二——CAN总线与RS485的比较 上 一节介绍了一下CAN总线的基本知识,那么有人会问,现在的总线格式很多,CAN相对于其他的总线有什么特点啊?这个问题问的好,所以我想与其它总线 ...

  4. Android Device Monitor 文件管理的常见问题 - z

    Android Device Monitor 是 Android Studio 中用于监测模拟器或真机运行状态的一款开发者工具.但开发者在使用它的过程中往往会遇到很多问题,尤其对于新手.本文分析了实际 ...

  5. VB 批量重命名文件

    VERSION 5.00 Begin VB.Form Form1 BorderStyle = 3 'Fixed Dialog Caption = "Rename use VB QQ 1009 ...

  6. c# Login UI with background picture animation

    准备4张图片 UI control: <Grid x:Class="Test1.MainBgAd" xmlns="http://schemas.microsoft. ...

  7. Python基础(条件判断和循环) if elif else for while break continue;

    条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...

  8. 如何取得Oracle并行执行的trace

    如何取得Oracle并行执行的trace: ALTER SESSION SET tracefile_identifier='10046_PROD';ALTER SESSION SET max_dump ...

  9. linux下如何解除被占用的端口号

    在本例中,假设8080端口被占用. 1.查看8080端口是否被占用: netstat -anp | grep 8080输出结果:tcp        0      0 :::8080         ...

  10. R实战 第九篇:数据标准化

    数据标准化处理是数据分析的一项基础工作,不同评价指标往往具有不同的量纲,数据之间的差别可能很大,不进行处理会影响到数据分析的结果.为了消除指标之间的量纲和取值范围差异对数据分析结果的影响,需要对数据进 ...