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. Python 强大而易用的文件操作(转载)

    在Python中可以很方便地做一些诸如浏览目录,创建文件夹,删除文件夹等等的操作. 对文件系统的访问大多通过os模块来实现,因为Python是多平台的,os模块只是前端,具体的实现还是由具体的系统来完 ...

  2. 【web安全】第二弹:XSS攻防中的复合编码问题

    最近一直在研究XSS的攻防,特别是dom xss,问题慢慢的迁移到浏览器编码解码顺序上去. 今儿被人放鸽子,无奈在KFC看了两个小时的资料,突然有种豁然开朗的感觉. 参考资料先贴出来: 1. http ...

  3. iostream/fstream中的输入输出流指针的绑定,tie函数的使用。

      为了兼容c语言的输入输出,c++里面采用tie将输入输出流经行绑定,所以cin/cout并不是独立的.当执行cin时,cout同时会被执行.反之亦然. by defalut,cin is tied ...

  4. java 动态代理理解

    动态代理,顾名思义就是动态创建一个代理对象,无需手动为被代理类创建一个代理类,java的动态代理通过Proxy类和Invocation接口实现,代码如下: //被代理接口 public interfa ...

  5. SpeedPHP关于一对一和一对多关联关系的建立 model建立

    新闻表:t_news 新闻类型表:b_type_to_name 其中一个新闻类型可以包含多个新闻(hasmany),一个新闻只能属于一种新闻类型(hasone) 下面是新闻model类: <?p ...

  6. android usb Host模式下与usb Hid 设备的通信

    做android 与USB HID设备的通信有段时间了,总结一下遇到的问题和解决方法: 1,第一次遇到的问题:android 版本低不支持usb hid, 被要求做相关项目的时候,就从mUsbMana ...

  7. 【Linux安全】安全口令策略设置

    命令: vim /etc/login.defs 默认设置: # Password aging controls: # # PASS_MAX_DAYS Maximum number of days a ...

  8. linux shell sleep/wait(转载)

    linux shell sleep/wait(转载) 2007-04-27 18:12 bash的基本配置是由配置文件组成的./etc/profile称之为shell的全局配置文件.另外一个文件在个人 ...

  9. 【HDOJ】4267 A Simple Problem with Integers

    树状数组.Easy. /* 4267 */ #include <iostream> #include <string> #include <map> #includ ...

  10. tlplayer for wince 版本正式商用

    开始的时候tlplayer遇到一些问题,后来经过一些简单优化后,可以满足商用条件. 支持http,mms,rtsp等网络协议,支持内存流播放.需要定制或者需要支持hls,rtmp,m3u8等协议的,请 ...