poj 2892 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:
- D x: The x-th village was destroyed.
- Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
- 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(线段树)的更多相关文章
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
- hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】
Tunnel Warfare Time Limit: 4000/2000 MS ...
- POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)
点我看题目 题意 :N个村子连成一条线,相邻的村子都有直接的地道进行相连,不相连的都由地道间接相连,三个命令,D x,表示x村庄被摧毁,R ,表示最后被摧毁的村庄已经重建了,Q x表示,与x直接或间 ...
- hdu 1540 Tunnel Warfare(线段树区间统计)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- POJ 2892 Tunnel Warfare
传送门 很神奇的一道题,可以用线段树搞,为了练习treap所以拿treap写了. 其实根据询问,删除那个标号就加入平衡树,然后找到最大的和最小的就好了. 一些很烦人的小细节. //POJ 2892 / ...
- HDU 1540 Tunnel Warfare 线段树区间合并
Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...
- hdu1540 Tunnel Warfare 线段树/树状数组
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
Tunnel Warfare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 1540 Tunnel Warfare (线段树)
Tunnel Warfare Problem Description During the War of Resistance Against Japan, tunnel warfare was ca ...
随机推荐
- CSS display:inline和float:left两者区别探讨
本文和大家重点讨论一下CSS display:inline和float:left两者的区别,CSS display是指显示状态,inline表示内联,特点是紧贴着前一个内联元素,通常默认的内联元素有 ...
- CGAL 介绍
CGAL组织 内核 数值健壮 基础库 扩展性 2.4 命名约定 Naming In order to make it easier to remember what kind of entity a ...
- Hive(转)
Hive分区表 在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入了partition概念.分区表指的是在创建表时指 ...
- javaWeb中的/路径问题
在写javaweb的时候,在写路径的时候,/有时候表示站点根目录,有时候表示当前web应用根目录,究竟如何区分呢? 首先,我们建议开发的时候,跳转之类的都是用绝对路径(注意:不是物理路径),而不是使用 ...
- Unity3d Shader开发(二)SubShader
(1)SubShader Unity中的每一个着色器都包含一个subshader的列表,当Unity需要显示一个网格时,它能发现使用的着色器,并提取第一个能运行在当前用户的显示卡上的子着色器. 当Un ...
- 拖尾渲染器 Trail Renderer
拖尾渲染器(Trail Renderer)用于制作跟在场景中的物体后面的拖尾效果来代表它们在到处移动. 必须给Materials一个材质渲染器设置的Colors才有效. 展示自己的一个demo...
- Mysql不区分大小写
mysql在windows下表名是不区分大小写的,但是在linux下是区分大小写的: 今天帮朋友解决安装到ubuntu系统下mysql不区分大小写的问题,最初在/etc/my.cnf文件中的[mysq ...
- 静态页面参数传递&回调函数写法&快速排序的实现方法
相信很多人都有一种陋习,就是收藏的文章,几乎从来都没有回过头来仔细看过.这次借着这次活动的机会,在<无懈可击的web设计>一书的学习过程中,穿插着讲自己曾经收藏过的,现在觉得还有价值的文章 ...
- word-wrap同word-break的区别(转)
本文列举了兼容 IE 和 FF 的换行 CSS 推荐样式,详细介绍了word-wrap同word-break的区别. 兼容 IE 和 FF 的换行 CSS 推荐样式 最好的方式是 word-wrap: ...
- zepto源码学习-06 touch
先上菜,看这个模块的最后一段代码,一看就明白. ['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown', 'doubleTap', 't ...