HDU 1540 Tunnel Warfare(最长连续区间 基础)

时间:2022-10-21 11:58:53

Tunnel Warfare

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

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
/*
HDU 1540 Tunnel Warfare(最长连续区间 基础) 给你1-n连续的n个数字,然后执行以下三种操作
1.D x 删除第x个数字
2.R 恢复上一次删除的数字
3.Q x 查询包含x的最长连续区间 主要有ls,rs,ms分别表示当前节点 左端点开始的最长...,右端点...,整体最长连续区间
然后主要是在push_up和query上面了,要进行一些特殊判断来确定长度是否应该合并 hhh-2016-03-27 16:39:28
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
const int maxn = 50050;
struct node
{
int l,r;
int ls,rs,ms; //左端点,右端点,最大
int mid()
{
return (l+r)>>1;
}
} tree[maxn*5]; void push_up(int i)
{
tree[i].ls = tree[lson].ls;
tree[i].rs = tree[rson].rs; tree[i].ms = max(tree[lson].ms,tree[rson].ms);
tree[i].ms = max(tree[i].ms,tree[rson].ls+tree[lson].rs);
//i的ms肯定是lson,rson的ms.或者它们中间相连的长度
if(tree[i].ls == tree[lson].r-tree[lson].l+1)
//如果包含左儿子的全部,则与右儿子的ls相连
tree[i].ls += tree[rson].ls;
if(tree[i].rs == tree[rson].r-tree[rson].l+1)
tree[i].rs += tree[lson].rs;
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].ls=tree[i].rs=tree[i].ms=0;
if(l ==r )
{
tree[i].ls=tree[i].rs=tree[i].ms=1;
return ;
}
int mid=tree[i].mid();
build(lson,l,mid);
build(rson,mid+1,r);
push_up(i);
} void push_down(int i)
{ } void Insert(int i,int k,int val)
{
if(tree[i].l == tree[i].r)
{
if(val == 1)
tree[i].ls=tree[i].rs=tree[i].ms=1;
else
tree[i].ls=tree[i].rs=tree[i].ms=0;
return ;
}
push_down(i);
int mid = tree[i].mid();
if(k <= mid)
Insert(lson,k,val);
else
Insert(rson,k,val);
push_up(i);
} int query(int i,int k)
{
if(tree[i].l==tree[i].r || tree[i].ms==0 || tree[i].ms==(tree[i].r-tree[i].l+1))
return tree[i].ms; int mid = tree[i].mid();
if(k <= mid)
{
if(k >= tree[lson].r-tree[lson].rs+1) //如果在rs的范围内,加上右儿子的ls(相连)
return query(lson,k) + query(rson,mid+1);
else
return query(lson,k);
}
else
{
if(k <= tree[rson].ls+tree[rson].l-1) //同理
return query(rson,k)+query(lson,mid);
else
return query(rson,k);
}
} int qry[maxn];
char op[0];
int main()
{
int n,x,q;
int cas =1;
while(scanf("%d%d",&n,&q) != EOF)
{
int tot = 0;
build(1,1,n);
for(int i = 1; i <= q; i++)
{
scanf("%s",op);
if(op[0] == 'D')
{
scanf("%d",&x);
qry[tot++] = x;
Insert(1,x,-1);
}
else if(op[0] == 'R')
{
x = qry[--tot];
Insert(1,x,1);
}
else
{
scanf("%d",&x);
printf("%d\n",query(1,x));
}
}
}
return 0;
}

  

HDU 1540 Tunnel Warfare(最长连续区间 基础)的更多相关文章

  1. hdu 1540 Tunnel Warfare &lpar;区间线段树(模板)&rpar;

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...

  2. hdu 1540 Tunnel Warfare (线段树,维护当前最大连续区间)

    Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively i ...

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

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

  4. hdu 1540 Tunnel Warfare&lpar;线段树区间统计&rpar;

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

  5. HDU 1540 Tunnel Warfare

    HDU 1540 思路1: 树状数组+二分 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #d ...

  6. HDU 1540 Tunnel Warfare 平衡树 &sol; 线段树:单点更新,区间合并

    Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Lim ...

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

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

  8. hdu 1540 Tunnel Warfare (线段树 区间合并)

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

  9. HDU 1540 Tunnel Warfare &lpar;线段树)

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

随机推荐

  1. &lbrack;原&rsqb;调试ComFriendlyWaitMtaThreadProc崩溃

    项目里安装了UIA相关的钩子来监听UIA相关事件,退出的时候偶尔会崩溃在ComFriendlyWaitMtaThreadProc中,如下  从上图可以看出 是访问到无效的地址了,用!address 0 ...

  2. &lt&semi;转&gt&semi;iOS9 Day-by-Day:iOS开发者必须了解的iOS 9新技术与API

    iOS9 Day-by-Day是作者Chris Grant新开的一个系列博客,覆盖了iOS开发者必须知道的关于iOS 9的新技术与API,并且还进行了实际操作演练,每篇文章中相关的代码Chris都会将 ...

  3. masterha&lowbar;check&lowbar;repl报错汇总

    [root@DBMysql ~]#masterha_check_repl --conf=/etc/masterha/app1.cnf 导致如下报错的原因主要有两类: 1.mysql的安装时用源码安装, ...

  4. (转载)APC支持php5&period;4了

    (转载)http://www.neatstudio.com/archives/?article-2061.html 时隔一年多,APC终于又更新了,这次更新最大的就是支持PHP5.4:- Add PH ...

  5. 【C&num;】委托与事件

    一.委托 1.概念:用来存放 方法 指针(地址)的容器. 为什么要有委托?当有的业务代码总体已经实现,但有部分需要调用者来决定,就可以使用委托的方式,让调用者把一段代码以 方法的方式 传入. [例子] ...

  6. Eclipse Useful Plugins Links

    1.maven for eclipse http://m2eclipse.sonatype.org/sites/m2e 2. svn1.6  for ecipse http://subclipse.t ...

  7. Winform 单实例运行

    Winform 单实例运行 前言 前两天在博客园看到<如何防止程序多次运行>,文章写的很好,最后还留下一个问题给我们思考.关于Winform的防止多次运行,曾经也想研究过,但是后来工作上没 ...

  8. QMenu 设置菜单图标 &amp&semi; 生成菜单树

    效果图 源码 .h 文件 protected slots: void onMenuTriggered(QAction*); .cpp 文件 // 菜单 QMenu *pMenu = new QMenu ...

  9. jQuery基础---Ajax基础

    内容提纲: 1.Ajax 概述 2.load()方法 3.$.get()和$.post() 4.$.getScript()和$.getJSON() 5.$.ajax()方法 6.表单序列化 发文不易, ...

  10. leetcode-下一个排列

    下一个排列 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外 ...