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. Sersync实时同步企业应用配置实战

    一.实验环境 CentOS版本: 6.6(2.6.32.-504.el6.x86_64) Rsync版本:  Rsync-3.0.6(系统自带) Sersync版本:sersync2.5.4_64bi ...

  2. post请求json内容丢失问题

    今天在项目组用json传输数据 post方法提交 发现传输过去的数据json内的+ 号被直接干掉了. 后来传输之前直接先编码. 接收端: public void ProcessRequest(Http ...

  3. Oracle的rowid结构解析

    SQL> select rowid,deptno from dept; ROWID                  DEPTNO ------------------ ---------- A ...

  4. MongoDB 覆盖索引查询

    MongoDB 覆盖索引查询 官方的MongoDB的文档中说明,覆盖查询是以下的查询: 所有的查询字段是索引的一部分 所有的查询返回字段在同一个索引中 由于所有出现在查询中的字段是索引的一部分, Mo ...

  5. C#基础|面向对象之继承

      面向对象的世界 在现实的世界里,发现事物可以进行分类,并且各个分类中又有这关系. 在面向对象的世界里,人们用类来模拟现实世界中的各种关系. 从大的范围来说,人属于人类,如果按照不同的身份将人类进行 ...

  6. C++编写操作系统(1):基于 EFI 的 Bootloader

    很久以前就对操作系统很好奇,用了这么多年Windows,对他的运作机理也不是很清楚,所以一直想自己动手写一个,研究一下操作系统究竟是怎么实现的.后来在网上也找到过一些教程(比如:<自己动手写操作 ...

  7. asp.net中对象的序列化,方便网络传输

    对象序列化 是将对象状态转换为可保持或传输的格式的过程.反序列化 是将流转换为对象序列化和反序列化相结合 可以使对象数据轻松的存储和传递 在 .NET 中,如果是对象可序列化,需要在 声明对象的开始部 ...

  8. jvm 性能调优

    [转载]:http://blog.csdn.net/chen77716/article/details/5695893 最近因项目存在内存泄漏,故进行大规模的JVM性能调优 , 现把经验做一记录. 一 ...

  9. [jobdu]最小的K个数

    一开始马上想起来寻找第k小的数,是采用快排的partition方法.但因为题目要把k之前的数排序输出,这个方法就不是很合适,因为(随机化后:http://blog.csdn.net/liangbopi ...

  10. Qt刷新机制的一些总结(Qt内部画的时候是相当于画在后台一个对象里,然后在刷新的时候调用bitblt统一画,调用window的api并不会影响到后面的那个对象)

    前段时间做过一个界面刷新的优化,遇到的坑比较多,在这里做一点点总结吧.     优化的方案是滚动滚动条的时候用截屏的方式代替界面全部刷新,优化完成后,界面在滚动时效率能提升大概一倍,背景介绍完毕.   ...