热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

POJ题目2892TunnelWarfare(线段树单点更新查询,求单点所在最大连续区间长度)

TunnelWarfareTimeLimit:1000MS MemoryLimit:131072KTotalSubmissions:7307

Tunnel Warfare















Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7307   Accepted: 2997


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:



  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint



An illustration of the sample input:


      OOOOOOO

D 3 OOXOOOO

D 6 OOXOOXO

D 5 OOXOXXO

R OOXOOXO

R OOXOOOO


Source


POJ Monthly--2006.07.30, updog


ac代码


#include
#include
#define max(a,b) (a>b?a:b)
struct s
{
int rl,ll,ml;
}node[50500<<2];
int stack[50500],top;
void build(int l,int r,int tr)
{
node[tr].ll=node[tr].rl=node[tr].ml=r-l+1;
if(l==r)
return;
int mid=(l+r)>>1;
build(l,mid,tr<<1);
build(mid+1,r,tr<<1|1);
}
void update(int pos,int l,int r,int tr,int val)
{
if(l==r)
{
if(val)
node[tr].ll=node[tr].rl=node[tr].ml=1;
else
node[tr].ll=node[tr].rl=node[tr].ml=0;
return;
}
int mid=(l+r)>>1;
if(pos<=mid)
{
update(pos,l,mid,tr<<1,val);
}
else
update(pos,mid+1,r,tr<<1|1,val);
node[tr].ll=node[tr<<1].ll;
node[tr].rl=node[tr<<1|1].rl;
node[tr].ml=max(node[tr<<1].ml,node[tr<<1|1].ml);
node[tr].ml=max(node[tr].ml,node[tr<<1].rl+node[tr<<1|1].ll);
if(node[tr<<1].ll==mid-l+1)
node[tr].ll+=node[tr<<1|1].ll;
if(node[tr<<1|1].rl==r-(mid+1)+1)
node[tr].rl+=node[tr<<1].rl;
}
int query(int pos,int l,int r,int tr)
{
if(l==r||node[tr].ml==0||node[tr].ml==r-l+1)
{
return node[tr].ml;
}
int mid=(l+r)>>1;
if(pos<=mid)
{
if(pos>=mid-node[tr<<1].rl+1)
return query(pos,l,mid,tr<<1)+query(mid+1,mid+1,r,tr<<1|1);
return query(pos,l,mid,tr<<1);
}
else
{
if(pos<=mid+1+node[tr<<1|1].ll-1)
return query(pos,mid+1,r,tr<<1|1)+query(mid,l,mid,tr<<1);
return query(pos,mid+1,r,tr<<1|1);
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,n,1);
top=0;
while(m--)
{
char s[2];
int x;
scanf("%s",s);
if(s[0]==&#39;D&#39;)
{
scanf("%d",&x);
stack[top++]=x;
update(x,1,n,1,0);
}
else
{
if(s[0]==&#39;Q&#39;)
{
int x;
scanf("%d",&x);
int ans=query(x,1,n,1);
printf("%d\n",ans);
}
else
{
int x=stack[top-1];
top--;
update(x,1,n,1,1);
}
}
}
}
}







版权声明:本文为博主原创文章,未经博主允许不得转载。


POJ 题目2892 Tunnel Warfare(线段树单点更新查询,求单点所在最大连续区间长度)



推荐阅读
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文探讨了如何通过最小生成树(MST)来计算严格次小生成树。在处理过程中,需特别注意所有边权重相等的情况,以避免错误。我们首先构建最小生成树,然后枚举每条非树边,检查其是否能形成更优的次小生成树。 ... [详细]
  • QUIC协议:快速UDP互联网连接
    QUIC(Quick UDP Internet Connections)是谷歌开发的一种旨在提高网络性能和安全性的传输层协议。它基于UDP,并结合了TLS级别的安全性,提供了更高效、更可靠的互联网通信方式。 ... [详细]
  • 2023 ARM嵌入式系统全国技术巡讲旨在分享ARM公司在半导体知识产权(IP)领域的最新进展。作为全球领先的IP提供商,ARM在嵌入式处理器市场占据主导地位,其产品广泛应用于90%以上的嵌入式设备中。此次巡讲将邀请来自ARM、飞思卡尔以及华清远见教育集团的行业专家,共同探讨当前嵌入式系统的前沿技术和应用。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 360SRC安全应急响应:从漏洞提交到修复的全过程
    本文详细介绍了360SRC平台处理一起关键安全事件的过程,涵盖从漏洞提交、验证、排查到最终修复的各个环节。通过这一案例,展示了360在安全应急响应方面的专业能力和严谨态度。 ... [详细]
author-avatar
Bleach1121
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有