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

  1. 7 9
  2. D 3
  3. D 6
  4. D 5
  5. Q 4
  6. Q 5
  7. R
  8. Q 4
  9. R
  10. Q 4

Sample Output

  1. 1
  2. 0
  3. 2
  4. 4

Hint

An illustration of the sample input:

  1. OOOOOOO

  2. D 3 OOXOOOO

  3. D 6 OOXOOXO

  4. D 5 OOXOXXO

  5. R OOXOOXO

  6. 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位于右子同理。

【代码】

  1. #include<cstdio>
  2. #include<iostream>
  3. #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
  4. using namespace std;
  5.  
  6. const int N = 1e5+;
  7.  
  8. struct Trie{ int l,r,ls,rs,ms; }T[N<<];
  9.  
  10. void maintain(int u) {
  11. int lc=u<<,rc=lc|;
  12. T[u].ls=T[lc].ls,T[u].rs=T[rc].rs;
  13. if(T[lc].ls==T[lc].r-T[lc].l+) T[u].ls+=T[rc].ls;
  14. if(T[rc].rs==T[rc].r-T[rc].l+) T[u].rs+=T[lc].rs;
  15. T[u].ms=max(T[lc].ms,T[rc].ms);
  16. T[u].ms=max(T[u].ms,T[lc].rs+T[rc].ls);
  17. }
  18. void build(int u,int L,int R) {
  19. T[u].l=L,T[u].r=R;
  20. if(L==R) {
  21. T[u].ls=T[u].rs=T[u].ms=R-L+;
  22. return ;
  23. }
  24. int M=(L+R)>>;
  25. build(u<<,L,M); build(u<<|,M+,R);
  26. maintain(u);
  27. }
  28. void update(int u,int r,int x) {
  29. if(T[u].l==T[u].r)
  30. T[u].ms=T[u].ls=T[u].rs=x;
  31. else {
  32. int M=(T[u].l+T[u].r)>>;
  33. if(r<=M) update(u<<,r,x);
  34. else update(u<<|,r,x);
  35. maintain(u);
  36. }
  37. }
  38. int query(int u,int x) {
  39. if(T[u].l==T[u].r || T[u].ms== || T[u].ms==T[u].r-T[u].l+)
  40. return T[u].ms;
  41. int M=(T[u].l+T[u].r)>>,lc=u<<,rc=lc|;
  42. if(x<=M) {
  43. if(x>=T[lc].r-T[lc].rs+)
  44. return query(lc,x)+query(rc,M+);
  45. else return query(lc,x);
  46. } else {
  47. if(x<=T[rc].l+T[rc].ls-)
  48. return query(rc,x)+query(lc,M);
  49. else return query(rc,x);
  50. }
  51. }
  52.  
  53. int n,m,s[N],top,f[N];
  54. char op[];
  55. void read(int& x) {
  56. char c=getchar(); int f=; x=;
  57. while(!isdigit(c)) {if(c=='-')f=-;c=getchar();}
  58. while(isdigit(c)) x=x*+c-'',c=getchar();
  59. x*=f;
  60. }
  61. int main() {
  62. read(n),read(m);
  63. build(,,n);
  64. int x;
  65. while(m--) {
  66. scanf("%s",op);
  67. switch(op[]) {
  68. case 'D':
  69. read(x);update(,x,);s[++top]=x;f[x]=;
  70. break;
  71. case 'R':
  72. if(top)
  73. update(,s[top],),f[s[top]]=,--top;
  74. break;
  75. case 'Q':
  76. read(x);
  77. if(f[x]) puts("");
  78. else printf("%d\n",query(,x));
  79. break;
  80. }
  81. }
  82. return ;
  83. }

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. MAC自带的lnmp

    MAC自身带有apache,php. 1.启动apache服务   sudo apachectl start/restart/stop 2.查看php的版本  php -v 3.让apache支持ph ...

  2. Erlang 开发者的福音:IntelliJ IDEA 的 Erlang 插件

    IntelliJ IDEA 的 Erlang 插件,主要特性: 智能编辑器:  Erlang 代码补全.语法和错误高亮.代码检查 代码导航:项目和文件结构视图.在文件.模型.函数和用例之间快速跳转 工 ...

  3. Web应用工作流程总结

    了解Web应用的工作过程有益于Web测试时更好的理解,Web应用工作的过程分为以下5个步骤: 1. 用户在Web浏览器中输入一个Web地址.选择一个超链接或点击一个按钮 2. Web浏览器将用户的动作 ...

  4. windows下使用MinGW的调试工具gdb.exe调试C程序

    1.编译源代码 C:MinGW\bin>gcc.exe -g -o program.exe program.c 编译选项上要加上“g”,这样生成的目标程序会含有调试内容,再用gdb调试的时候才能 ...

  5. linux下使用split 来分割大文件

    linux下使用split 来分割大文件 2010-07-27 15:46:27|  分类: 技术文稿 |  标签:split  分割  linux   |字号 订阅   平常都是使用ssh来进行远程 ...

  6. keil 51警告编译优化

    KeilC51中将工程中没有调用的函数不进行编译的方法 把Target Options中的Device页中选上"Use Extended Linker(LX51)instead of BL5 ...

  7. UIExtendedEdge

    在IOS7以后 ViewController 开始使用全屏布局的,而且是默认的行为通常涉及到布局.就离不开这个属性 edgesForExtendedLayout,它是一个类型为UIExtendedEd ...

  8. java.sql.SQLException: Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=186646784)(ERR=12505)(ERR

    dbc 链接orcal出错 java.sql.SQLException: Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=186646784)( ...

  9. 【转】spring3 MVC实战,手工搭建Spring3项目demo

    更新:这几天对spring3的理解又进了一步,今天抽空把这篇文章中的错误和不当之处做了修改. 最近的项目在用Spring3,涉及到了基于注解的MVC,事务管理,与hibernate的整合开发等内容,我 ...

  10. IndexReader和IndexWriter的生命周期

    http://youyang-java.iteye.com/blog/1731205 对于IndexReader而言,反复使用 IndexReader .open打开会有很大的开销,所以一般在整个程序 ...