Tunnel Warfare(线段树+查找最大值最小值+模板)
来源:互联网 发布:sql视图和表的区别 编辑:程序博客网 时间:2024/05/16 18:59
Tunnel Warfare
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 27 Accepted Submission(s) : 7
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.
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
Statistic | Submit | Back
参考:http://blog.csdn.net/chudongfang2015/article/details/52133243
题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少
思路:
首先,在某点时,用线段树求出这点之前的被破坏的村庄的最大值,这点之后的被破坏村庄的最小值,注意特殊情况(如最大值最小值均是本身),所以这就需要在建立树的时候,使得没有被破坏的村庄用0和n+1代替然后存入线段树的节点中去,然后最大值最小值套模板即可
注意在写更新操作是单点更新用了两个数,因为这样是为了以后的恢复,一个数代表村庄,一个数代表被恢复后得数(0或n+1)
代码:’
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define INF 0x3f3f3f3f#define maxn 50010struct Tree{int maxx;int minn;}tree[maxn<<2];//线段树结构体,用来求最大值最小值int n,m; int ds[maxn<<2];//保存被破坏的村庄,便于恢复//建树void build(int l,int r,int i){if(l==r){tree[i].maxx=0;tree[i].minn=n+1;return;}else{int mid=(l+r)>>1;build(l,mid,L(i));build(mid+1,r,R(i));tree[i].maxx=max(tree[L(i)].maxx,tree[R(i)].maxx);tree[i].minn=min(tree[L(i)].minn,tree[R(i)].minn);}}void update1(int s,int e,int l,int r,int i){ //更新求最大if(l==r){tree[i].maxx=e;return;}else{int mid=(l+r)>>1;if(s<=mid)update1(s,e,l,mid,L(i));elseupdate1(s,e,mid+1,r,R(i));tree[i].maxx=max(tree[L(i)].maxx,tree[R(i)].maxx);}}void update2(int s,int e,int l,int r,int i)//更新求最小{if(l==r){tree[i].minn=e;return;}else{int mid=(l+r)>>1;if(s<=mid)update2(s,e,l,mid,L(i));elseupdate2(s,e,mid+1,r,R(i));tree[i].minn=min(tree[L(i)].minn,tree[R(i)].minn);}}int query_max(int s,int e,int l,int r,int i){//查找区间最大值if(s<=l&&r<=e){return tree[i].maxx;}int mid=(l+r)>>1;int res=0;if(s<=mid)res=max(res,query_max(s,e,l,mid,L(i)));if(e>mid)res=max(res,query_max(s,e,mid+1,r,R(i)));return res;}int query_min(int s,int e,int l,int r,int i){//查找区间最小子if(s<=l&&r<=e){return tree[i].minn;}int mid=(l+r)>>1;int res=INF;if(s<=mid)res=min(res,query_min(s,e,l,mid,L(i)));if(e>mid)res=min(res,query_min(s,e,mid+1,r,R(i)));return res;}int main(){int i,ans,x;char ch[15];while(scanf("%d%d",&n,&m)!=EOF){ans=0;memset(ds,0,sizeof(ds));memset(tree,0,sizeof(tree));build(1,n,1);while(m--){scanf("%s",&ch);if(ch[0]=='D'){scanf("%d",&x);update1(x,x,1,n,1);update2(x,x,1,n,1);ds[++ans]=x;}else if(ch[0]=='Q'){int max1,min1;scanf("%d",&x);max1=query_max(1,x,1,n,1);min1=query_min(x,n,1,n,1);//据线段树查询if(max1==min1)printf("0\n");elseprintf("%d\n",(min1-max1-1));}else{int t=ds[ans--];update1(t,0,1,n,1);//恢复村庄,注意时0或n+1
update2(t,n+1,1,n,1);}}} return 0;}
阅读全文
0 0
- Tunnel Warfare(线段树+查找最大值最小值+模板)
- Tunnel Warfare(线段树)
- poj 2892 Tunnel Warfare(线段树)
- HDU 1540 Tunnel Warfare(线段树)
- POJ 2892 Tunnel Warfare (线段树)
- hdu1540 Tunnel Warfare(线段树)
- poj2892&&HDU1540 Tunnel Warfare(线段树)
- poj-1540---Tunnel Warfare (线段树)
- HDU_1540 Tunnel Warfare 线段树
- hdu Tunnel Warfare(线段树)
- poj2892--Tunnel Warfare(线段树)
- HDU 1540Tunnel Warfare Tunnel Warfare (线段树- 区间合并-单点更新查询)
- hdu1540 Tunnel Warfare(线段树)(好题)
- hdu 1540 Tunnel Warfare(线段树 连续区间)
- POJ 2892 Tunnel Warfare && HDOJ 1540 (线段树)
- 【Hdu】1540 Tunnel Warfare(线段树|区间合并)
- HDU 1540 Tunnel Warfare(线段树区间合并)
- HDU 1540 Tunnel Warfare (线段树区间合并)
- [Android]计算器源代码分析
- 初探RESTful——使用Grizzly和Jersey搭建RESTful环境
- bzoj题目总结
- VC++文件操作之最全篇
- Material Design
- Tunnel Warfare(线段树+查找最大值最小值+模板)
- struts2中form表单使用post方式提交参数(键值对)超过10000的限制
- CyclicBarri和 CountDownLatch的使用场景
- 作用域链和原型链
- 标识符的含义以及在定义标识符时的注意事项
- 测试报告HTMLTestRunner,python3
- mac电脑设置 ssh localhost免密码登陆 hadoop需要
- 99乘法
- OS2 Singlemode Simplex Vs. Duplex Patch Cable