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 ...
随机推荐
- eclipse插件
#eclipse market http://www.eclipse.org/mpc/archive.php http://download.eclipse.org/mpc/mars/ #文件路径 p ...
- 使用Squirrel创建基于Electron开发的Windows 应用安装包
我们把自己开发的Electron应用发布之前,需要把app打包成简单的安装包,这样app更容易被获取,以此来发布我们的应用.我们可以参考Wix或其他的安装程序,但是对于Electron应用更好的打包程 ...
- Python-Numpy函数-tile函数
tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组.比如tile(A,n),功能是将数组A重复n次,构成一个新的数组,我们还是使用具体的例子来说明问题 ...
- mysql数据库常规命令操作
1.MySQL数据库导出命令 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcn ...
- 如何保护.net中的dll文件(防破解、反编译)
如何保护.net中的dll文件(防破解.反编译) 2010-07-19 15:08 [小 大] 来源: 赛迪网 评论: 0 分享至: 百度权重查询 词库网 网站监控 服务器监控 SEO监控 ...
- VMware/Microsoft官网查询参加的培训及认证信息
如果你参加了VMWare的培训,会要求你拿一个已经注册的邮箱加上一个密码在VMware的系统里面登记,这样你就能在VMWARE官网查到注册,并据此你才能申请VMWare的考试认证. 例如下图,路径为 ...
- Linux学习笔记——重点推荐的Linux网络在线学习资源
首先非常感谢百度,感谢网络的搜索引擎技术,也非常感谢学习资源的贡献者和组织! 1:http://billie66.github.io/TLCL/book/zh/ 2:http://www.ha97. ...
- linux命令小常识
作为一个tester我们必须要会linux,也许你会说不用也可以.那么我想问,你部署测试环境在哪里,你下载war包并部署war包呢,你看日志在哪里? 基于测试需要用到liunx,我这里只针对需要用到的 ...
- LVS+keepalived负载均衡实战
1 首先安装虚拟机 安装系统 这里 配置两台虚拟机 1:192.168.137.102 2:192.168.137.103 分别安装tomcat 默认80端口,同时都是开启状态 配置192.168 ...
- js原型基础
js中的原型链是实现继承等面向对象的一种方式,而我这里不详细展开,仅仅是画了一张原型部分结构图,主要说明: 1._proto_和prototype之间的关系 2.原型链的起点是Object.proto ...