Tunnel Warfare

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

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
题目大意:
有n个村庄,每个操作:
D x 表示摧毁x
Q x 询问与x相连接的有多少个
R 恢复之前摧毁的一个城市
这道题是线段树的区间合并。
首先用0表示已经摧毁的村庄,1表示未摧毁或已经修复的村庄
记录的信息有两个:
1.l表示该区间从左数的最长1串
2.r表示该区间从右数的最长1串
pushup时在长度没有覆盖整个左区间或右区间时直接做

tr[x].l=tr[ls].l;
tr[x].r=tr[rs].r;

如果完整覆盖了左或右区间再做特殊处理

if(tr[ls].l==mid-l+1) tr[x].l+=tr[rs].l;
if(tr[rs].r==r-mid) tr[x].r+=tr[ls].r;

所以对于摧毁和恢复直接进行单点修改,将tr[x].l=tr[x].r=z(z为0或1)

对于询问,可以分别查询a向左的最长1串和a向右最长1串-1

特别的如果a已经被摧毁,则ans=-1

此时要做特判。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ls x<<1
#define rs x<<1|1
const int N=;
struct X
{
int l,r;
}tr[N<<];
int st[N];
void bu(int l,int r,int x)
{
if(l==r) tr[x].l=tr[x].r=;
else
{
int mid=l+(r-l)/;
bu(l,mid,ls);
bu(mid+,r,rs);
tr[x].l=tr[x].r=r-l+;
}
}
void chan(int l,int r,int x,int w,int z)
{
if(l==r) tr[x].l=tr[x].r=z;
else
{
int mid=l+(r-l)/;
if(w<=mid) chan(l,mid,ls,w,z);
else chan(mid+,r,rs,w,z);
tr[x].l=tr[ls].l;tr[x].r=tr[rs].r;
     if(tr[x].l==mid-l+) tr[x].l+=tr[rs].l;
if(tr[x].r==r-mid) tr[x].r+=tr[ls].r;
}
}
int ask1(int l,int r,int x,int w)
{
if(w==r) return tr[x].r;
else
{
int mid=l+(r-l)/,re;
if(w>mid)
{
re=ask1(mid+,r,rs,w);
if(w-mid==re) re+=tr[ls].r;
}
else re=ask1(l,mid,ls,w);
return re;
}
}
int ask2(int l,int r,int x,int w)
{
if(w==l) return tr[x].l;
else
{
int mid=l+(r-l)/,re;
if(w<=mid)
{
re=ask2(l,mid,ls,w);
if(mid-w+==re) re+=tr[rs].l;
}
else re=ask2(mid+,r,rs,w);
return re;
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int s=;
memset(st,,sizeof(st));
memset(tr,,sizeof(tr));
bu(,n,);
while(m--)
{
char c;
scanf("\n%c",&c);
if(c=='D')
{
scanf("%d",&st[++s]);
chan(,n,,st[s],);
}
else if(c=='R') chan(,n,,st[s--],);
else
{
int a,ans;
scanf("%d",&a);
ans=ask1(,n,,a)+ask2(,n,,a)-;
if(ans>) printf("%d\n",ans);
else printf("0\n");
}
}
}
return ;
}

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(线段树区间更新)

    题目链接:1540 Tunnel Warfare 以为单组输入 这个题多组输入 结构体记录每个区间左边和右边的连续区间 ms记录最大 在查询操作时: 1.这个点即将查询到右区间 看这个点 x 是否存在 ...

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

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

  4. hdu1540 Tunnel Warfare【线段树】

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

  5. HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解

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

  6. kuangbin专题七 HDU1540 Tunnel Warfare (前缀后缀线段树)

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

  7. HDU-1540 Tunnel Warfare(区间连续点长度)

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

  8. HDU1540 Tunnel Warfare 水题

    分析:不需要线段树,set可过,STL大法好 #include <iostream> #include <cstdio> #include <cstring> #i ...

  9. Tunnel Warfare(hdu1540 线段树)

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

随机推荐

  1. Spring3.1新特性(转)

    一.Spring2.5之前,我们都是通过实现Controller接口或其他实现来定义我们的处理器类. 二.Spring2.5引入注解式处理器支持,通过@Controller 和 @RequestMap ...

  2. #include <objsafe.h>//OCX控件在IE8浏览器下不能使用问题

    一.OCX控件开发常见的问题 1.OCX控件在IE8浏览器下不能使用问题 原因:IE8会拦截OCX控件的方法. 解决方法:在OCX控件开发时加入安全接口. (1)在有"Crtl"字 ...

  3. VS2010统计代码行数 [转]

    按CTRL+SHIFT+F (Find in files),勾上支持正则表达式,然后输入搜索内容:  ^:b*[^:b#/]+.*$ 以上表达式的统计可做到:#开头 和 /开头 或者 空行 都不计入代 ...

  4. CentOS 6.5下静默安装oracle

    本例: 通过SSH远程连接云主机,上传oracle11g安装包,在centos6.5上无图形化界面静默安装oracle11g. 涉及工具及环境: 1.本地环境windows7+ssh远程连接工具xSh ...

  5. [POJ2096] Collecting Bugs (概率dp)

    题目链接:http://poj.org/problem?id=2096 题目大意:有n种bug,有s个子系统.每天能够发现一个bug,属于一个种类并且属于一个子系统.问你每一种bug和每一个子系统都发 ...

  6. (Array)169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. day8-异常

    异常处理 1.异常基础 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!! try: pass except Ex ...

  8. .Net 连接字符串的解释

    https://msdn.microsoft.com/zh-cn/library/cc716756.aspx 连接字符串参数 连接字符串的格式是使用分号分隔的键/值参数对列表: keyword1=va ...

  9. onethink上传图片(资源)和预览

    直接上干货 不废话了 普通上传:  onthink框架 后台已经有图片和文件上传功能 controller里只需: public function addPicture(){ /* 调用文件上传组件上 ...

  10. CSS3中-webkit-overflow-scrolling: touch 的使用方法详解

    -webkit-overflow-scrolling 属性控制元素在移动设备上是否使用滚动回弹效果. auto 使用普通滚动, 当手指从触摸屏上移开,滚动会立即停止. touch 使用具有回弹效果的滚 ...