hdu 1540 Tunnel Warfare 线段数区间合并
2016-07-20 15:13
246 查看
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)[align=left]Problem Description[/align]
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!
[align=left]Input[/align]
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.
[align=left]Output[/align]
Output the answer to each of the Army commanders’ request in order on a separate line.
[align=left]Sample Input[/align]
7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
[align=left]Sample Output[/align]
1
0
2
4
[align=left]Source[/align]
POJ Monthly
思路:线段数区间合并;
#include<bits/stdc++.h> using namespace std; #define ll __int64 #define esp 0.00000000001 const int N=1e5+10,M=1e6+10,inf=1e9+10,mod=1000000007; int max(int x,int y,int z) { return max(x,max(y,z)); } struct is { int l,r,len,lans,rans,sans;//这个区间的左端连续最大,右端,区间连续最大 }tree[N*4]; void buildtree(int l,int r,int pos) { tree[pos].l=l; tree[pos].r=r; tree[pos].len=tree[pos].lans=tree[pos].rans=tree[pos].sans=(r-l+1); if(l==r) return; int mid=(l+r)>>1; buildtree(l,mid,pos<<1); buildtree(mid+1,r,pos<<1|1); } void update(int point,int change,int pos) { if(tree[pos].l==point&&tree[pos].r==tree[pos].l) { tree[pos].len=tree[pos].lans=tree[pos].rans=tree[pos].sans=change; return; } int mid=(tree[pos].l+tree[pos].r)>>1; if(point<=mid) update(point,change,pos<<1); else update(point,change,pos<<1|1); tree[pos].lans=tree[pos<<1].lans; tree[pos].rans=tree[pos<<1|1].rans; tree[pos].sans=max(tree[pos<<1].sans,tree[pos<<1|1].sans,tree[pos<<1].rans+tree[pos<<1|1].lans); if(tree[pos<<1].lans==tree[pos<<1].r-tree[pos<<1].l+1) tree[pos].lans+=tree[pos<<1|1].lans; if(tree[pos<<1|1].rans==tree[pos<<1|1].r-tree[pos<<1|1].l+1) tree[pos].rans+=tree[pos<<1].rans; } int query(int x,int pos) { if(tree[pos].l==x&&tree[pos].r==x||tree[pos].sans==0||tree[pos].sans==tree[pos].r-tree[pos].l+1) { return tree[pos].sans; } int mid=(tree[pos].l+tree[pos].r)>>1; if(x<=mid) { if(x>=tree[pos<<1].r-tree[pos<<1].rans+1) return query(x,pos<<1)+query(mid+1,pos<<1|1); else return query(x,pos<<1); } else { if(x<=tree[pos<<1|1].l+tree[pos<<1|1].lans-1) return query(x,pos<<1|1)+query(mid,pos<<1); else return query(x,pos<<1|1); } } char a[10]; int main() { int x,y,z,i,t; int T; while(~scanf("%d%d",&x,&y)) { stack<int>s; buildtree(1,x,1); for(i=0;i<y;i++) { scanf("%s",a); if(a[0]=='D') { scanf("%d",&z); update(z,0,1); s.push(z); } else if(a[0]=='Q') { scanf("%d",&z); printf("%d\n",query(z,1)); } else { update(s.top(),1,1); s.pop(); } } } return 0; }
相关文章推荐
- 二维数组的创建与遍历(php)
- 一起深入理解BFC~
- oracle 11g 测试服务器修改system密码时显示???解决方案
- Codeforces Round #363 (Div. 2)
- JAVA常见面试题及解答
- 一个月开发微信蓝牙四旋翼飞机
- 简单实现断点下载
- 手机直播可二次开发、萌颜、连麦、弹幕,首选云豹直播
- NOIPTG A组总结&T2题解
- C# 数组拆分
- 盛水最大量---贪心算法
- mysql 编译参数详解
- Javascript 基础之Number对象
- 脏数据清洗
- Cpp环境【OpenJudge3344】【Vijos2874】冷血格斗场
- csdn markdown 使用方法,快捷键
- threadlocal与数据库连接池
- hdu 5726 GCD 2016多校赛第一场
- rxjava+retrofit+okhttp 打造通用接口
- 孪生素数问题 &&一种排序 && 大数阶乘 &&筛素(2)