HDU 1540 Tunnel Warfare 线段树
来源:互联网 发布:霜之哀伤淘宝 编辑:程序博客网 时间:2024/04/30 10:02
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4567 Accepted Submission(s): 1750
Problem 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!
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.
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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample Output
1024
Source
POJ Monthly
n个点,标号1----n,m次操作,D x 是破坏x点,R是修复当前最后一个被破坏的点,Q是询问某个点当前和几个点直接或间接连通(把自己那个点也算上),输出和当前点连通点的个数。
一开始以为是并查集,但感觉并查集太鸡肋,就用哈希和查找水了水,结果TLE,看分类是线段树,然后就往线段树上使劲的想,最后想出来了。
设置未被破坏是1,被破坏是0,线段树回溯的时候,如果左右子树都不为0,就回溯他们的和,否则回溯0,查询的时候查询和输入的点最近的断开的点,然后可以得结果。。。。。
#include <stdio.h>#include <string.h>#include <stack>using namespace std;int d[500010];//线段树数组bool hs[50010];//哈希数组,用来判某一个点是否被破坏stack <int> st;//栈,修复被破坏的点从最后一个开始int mx,mn;//小于要查询点的编号的最大值和大于要查找点的编号的最小值 //他们的差值-1就是结果void creat(int s,int t,int step)//线段树建树{ if(s >= t) { d[step] = 1; hs[s] = true;//一开始都没破坏 return ; } int mid = (s + t) >> 1; creat(s,mid,step << 1); creat(mid + 1,t,step << 1 | 1); d[step] = d[step << 1] + d[step << 1 | 1];}void upd(int s,int t,int us,int unum,int step)//更新{ if(s > us || t < us) return ; if(s >= t) { d[step] = unum; return ; } int mid = (s + t) >> 1; if(us <= mid) upd(s,mid,us,unum,step << 1); if(us > mid) upd(mid + 1,t,us,unum,step << 1 | 1); if(d[step << 1] && d[step << 1 | 1])//如果左右均不为0,回溯他们的和 d[step] = d[step << 1] + d[step << 1 | 1]; else d[step] = 0;//如果有一个为0,回溯0}void qu(int s,int t,int qs,int step)//查询{ if(s >= t) { if(mx < s && s < qs)//小于要查询点的编号的最大值 mx = s; if(mn > s && s > qs)//大于要查找点的编号的最小值 mn = s; return ; } int mid = (s + t) >> 1; if(!d[step << 1])//如果左子树为0,递归遍历 qu(s,mid,qs,step << 1); if(!d[step << 1 | 1])//如果右子树为0,递归遍历 qu(mid + 1,t,qs,step << 1 | 1);}int main(){ int n,m,sr; char ch[5]; while(~scanf("%d%d",&n,&m)) { creat(1,n,1); while(!st.empty()) st.pop(); while(m--) { scanf("%s",ch); if(ch[0] == 'D') { scanf("%d",&sr); hs[sr] = false; st.push(sr); upd(1,n,sr,0,1); } else if(ch[0] == 'Q') { scanf("%d",&sr); if(!hs[sr])//如果要查询的点被破坏,直接输出0 printf("0\n"); else { if(!d[1]) { mx = 0; mn = n + 1;//初始认为都没有被破坏,我们认为编号n + 1和0是被破坏了 qu(1,n,sr,1); printf("%d\n",mn - mx - 1); } else { printf("%d\n",n); } } } else if(ch[0] == 'R') { if(!st.empty()) { int cd = st.top(); st.pop(); upd(1,n,cd,1,1); hs[cd] = true; } } } } return 0;}
0 0
- hdu 1540 Tunnel Warfare 线段树
- hdu-1540-Tunnel Warfare-线段树
- 【HDU】 1540 Tunnel Warfare 线段树
- hdu 1540 Tunnel Warfare【线段树】
- hdu 1540 Tunnel Warfare(线段树)
- HDU 1540 Tunnel Warfare(线段树)
- HDU 1540 Tunnel Warfare 线段树
- HDU 1540 Tunnel Warfare [二分 + 线段树]
- HDU 1540 Tunnel Warfare (线段树)
- hdu 1540 Tunnel Warfare 线段树 / set
- hdu Tunnel Warfare(线段树)
- HDU 1540Tunnel Warfare Tunnel Warfare (线段树- 区间合并-单点更新查询)
- HDU 1540 Tunnel Warfare 【线段树--最大连续区间】
- hdu 1540 Tunnel Warfare(线段树 连续区间)
- POJ 2892 & HDU 1540 Tunnel Warfare (线段树)
- hdu 1540 Tunnel Warfare(线段树区间统计)
- HDU-1540 Tunnel Warfare 线段树 区间合并
- hdu 1540 Tunnel Warfare(线段树区间合并)
- 【c++】第一遍宏观总结
- Activity 数据交换的几种形式介绍
- 键盘上方自定义view
- CSS布局模型(三)——层模型
- HDU 2147 kiki's game 博弈
- HDU 1540 Tunnel Warfare 线段树
- ACM——01——1006: 【入门】倒序输出一个四位整数 【运算符】
- poj 1269 判断两直线位置
- label 首行缩进
- Socket常见错误代码与描述
- SQL sever2008服务无法启动,提示“远程过程调试失败···”
- 加载数据
- 二维标准正态分布的matlab方程
- eclipse中使用svn