hdu1540 Tunnel Warfare
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7703 Accepted Submission(s):
2981
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!
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.
request in order on a separate line.
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
0
2
4
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的更多相关文章
- HDU1540 Tunnel Warfare —— 线段树 区间合并
题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...
- HDU--1540 Tunnel Warfare(线段树区间更新)
题目链接:1540 Tunnel Warfare 以为单组输入 这个题多组输入 结构体记录每个区间左边和右边的连续区间 ms记录最大 在查询操作时: 1.这个点即将查询到右区间 看这个点 x 是否存在 ...
- hdu1540 Tunnel Warfare 线段树/树状数组
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- hdu1540 Tunnel Warfare【线段树】
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- kuangbin专题七 HDU1540 Tunnel Warfare (前缀后缀线段树)
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- HDU-1540 Tunnel Warfare(区间连续点长度)
http://acm.hdu.edu.cn/showproblem.php?pid=1540 Time Limit: 4000/2000 MS (Java/Others) Memory Limi ...
- HDU1540 Tunnel Warfare 水题
分析:不需要线段树,set可过,STL大法好 #include <iostream> #include <cstdio> #include <cstring> #i ...
- Tunnel Warfare(hdu1540 线段树)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- 一次Redis的使用Bug记录(exec)
博主在一次项目中,使用了工具类中的Redis类,因为该Redis没有封装管道pipeline和exec命令,所以就大笔一挥来了一段__call; 代码如下(其中$this->_connect() ...
- 关于margin的问题
昨晚是第一天,现在来说说margin的这个存在的问题吧, 嗯,就是当你碰到 <div class="max-box"> <div class="box1 ...
- LAMP_03_Win下Apache+PHP+MySQL整合
文件: Apache : 首先修改Apache的配置文件,让Apache支持解析PHP文件.Apache配置文件在Apache安装目录的conf目录下的httpd.conf.1. 让Apache可以 ...
- 黑马程序员_ Objective-c 面向对象笔记详解
1)类,对象,方法 类 类名: 1) 类名的第一个字母必须是大写 2) 不能有下划线 3) 多个英文单词,用驼峰标识 类的声明和实现 类的声明 @interface 类名 : NSObject { @ ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- ShellExecute 使用方法
ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件.打开一个目录.打印一个文件等等),并对外部程序有一定的控制. 有几个API函数都可以实现这些功能,但是在大多数情况下She ...
- 在linux中配置安装telnet服务
Telnet 是一种流行的用于通过 Internet 登录到远程计算机的协议.Telnet 服务器软件包为远程登录主机提供了支持.要通过 Telnet 协议与另一台主机通讯,您可以使用名称或 Inte ...
- os.path 大全
os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回一个路径的最后一个组成部分 os.path.commonprefix(list) #返回 ...
- 【JAVA】【Eclipse】出现This element neither has attached source nor attached Javadoc...的解决方法
This element neither has attached source nor attached Javadoc and hence no Javadoc could be found Ec ...
- 1o_Samba
∮Linux下Samba的安装配置 §Linux 下文件共享软件 Samba 是一款实现跨主机,跨平台文件共享的软件.同类型的软件还有 ftp,nfs 不再赘述. §文件共享协议 Windows : ...