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 ...
随机推荐
- 设置nginx禁止通过IP访问服务器的方法
在Nginx上设置禁止通过IP访问服务器,只允许通过域名访问,以避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网. nginx的默认虚拟主机允许用户通过IP访问,或者通过未设置的域名访问 ...
- perl命令批量替换文件内容
转自:http://www.jbxue.com/article/12638.html 使用perl命令批量替换文件内容. 对linux下的文件内容进行替换,有时不用编写perl脚本,用perl命令就可 ...
- puppet中anchor的作用
anchor出现背景:Puppet Forge是一个网上的module仓库,许多人写的puppet module会传上去,供大家下载使用.大家下载了一个module可以直接使用,不应该再来改动里面ma ...
- WPF中Application.Current的使用
WPF程序对应一个Application对象,当前的Application对象可以通过Application.Current获取,通过获取到的Application对象,我们可以做以下的事情: App ...
- 【spring配置】 一组配置文件引出的问题
applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans x ...
- C# - 参数数组Params
参数数组可以使用个数不定的参数调用函数,此时用params关键字定义它们. 限制:必须是函数定义中的最后一个参数:必须是同一类型的. 优点:不用在调用代码中传入数组,指定参数个数不受限制,可以不指定该 ...
- Hadoop集群(第10期)_MySQL关系数据库
1.MySQL安装 MySQL下载地址:http://www.mysql.com/downloads/ 1.1 Windows平台 1)准备软件 MySQL版本:mysql-5.5.21-win32. ...
- PHP之序列化与反序列化讲解
serialize() 把变量和它们的值编码成文本形式 unserialize() 恢复原先变量 eg: $stooges = array('Moe','Larry','Curly');$new = ...
- 关联规则算法Apriori的学习与实现
转自关联规则算法Apriori的学习与实现 首先我们来看,什么是规则?规则形如"如果-那么-(If-Then-)",前者为条件,后者为结果.关联规则挖掘用于寻找给定数据集中项之间的 ...
- win7 不能启动 memcached 总是反回failde to start service
原地址: http://zhidao.baidu.com/link?url=Ul9hJxFckU9IHWRy0pcxT11f2c0-p2uXkXhLria73mLNxYuV7IiaKYRtIl6vED ...