HDU 1540 Tunnel Warfare
2017-02-05 12:51
246 查看
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 9D 6
D 5
Q 4
Q 5
R Q
4 R
Q 4
Sample Output
10
2
4
代码
//============================================================================ // Name : tree.cpp // Author : Qihan // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ //线段树区间合并 #include <bits/stdc++.h> using namespace std; #define pi acos(-1.0) #define Lowbit(x) (x & (-x)) #define lson l,mid,rt << 1 #define rson mid + 1,r,rt << 1 | 1 #define inf 0x3f3f3f #define maxn (50000 + 10) typedef long long int LLI; typedef pair<int,int> PII; struct Node { int l,r; int lm,rm,mm;//最左侧连续区间长度,最右侧连续区间长度,最大连续区间长度 = max(lson.mm,rson.mm,lson.rm + rson.lm); Node() {} Node(int _l,int _r,int _lm,int _rm,int _mm):l(_l),r(_r),lm(_lm),rm(_rm),mm(_mm) {} } tree[maxn << 2]; void build(int l,int r,int rt) { int s = r - l + 1; tree[rt] = Node(l,r,s,s,s); if(l == r) return; int mid = (l + r) >> 1; build(lson); build(rson); } void update(int in,int rt,int change) { if(tree[rt].l == tree[rt].r) { if(change == 1) { tree[rt].lm = 1; tree[rt].rm = 1; tree[rt].mm = 1; } else { tree[rt].lm = 0; tree[rt].rm = 0; tree[rt].mm = 0; } return; } int mid = (tree[rt].l + tree[rt].r) >> 1; if(in <= mid) update(in,rt << 1,change); else update(in,rt << 1 | 1,change); int maxs = max(tree[rt << 1].mm,tree[rt << 1 | 1].mm); maxs = max(maxs,tree[rt << 1].rm + tree[rt << 1 | 1].lm); tree[rt].lm = tree[rt << 1].lm; tree[rt].rm = tree[rt << 1 | 1].rm; tree[rt].mm = maxs; if (tree[rt << 1].lm == tree[rt << 1].r - tree[rt << 1].l + 1) tree[rt].lm += tree[rt << 1 | 1].lm; if (tree[rt << 1 | 1].rm == tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1) tree[rt].rm += tree[rt << 1].rm; //合并之前lson.lm的长度如果等于区间长度,那么合并后lm的长度 = lson.lm + rson.lm; //合并之前rson.rm的长度如果等于区间长度,那么合并后rm的长度 = rson.rm + lson.rm; } int Query(int in,int rt) { if(tree[rt].l == tree[rt].r) return tree[rt].mm; if(tree[rt].mm == 0) return tree[rt].mm; if(tree[rt].mm == tree[rt].r - tree[rt].l + 1) return tree[rt].mm; int mid = (tree[rt].l + tree[rt].r) >> 1; if(in <= mid) { if(in >= tree[rt << 1].r - tree[rt << 1].rm + 1) { return Query(in,rt << 1) + tree[rt << 1 | 1].lm; } else return Query(in,rt << 1); } else { if(in <= tree[rt << 1 | 1].l + tree[rt << 1 | 1].lm - 1) { return Query(in,rt << 1 | 1) + tree[rt << 1].rm; } return Query(in,rt << 1 | 1); } } stack<int> Sta; int main() { // freopen("/home/qihan/Documents/in","r",stdin); int n,m,in; char c; while(~scanf("%d%d",&n,&m)) { while(!Sta.empty()) Sta.pop(); build(1, n, 1); for (int i = 1; i <= m; i++) { cin >> c; if (c == 'D') { scanf("%d", &in); Sta.push(in); update(in, 1, 0); } else if (c == 'R') { in = Sta.top(); Sta.pop(); update(in, 1, 1); } else if (c == 'Q') { scanf("%d", &in); printf("%d\n", Query(in, 1)); } } } return 0; }
相关文章推荐
- HDU 1540 Tunnel Warfare(线段树单点更新+区间合并)
- 【HDU - 1540】Tunnel Warfare 【线段树+单点更新+区间合并】
- hdu 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare(用set解决线段树中单点查询)
- hdu 1540 Tunnel Warfare 线段树
- hdu-1540-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(线段树)
- ACM 数据结构 线段树 HDU 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare
- HDU 1540 Tunnel Warfare 【线段树】
- HDU - 1540 Tunnel Warfare —— 区间合并
- HDU1540 Tunnel Warfare (线段树区间合并)
- POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)
- HDU 1540 Tunnel Warfare (线段树)