POJ 2892-Tunnel Warfare(线段树单点更新-炸毁修复城市隧道)
来源:互联网 发布:强制写作软件 编辑:程序博客网 时间:2024/05/29 13:24
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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample Output
1024
Hint
An illustration of the sample input:
OOOOOOOD 3 OOXOOOOD 6 OOXOOXOD 5 OOXOXXOR OOXOOXOR OOXOOOO
Source
题目意思:
解题思路:
#include<iostream>#include<cstdio>#include<iomanip>#include<cmath>#include<cstdlib>#include<cstring>#include<map>#include<algorithm>#include<vector>#include<queue>using namespace std;#define INF 0xfffffff#define MAXN 500005int sta[MAXN];//修复栈struct Node //树{ int l,r;//左右节点 int llen,rlen;//区间内的城市情况,连续个1的个数};Node tree[MAXN<<2];void PushUP(int i)//向上更新父节点的值{ tree[i].llen=tree[2*i].llen ; tree[i].rlen=tree[2*i+1].rlen ; if(tree[2*i].llen==tree[2*i].r-tree[2*i].l+1)//区间连续 tree[i].llen+=tree[2*i+1].llen; if(tree[2*i+1].rlen==tree[2*i+1].r-tree[2*i+1].l+1) tree[i].rlen+=tree[2*i].rlen;}void BuildTree(int i, int l, int r)//建树{ tree[i].l=l; tree[i].r=r; if(l==r) { tree[i].llen=tree[i].rlen=1;//初始状态都是连通的 return ; } BuildTree(2*i,l,(l+r)/2); BuildTree(2*i+1,(l+r)/2+1,r); PushUP(i);//继续更新}void Update(int i,int l,int r,int t,int op){ if(tree[i].l==tree[i].r) { tree[i].llen=tree[i].rlen=op; return ; } int mid=(tree[i].l+tree[i].r)/2; if(t<=mid) Update(2*i,l,r,t,op); else Update(2*i+1,l,r,t,op); PushUP(i);//修改完后,仍然要向上修正父节点的值}int Query(int i,int l,int r,int t){ if(tree[i].l==tree[i].r) return tree[i].llen; int mid=(tree[i].l+tree[i].r)/2; if(t<=mid) { if(tree[2*i].r-tree[2*i].rlen+1<=t)//t被包含在左子树连续区间内 return tree[2*i].rlen+tree[2*i+1].llen; Query(2*i,l,r,t); } else { if(tree[2*i+1].llen+tree[2*i+1].l-1>=t)//t被包含在右子树连续区间内 return tree[2*i].rlen+tree[2*i+1].llen; Query(2*i+1,l,r,t); }}int main(){#ifdef ONLINE_JUDGE#else freopen("G:/cbx/read.txt","r",stdin); //freopen("G:/cbx/out.txt","w",stdout);#endif int n,m; while(~scanf("%d%d\n",&n,&m)) { BuildTree(1,1,n);//建树 int cnt=0; for(int j=0; j<m; ++j) { char ch; cin>>ch; if(ch=='D')//摧毁 { int t; scanf("%d\n",&t); sta[cnt++]=t; Update(1,1,n,t,0); } else if(ch=='Q')//查询 { int t; scanf("%d\n",&t); printf("%d\n",Query(1,1,n,t)); } else if(ch=='R')//修复 { int t=sta[--cnt];//从栈中取出最后炸毁的序列号 Update(1,1,n,t,1); } } } return 0;}
- POJ 2892-Tunnel Warfare(线段树单点更新-炸毁修复城市隧道)
- poj 2892 Tunnel Warfare(线段树 单点更新 区间合并)
- POJ 题目2892 Tunnel Warfare(线段树单点更新查询,求单点所在最大连续区间长度)
- HDU 1540Tunnel Warfare Tunnel Warfare (线段树- 区间合并-单点更新查询)
- poj 2892 Tunnel Warfare(线段树)
- POJ 2892 Tunnel Warfare (线段树)
- HDU 1540——Tunnel Warfare(线段树,区间合并+单点更新+单点查询)
- HDU 1540 Tunnel Warfare(线段树 区间合并 +单点更新)
- POJ-2892-Tunnel Warfare(线段树)
- poj-1540---Tunnel Warfare (线段树)
- POJ 2892 Tunnel Warfare && HDOJ 1540 (线段树)
- HDU 1540 Tunnel Warfare(线段树单点更新+区间合并)
- hdu 1540 Tunnel Warfare(线段树单点更新+区间合并)
- hdu 1540 Tunnel Warfare(线段树——单点更新+区间合并)
- Tunnel Warfare(线段树)
- poj 2892 Tunnel Warfare(线段树#5/树状数组)
- POJ 2892 Tunnel Warfare 区间合并线段树
- poj 2892 Tunnel Warfare(线段树…
- 1004 Let the Balloon Rise
- 动态内存分配
- Educational Codeforces Round 19 A+B+C+E!
- NYOJ46 最少乘法次数(二进制思想)
- 2017.4.16 幂次方 思考记录
- POJ 2892-Tunnel Warfare(线段树单点更新-炸毁修复城市隧道)
- 关于Android单元测试
- java的枚举
- opencv学习(四十三)之图像的矩moments()
- mysql安装包方式安装及配置(Windows)
- How tomcat works——19 管理Servlet
- 初次接触堆排序
- propertyAnimator(属性动画)之objectanimator(动画执行类)
- HDU 1520 Anniversary party (树型dp)