POJ 题目2892 Tunnel Warfare(线段树单点更新查询,求单点所在最大连续区间长度)
2015-08-05 20:47
435 查看
Tunnel Warfare
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
Sample Output
Hint
An illustration of the sample input:
Source
POJ Monthly--2006.07.30, updog
ac代码
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 7307 | Accepted: 2997 |
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
Source
POJ Monthly--2006.07.30, updog
ac代码
#include<stdio.h> #include<string.h> #define max(a,b) (a>b?a:b) struct s { int rl,ll,ml; }node[50500<<2]; int stack[50500],top; void build(int l,int r,int tr) { node.ll=node .rl=node .ml=r-l+1; if(l==r) return; int mid=(l+r)>>1; build(l,mid,tr<<1); build(mid+1,r,tr<<1|1); } void update(int pos,int l,int r,int tr,int val) { if(l==r) { if(val) node .ll=node .rl=node .ml=1; else node .ll=node .rl=node .ml=0; return; } int mid=(l+r)>>1; if(pos<=mid) { update(pos,l,mid,tr<<1,val); } else update(pos,mid+1,r,tr<<1|1,val); node .ll=node[tr<<1].ll; node .rl=node[tr<<1|1].rl; node .ml=max(node[tr<<1].ml,node[tr<<1|1].ml); node .ml=max(node .ml,node[tr<<1].rl+node[tr<<1|1].ll); if(node[tr<<1].ll==mid-l+1) node .ll+=node[tr<<1|1].ll; if(node[tr<<1|1].rl==r-(mid+1)+1) node .rl+=node[tr<<1].rl; } int query(int pos,int l,int r,int tr) { if(l==r||node .ml==0||node .ml==r-l+1) { return node .ml; } int mid=(l+r)>>1; if(pos<=mid) { if(pos>=mid-node[tr<<1].rl+1) return query(pos,l,mid,tr<<1)+query(mid+1,mid+1,r,tr<<1|1); return query(pos,l,mid,tr<<1); } else { if(pos<=mid+1+node[tr<<1|1].ll-1) return query(pos,mid+1,r,tr<<1|1)+query(mid,l,mid,tr<<1); return query(pos,mid+1,r,tr<<1|1); } } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { build(1,n,1); top=0; while(m--) { char s[2]; int x; scanf("%s",s); if(s[0]=='D') { scanf("%d",&x); stack[top++]=x; update(x,1,n,1,0); } else { if(s[0]=='Q') { int x; scanf("%d",&x); int ans=query(x,1,n,1); printf("%d\n",ans); } else { int x=stack[top-1]; top--; update(x,1,n,1,1); } } } } } 相关文章推荐
- hdu 5351 MZL's Border(15多校第五场1009)
- hdu5344 MZL's xor(水题)
- tcp 多线程与多进程调用close
- VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径
- iOS- <项目笔记>项目配置常见文件
- 8.05(生产和消费线程)
- C语言:创建动态单向链表,创建完成后,输出每一个节点的数据信息。
- HDOJ1213(并查集)
- nyoj 47 过河问题(贪心)
- HDU 2553 N皇后问题
- hdu 5349 MZL's simple problem(15多校第五场1007)
- c++之简单I/O格式控制
- uva 12186 Another Crisis 树形dp
- 黑马程序员——break和continue语句+函数+函数重载-第4天
- Android-Activity之间数据传递的多种方式
- 学习JS权威指南记录
- 黑马程序员—JAVA基础—反射
- Android 四大组件之Content Provider
- Dividing
- Android Studio 视图解析