HDU - 1540 Tunnel Warfare —— 区间合并
2017-09-19 23:42
239 查看
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9906 Accepted Submission(s): 3877
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!
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 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
Sample Output
1
0
2
4
题意:有好几个村庄 D表示破坏村庄 R表示修复村庄 Q表示包括x的最大区间
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <stack> #include <vector> #define max_ 50010 #define inf 0x3f3f3f3f #define ll long long using namespace std; struct node { int l,r; int ls,rs,ms;//ls,左端最大连续区间,rs右端最大连续区间,ms区间内最大连续区间 }; struct node tree[max_*4]; int n,m; stack<int> s; void build(int i,int l,int r) { tree[i].l=l; tree[i].r=r; tree[i].ls=tree[i].rs=tree[i].ms=r-l+1; if(l==r) return; int mid=(l+r)>>1; build(i<<1,l,mid); build(i<<1|1,mid+1,r); } void update(int i,int x,int v) { if(tree[i].l==x&&tree[i].r==x) { tree[i].ls=tree[i].rs=tree[i].ms=v; return; } int mid=(tree[i].l+tree[i].r)>>1; if(x<=mid) update(i<<1,x,v); else update(i<<1|1,x,v); tree[i].ls=tree[i<<1].ls;//左区间 tree[i].rs=tree[i<<1|1].rs;//右区间 tree[i].ms=max(max(tree[i<<1].ms,tree[i<<1|1].ms),tree[i<<1].rs+tree[i<<1|1].ls); //父亲区间内的最大区间必定是,左子树最大区间,右子树最大区间,左右子树合并的中间区间,三者中最大的区间值 if(tree[i<<1].ls==tree[i<<1].r-tree[i<<1].l+1)//左子树区间满了的话,父亲左区间要加上右孩子的左区间 tree[i].ls+=tree[i<<1|1].ls; if(tree[i<<1|1].rs==tree[i<<1|1].r-tree[i<<1|1].l+1) tree[i].rs+=tree[i<<1].rs; } int query(int i,int x) { if(tree[i].l==tree[i].r||tree[i].ms==0||tree[i].ms==tree[i].r-tree[i].l+1)//到了叶子节点或者该访问区间为空或者已满都不必要往下走了 return tree[i].ms; int mid=(tree[i].l+tree[i].r)>>1; if(x<=mid) { if(x>=tree[i<<1].r-tree[i<<1].rs+1)//因为t<=mid,看左子树,a[2*i].r-a[2*i].rs+1代表左子树右边连续区间的左边界值 //如果t在左子树的右区间内,则要看右子树的左区间有多长并返回 return query(i<<1,x)+query(i<<1|1,mid+1); else return query(i<<1,x);//如果不在左子树的右边界区间内,则只需要看左子树 } else { if(x<=tree[i<<1|1].l+tree[i<<1|1].ls-1) return query(i<<1,mid)+query(i<<1|1,x); else return query(i<<1|1,x); } } int main(int argc, char const *argv[]) { while(scanf("%d%d",&n,&m)!=EOF) { while(s.size()) s.pop(); memset(tree,0,sizeof(tree)); build(1,1,n); while(m--) { char c; int x; scanf(" %c",&c); if(c=='D') { scanf("%d",&x); update(1,x,0); s.push(x); } else if(c=='Q') { scanf("%d",&x); printf("%d\n",query(1,x)); } else if(c=='R') { update(1,s.top(),1); s.pop(); } } } return 0; }
相关文章推荐
- HDU 1540——Tunnel Warfare(线段树,区间合并+单点更新+单点查询)
- 【HDU - 1540】Tunnel Warfare 【线段树+单点更新+区间合并】
- HDU 1540 Tunnel Warfare(线段树区间合并)
- HDU1540 Tunnel Warfare (线段树区间合并)
- hdu--1540 Tunnel Warfare(线段树+区间合并)
- hdu 1540 & poj 2892 Tunnel Warfare 线段树区间合并
- HDU 1540 Tunnel Warfare(线段树区间合并)
- HDU 1540 Tunnel Warfare(线段树区间合并)
- hdu1540 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(线段树单点更新+区间合并)
- HDU1540 Tunnel Warfare(线段树区间合并 | 线段树新姿势)
- hdu 1540 Tunnel Warfare(单点更新,区间合并)