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 (区间线段树(模板))

    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(线段树区间统计)

    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 平衡树 / 线段树:单点更新,区间合并

    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 (线段树)

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

随机推荐

  1. Django 模版语法

    一.简介 模版是纯文本文件.它可以产生任何基于文本的的格式(HTML,XML,CSV等等). 模版包括在使用时会被值替换掉的 变量,和控制模版逻辑的 标签. {% extends "base ...

  2. 利用python实现简单邮件功能

    #!/usr/bin/env python # -*- coding:utf-8 -*- import smtplib from email.utils import formataddr from ...

  3. android头像选择(拍照,相册,裁剪)

    组织头像上传时候,不兼容android6.0,并且 imageview.setImageBitmap(BitmapFactory.decodeFile(IMAGE_FILE_LOCATION));// ...

  4. 【TensorFlow随笔】关于一个矩阵与多个矩阵相乘的问题

    问题描述: Specifically, I want to do matmul(A,B) where  'A' has shape (m,n)  'B' has shape (k,n,p) and t ...

  5. Python之旅_第一章Python入门

    一.编程语言分类 1.机器语言:即计算机能听懂的二进制语言,0000 0001,直接操控硬件: 2.汇编语言:简写的英文标识符代替二进制语言,本质同样是直接操控硬件: 3.高级语言:用更贴近人类的语言 ...

  6. Oracle update 执行更新操作后的数据恢复

    操作数据库,经常会出现误操作,昨天执行的更新操作之后发现更新错了,只能想办法数据恢复了,现在整理一下 第一步:查询执行更新操作的时间 select r.FIRST_LOAD_TIME,r.* from ...

  7. 学习ASP.NET Core Razor 编程系列五——Asp.Net Core Razor新建模板页面

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  8. MYSQL之库操作

    一.系统数据库 information_schema :虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等 mysql:核心数据库,里面包含用户.权限. ...

  9. python——常用模块

    python--常用模块 1 什么是模块: 模块就是py文件 2 import time #导入时间模块 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的 ...

  10. SpringMVC(八):使用Servlet原生API作为Spring MVC hanlder方法的参数

    在SpringMVC开发中,是有场景需要在Handler方法中直接使用ServletAPI. 在Spring MVC Handler的方法中都支持哪些Servlet API作为参数呢? --Respo ...