Tunnel Warfare

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

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

题意:题目给出一连串点,会破坏指定的点,修复上一个点(依次向上回溯),然后最后求包含X的最长连续区间长度

思路:设定每个区间都有一个左最大连续子区间长度,右最大连续子区间长度,然后一直维护这两个值。

代码:

#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
const int N = 50000+5;
const int MOD = 20071027;
using namespace std;
struct Node{
int l,r;
int lmax,rmax;
}node[N<<2];
void build(int l,int r,int rt){
node[rt].l = l;
node[rt].r = r;
node[rt].lmax = node[rt].rmax = r - l + 1;
if(l == r) return;
int m = (l + r) >> 1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
}
void fresh(int rt){    //更新左右最大值
node[rt].lmax = node[rt<<1].lmax;
if(node[rt<<1].lmax + node[rt<<1].l - 1 == node[rt<<1].r)   
node[rt].lmax += node[rt<<1|1].lmax; node[rt].rmax = node[rt<<1|1].rmax;
if(node[rt<<1|1].r - node[rt<<1|1].rmax + 1 == node[rt<<1|1].l)
node[rt].rmax += node[rt<<1].rmax;
}
void update(int v,int rt,int x){
if(node[rt].l == node[rt].r){
node[rt].lmax = node[rt].rmax = v;
return;
}
int m = (node[rt].l + node[rt].r) >> 1;
if(x <= m) update(v,rt<<1,x);
else update(v,rt<<1|1,x);
fresh(rt);
}
int query(int rt,int x){
if(node[rt].l == node[rt].r){
return node[rt].lmax;
}
int m = (node[rt].l + node[rt].r) >> 1;
if(x <= m){
if(x >= node[rt<<1].r - node[rt<<1].rmax + 1){
return node[rt<<1].rmax + node[rt<<1|1].lmax;
}
else{
return query(rt<<1,x);
}
}
else{
if(x <= node[rt<<1|1].lmax + node[rt<<1|1].l - 1){
return node[rt<<1|1].lmax + node[rt<<1].rmax;
}
else{
return query(rt<<1|1,x);
}
}
} int main(){
int n,q,x,last;
char arr[2];
while(~scanf("%d%d",&n,&q)){
stack<int> reb;
build(1,n,1);
while(q--){
scanf("%s",arr);
if(arr[0] == 'D'){
scanf("%d",&x);
update(0,1,x);
reb.push(x);
}
else if(arr[0] == 'R'){
x=reb.top();
reb.pop();
update(1,1,x);
}
else{
scanf("%d",&x);
printf("%d\n",query(1,x));
}
}
}
return 0;
}

HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解的更多相关文章

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

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

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

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

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

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

  4. hdu1540 Tunnel Warfare 线段树/树状数组

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

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

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

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

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

  7. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

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

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

  9. HDU 1540 Tunnel Warfare (线段树)

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

随机推荐

  1. mysql 表基本增删查改

    对表的操作是在某个数据库下才能进行的,所以要先选择数据库 "use 数据库名;" 1.创建数据表 1)"create table 表名 (字段1  类型  [约束],  ...

  2. SQLServerDBA十大必备工具---让生活轻松点

    原贴:http://www.cnblogs.com/fygh/archive/2012/04/25/2469563.html 国外整理拓展帖:http://weblogs.sqlteam.com/ml ...

  3. OLTP与OLAP

    当今的数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical Processing ...

  4. 如何设置dedecms自定义表单必填项?

    用dedecms自定义表单可以制作一个简单的预约系统,有些相关信息需要设置为必填项,比如联系方式,没有留下真实的电话或其他信息,以后要怎么联系到你的客户.那我们要如何设置织梦cms自定义表单必填项呢? ...

  5. python2与python3之间的主要区别

    python2与python3之间的主要区别 print py2:print语句,语句就意味着可以直接跟要打印的东西,如果后面接的是一个元组对象,直接打印 py3:print函数,函数就以为这必须要加 ...

  6. ab压力测试遭遇apr_socket_recv: Connection reset by peer (104) 怎么办

    ab -r  -c 2000 -n 200000  www.baidu.com 其实只要加上-r就可以了.但是为什么呢?ab --help就知道了 当Socket接收到错误的时候不退出,就是这句.事实 ...

  7. 从Maven仓库中导出jar包

    从Maven仓库中导出jar包:进入工程pom.xml 所在的目录下,输入以下命令:mvn dependency:copy-dependencies -DoutputDirectory=lib更简单的 ...

  8. c++中的构造(包括移动),赋值(包括移动),析构详解

    这五种操作:构造(包括移动),赋值(包括移动),析构其实就是定义了对一个对象进行构造,赋值,析构时的行为.理解这些行为并不复杂,复杂的是理解在继承下这些行为的表现.需要注意的是他们并不会被继承(传统意 ...

  9. JSON—基础

    什么是JSON?(JavaScript Object Notation) 1:一种轻量级的数据交换格式 2:采用独立于语言的文本格式 3:常用于客户端和服务器之间传递数据(以对象的形式传递) JSON ...

  10. webstorm使用心得

    收藏夹功能:当工程目录很庞大时,有些子目录很经常打开,但层级又很深,这时候可以把目录添加到收藏夹里面,添加成功后,左侧有个“Favorites”菜单 面包屑导航:除了左侧的工程页面,可以选择目录之外, ...