Tunnel Warfare
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7499   Accepted: 3096

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

【题意】

一排村庄,需要操作:D(x)破坏村庄x,R回复最后破化的村庄,Q(x)与x相连的最长村庄数。

【思路】

线段树。

01表示村庄是否存在。维护ls rs ms三个信息。

Query:

    如果x位于左子,如果x位于左子的rs中则需要查询右子中与M+1相连的最长段,即query(u<<1,x)+query(u<<1|1,M+1);如果不处于rs中直接返回query(u<<1,x),X位于右子同理。

【代码】

 #include<cstdio>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int N = 1e5+; struct Trie{ int l,r,ls,rs,ms; }T[N<<]; void maintain(int u) {
int lc=u<<,rc=lc|;
T[u].ls=T[lc].ls,T[u].rs=T[rc].rs;
if(T[lc].ls==T[lc].r-T[lc].l+) T[u].ls+=T[rc].ls;
if(T[rc].rs==T[rc].r-T[rc].l+) T[u].rs+=T[lc].rs;
T[u].ms=max(T[lc].ms,T[rc].ms);
T[u].ms=max(T[u].ms,T[lc].rs+T[rc].ls);
}
void build(int u,int L,int R) {
T[u].l=L,T[u].r=R;
if(L==R) {
T[u].ls=T[u].rs=T[u].ms=R-L+;
return ;
}
int M=(L+R)>>;
build(u<<,L,M); build(u<<|,M+,R);
maintain(u);
}
void update(int u,int r,int x) {
if(T[u].l==T[u].r)
T[u].ms=T[u].ls=T[u].rs=x;
else {
int M=(T[u].l+T[u].r)>>;
if(r<=M) update(u<<,r,x);
else update(u<<|,r,x);
maintain(u);
}
}
int query(int u,int x) {
if(T[u].l==T[u].r || T[u].ms== || T[u].ms==T[u].r-T[u].l+)
return T[u].ms;
int M=(T[u].l+T[u].r)>>,lc=u<<,rc=lc|;
if(x<=M) {
if(x>=T[lc].r-T[lc].rs+)
return query(lc,x)+query(rc,M+);
else return query(lc,x);
} else {
if(x<=T[rc].l+T[rc].ls-)
return query(rc,x)+query(lc,M);
else return query(rc,x);
}
} int n,m,s[N],top,f[N];
char op[];
void read(int& x) {
char c=getchar(); int f=; x=;
while(!isdigit(c)) {if(c=='-')f=-;c=getchar();}
while(isdigit(c)) x=x*+c-'',c=getchar();
x*=f;
}
int main() {
read(n),read(m);
build(,,n);
int x;
while(m--) {
scanf("%s",op);
switch(op[]) {
case 'D':
read(x);update(,x,);s[++top]=x;f[x]=;
break;
case 'R':
if(top)
update(,s[top],),f[s[top]]=,--top;
break;
case 'Q':
read(x);
if(f[x]) puts("");
else printf("%d\n",query(,x));
break;
}
}
return ;
}

poj 2892 Tunnel Warfare(线段树)的更多相关文章

  1. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  2. hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】

    Tunnel Warfare                                                             Time Limit: 4000/2000 MS ...

  3. POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)

    点我看题目 题意 :N个村子连成一条线,相邻的村子都有直接的地道进行相连,不相连的都由地道间接相连,三个命令,D x,表示x村庄被摧毁,R  ,表示最后被摧毁的村庄已经重建了,Q x表示,与x直接或间 ...

  4. hdu 1540 Tunnel Warfare(线段树区间统计)

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

  5. POJ 2892 Tunnel Warfare

    传送门 很神奇的一道题,可以用线段树搞,为了练习treap所以拿treap写了. 其实根据询问,删除那个标号就加入平衡树,然后找到最大的和最小的就好了. 一些很烦人的小细节. //POJ 2892 / ...

  6. HDU 1540 Tunnel Warfare 线段树区间合并

    Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...

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

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

  8. hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

    Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. HDU 1540 Tunnel Warfare (线段树)

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

随机推荐

  1. Starting MySQL.. ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid).

      [root@localhost ~]# cd /usr/local/mysql   [root@localhost mysql]# chown -R mysql.mysql . [root@loc ...

  2. 简单的powershell 批量生成身份证复印件

    用Powshell 脚本批量完成,代码比较简单,出生日期,图片我都没有改,图片用于测试,附件. cls; $path = Split-Path -Parent $MyInvocation.MyComm ...

  3. js常见事件

    1.onblur:(使用在表单元素中,当元素失去焦点的时候执行) 2.onchange:(使用在表单元素中,当某些东西改变是执行) 3.onclick:(鼠标点击一个元素时执行) 4.ondblcli ...

  4. net.sf.json日期类型格式化输出

    net.sf.json 日期类型格式化输出 Date, Timestamp ; 编写工具类 package cn.jorcen.commons.util; import java.text.DateF ...

  5. Portal相关技术及架构

    Portal以用户为中心,提供统一的用户登录,实现信息的集中访问,集成了办公商务一体的工作流环境.利用Portal技术,可以方便地将员工所需要的,来源于各种渠道的信息资料集成在一个统一的桌面视窗之内. ...

  6. Tomacat服务器的安装和配置

    一, Tomcat服务器的下载地址(Apache Tomcat的官网):  http://tomcat.apache.org/download-70.cgi 这里为了稳定性安装的版本为7.0. 截止目 ...

  7. IOS 接ShareSDK问题

    如果报错AGCommon 等错误 引用libicucore.A.dylib ShareSDK 官网 管理中心 → 创建一个新的应用 获得key之后  啥都别做.! - (BOOL)applicatio ...

  8. WAF 与 RASP 的安装使用大比拼!

    什么是WAF和RASP? WAF全称是Web application firewall,即 Web 应用防火墙.RASP 全称是 Runtime Application Self-protect,即应 ...

  9. 为什么Nagios会那么吵?你又能做些什么呢?(1)

    如果你受困于 Nagios 的告警洪潮中不能自拔,那么这两篇连载博客就是为你而生的.让我们来详细的阐述下这个问题! 运维人员都有着独立的监控工具,因此会经常受到 Nagios 告警吵闹的影响.很多运维 ...

  10. 如何启动 SQL Server Agent(SQL Server 配置管理器)

    如何启动 SQL Server Agent(SQL Server 配置管理器) SQL Server 2008 R2 其他版本   4(共 6)对本文的评价是有帮助 - 评价此主题 可以从 SQL S ...