HDU1540-Tunnel Warfare(线段树区间合并)
来源:互联网 发布:mac电脑分享wifi给手机 编辑:程序博客网 时间:2024/05/22 02:10
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8634 Accepted Submission(s): 3337
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
用三个变量分别存储左边最大连续区间,右边最大连续区间,最大连续区间
注意:此处的左边最大连续区间和右边最大连续区间都是区间内的
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define maxn 50005#define mem(a,b) memset(a,b,sizeof(a))using namespace std;struct node{ int le,ri,ls,ms,rs;//ls,rs分别表示左端,右端最大连续区间,ms表示最大连续区间 int mid() { return (le+ri)>>1; }}t[maxn<<2];int book[maxn];void build(int l,int r,int rt){ t[rt].le=l,t[rt].ri=r; t[rt].ls=t[rt].ms=t[rt].rs=r-l+1; if(l==r) { return; } int m=t[rt].mid(); build(l,m,rt<<1); build(m+1,r,rt<<1|1);}void pushup(int rt){ t[rt].ls=t[rt<<1].ls; t[rt].rs=t[rt<<1|1].rs; t[rt].ms=max(max(t[rt<<1].ms,t[rt<<1|1].ms),t[rt<<1].rs+t[rt<<1|1].ls); //最大连续区间取子节点最大连续区间 和 左子节点右边最大连续区间+右子节点左边最大连续区间之和 两者的较大值 if(t[rt<<1].ls==t[rt<<1].ri-t[rt<<1].le+1) t[rt].ls+=t[rt<<1|1].ls; //判断左子节点的最大连续左区间在此区间内是否已满,如果满了还要加上右子节点的最大连续左区间 if(t[rt<<1|1].rs==t[rt<<1|1].ri-t[rt<<1|1].le+1) t[rt].rs+=t[rt<<1].rs; //同理}void update(int x,int rt,int op)//更新就不用说了{ if(t[rt].le==t[rt].ri) { t[rt].ls=t[rt].ms=t[rt].rs=op; return; } int m=t[rt].mid(); if(x<=m) update(x,rt<<1,op); else update(x,rt<<1|1,op); pushup(rt);}int query(int x,int rt){ if((t[rt].ri==t[rt].le)||(t[rt].ms==0)||(t[rt].ms==t[rt].ri-t[rt].le+1))//当到达叶节点或者该区间为空或已满直接返回 { return t[rt].ms; } int m=t[rt].mid(); if(x<=m) { //x<=m时看左子树,(t[rt<<1].ri-t[rt<<1].rs+1)表示左子节点的最右端点 //如果m在左子树中,但m的值>=左子节点的最右区间,此时就意味着左子节点的右端最大连续区间和右子节点的左端最大连续区间相连,那么就还要加上右子节点的情况 if(x>=t[rt<<1].ri-t[rt<<1].rs+1) return query(x,rt<<1)+query(m+1,rt<<1|1); else //否则直接返回左子节点的情况 return query(x,rt<<1); } else { //同理,判断右子节点的左边最大连续区间是否与左子节点的右边相连 if(x<=t[rt<<1|1].le+t[rt<<1|1].ls-1) return query(x,rt<<1|1)+query(m,rt<<1); else return query(x,rt<<1|1); }}int main(){ int n,m,x; char op[4]; while(~scanf("%d%d",&n,&m)) { int tot=0; build(1,n,1); for(int i=0;i<m;i++) { scanf("%s",op); if(op[0]=='D') { scanf("%d",&x); book[++tot]=x; update(x,1,0); } else if(op[0]=='Q') { scanf("%d",&x); printf("%d\n",query(x,1)); } else { if(tot>0) { x=book[tot--]; update(x,1,1); } } } } return 0;}
阅读全文
0 0
- HDU1540:Tunnel Warfare(线段树区间合并)
- hdu1540 Tunnel Warfare 线段树区间合并
- HDU1540-Tunnel Warfare-线段树区间合并
- hdu1540 Tunnel Warfare 线段树区间合并
- HDU1540 Tunnel Warfare (线段树区间合并)
- hdu1540-Tunnel Warfare 线段树区间合并
- HDU1540 Tunnel Warfare(线段树,区间合并)
- HDU1540 Tunnel Warfare (线段树区间合并)
- HDU1540 Tunnel Warfare(线段树+区间合并)
- HDU1540 Tunnel Warfare(线段树区间合并)
- hdu1540 Tunnel Warfare(线段树区间合并详解)
- HDU1540-Tunnel Warfare(线段树区间合并)
- HDU1540 Tunnel Warfare(线段树区间合并 | 线段树新姿势)
- hdu1540 Tunnel Warfare(线段树)
- poj2892&&HDU1540 Tunnel Warfare(线段树)
- Tunnel Warfare(线段树区间合并问题)
- 【Hdu】1540 Tunnel Warfare(线段树|区间合并)
- HDU 1540 Tunnel Warfare(线段树区间合并)
- list -- set方法——替换列表中指定位置的元素
- 第十二章 构建API
- 集合框架源码学习——ArrayList
- Web.xml配置详解之context-param
- linux下安装nodejs的 webpack的问题解决
- HDU1540-Tunnel Warfare(线段树区间合并)
- Winidows下版本控制Git + TortoiseGit安装使用
- Android系统休眠---踩坑的路
- 乐驾 -- 5
- intellj idea 如何设置类头注释和方法注释
- iOS中改变键盘的高度 监听键盘
- 【HNOI2008】bzoj1005 明明的烦恼
- epage 学习笔记第三节
- Android将jar包转换为dex二进制文件