【HDU - 1540】Tunnel Warfare 【线段树+单点更新+区间合并】
2017-10-23 18:16
429 查看
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 o
4000
f 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
题意 :原来有n个点,起始每个点上都是1,之后有m个操作
1> D x 将序号x处变为0
2> R 将最近被变为0的恢复为1
3> Q x 询问x所在区间中,连续的1的个数 。
分析 : 维护区间的连续个数问题,都可以用区间合并来解决。
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 o
4000
f 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
题意 :原来有n个点,起始每个点上都是1,之后有m个操作
1> D x 将序号x处变为0
2> R 将最近被变为0的恢复为1
3> Q x 询问x所在区间中,连续的1的个数 。
分析 : 维护区间的连续个数问题,都可以用区间合并来解决。
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>pii; #define first fi #define second se #define LL long long #define fread() freopen("in.txt","r",stdin) #define fwrite() freopen("out.txt","w",stdout) #define CLOSE() ios_base::sync_with_stdio(false) const int MAXN = 5e4+11; const int MAXM = 1e6; const int mod = 1e9+7; const int inf = 0x3f3f3f3f; struct Tree{ int l,r,len; int lsum,rsum,sum; }tree[MAXN<<2]; void Up(int o){ // 重点 tree[o].lsum=tree[o<<1].lsum; tree[o].rsum=tree[o<<1|1].rsum; tree[o].sum=max(tree[o<<1].sum,tree[o<<1|1].sum); if(tree[o<<1].lsum==tree[o<<1].len) tree[o].lsum+=tree[o<<1|1].lsum; if(tree[o<<1|1].rsum==tree[o<<1|1].len) tree[o].rsum+=tree[o<<1].rsum; tree[o].sum=max(tree[o].lsum,tree[o].rsum) ; tree[o].sum=max(tree[o].sum,tree[o<<1].rsum+tree[o<<1|1].lsum); } void Build(int o,int le,int ri){ tree[o].l=le,tree[o].r=ri,tree[o].len=ri-le+1; tree[o].lsum=tree[o].rsum=tree[o].sum=1; if(le==ri) return ; int mid=(tree[o].l+tree[o].r)>>1; Build(o<<1,le,mid); Build(o<<1|1,mid+1,ri); Up(o); } void Update(int o,int pos,int val){ if(tree[o].l==tree[o].r) { tree[o].lsum=tree[o].rsum=tree[o].sum=val; return ; } int mid=(tree[o].l+tree[o].r)>>1; if(pos<=mid) Update(o<<1,pos,val); else Update(o<<1|1,pos,val); Up(o); } int Query(int o,int pos){ if(tree[o].l==tree[o].r) { return tree[o].sum; } int mid=(tree[o].l+tree[o].r)>>1; if(pos<=mid) { // 因为是点,所以可能和分开的右半部分有连接 int ans=0; if(pos>=tree[o<<1].r-tree[o<<1].rsum+1) return ans=tree[o<<1].rsum+tree[o<<1|1].lsum; else return Query(o<<1,pos); }else { int ans=0; if(tree[o<<1|1].lsum+tree[o<<1|1].l-1>=pos) return ans=tree[o<<1|1].lsum+tree[o<<1].rsum; else return Query(o<<1|1,pos); } } int main(){ CLOSE(); // fread(); // fwrite(); int n,m; while(~scanf("%d%d",&n,&m)){ Build(1,1,n); stack<int>S; char op[5]; while(m--){ scanf("%s",op); if(op[0]=='Q'){ int a;scanf("%d",&a); printf("%d\n",Query(1,a)); }else { int a; if(op[0]=='D'){ scanf("%d",&a); S.push(a); Update(1,a,0); } else{ if(!S.empty()){ a=S.top(); S.pop(); Update(1,a,1); } } } } } return 0; }
相关文章推荐
- 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 - 1540Tunnel Warfare(线段树 单点更新 区间查询)
- hdu 1540(线段树单点更新 区间合并)
- hdu1540之线段树单点更新+区间合并
- HDU 1540 Tunnel Warfare(单点更新,区间合并)
- hdu 1540 Tunnel Warfare(线段树区间合并)
- 【HDU - 3308】 LCIS 【线段树+单点更新+区间合并】
- Tunnel Warfare HDU - 1540 单点更新 区间合并 线段树
- I - Tunnel Warfare - hdu 1540(区间合并更新)
- hdu 1540 Tunnel Warfare 线段树 区间合并
- [HDU 5316] Magician (线段树+单点更新+区间询问+区间合并)
- hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并
- POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)
- Hdu 1540 Tunnel Warfare【线段树区间合并学习】