Language: Default
Tunnel Warfare
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:
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 Hint An illustration of the sample input: OOOOOOO D 3 OOXOOOO D 6 OOXOOXO D 5 OOXOXXO R OOXOOXO R OOXOOOO Source
POJ Monthly--2006.07.30, updog
|
比较基础的一道线段树。
题意是给你若干个顺序相连的村庄,你有3种命令:D x :把x村庄干掉,R:重建上一个被干掉的村庄,Q x:输出与x村庄直接或间接相连的村庄数。
显然我们用线段树去维护3个域即可,suml该节点向左的最大长度,sumr该节点向右的最大长度,sum该段的长度。
村庄有3种状态,用cover记录: 1:全部村庄存在; 0:全部村庄不存在; -1:有的村庄存在,有的不存在。
区间合并时有两种情况:
1:mid - l <= 左儿子的sumr 2:l - mid + 1 <= 右儿子的suml
两种情况皆为左儿子的sumr + 右儿子的suml 。
#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> #include <utility> using namespace std; //#define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define lson l , mid , rt << 1 #define rson mid, r , rt << 1 | 1 //#define mid ((l + r) >> 1) #define mk make_pair #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) const int MAXN = 50000 + 500; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 0x7fffffff; const int IMIN = 0x80000000; #define eps 1e-8 #define mod 1000000007 typedef long long LL; const double PI = acos(-1.0); typedef double D; typedef pair<int , int> pi; //#pragma comment(linker, "/STACK:102400000,102400000") struct t { int l , r , suml , sumr , cover , sum; }t[MAXN << 2]; stack <int>s; void init() { clr(t , 0); } void build(int l , int r , int rt) { t[rt].l = l ,t[rt].r = r; t[rt].suml = t[rt].sumr = t[rt].sum = r - l; t[rt].cover = 1; if(l == r - 1)return ; int mid = (l + r) >> 1; build(lson) ,build(rson); } void PushUp(int rt) { if(t[rt << 1].cover == 0 && t[rt << 1 | 1].cover == 0)t[rt].cover = 0; else if(t[rt << 1].cover == 1 && t[rt << 1 | 1].cover == 1)t[rt].cover = 1; else { t[rt].cover = -1; } t[rt].suml = t[rt << 1].suml + (t[rt << 1].cover == 1 ? t[rt << 1 | 1].suml : 0); t[rt].sumr = t[rt << 1 | 1].sumr + (t[rt << 1 | 1].cover == 1 ? t[rt << 1].sumr : 0); t[rt].sum = max(t[rt].suml , max(t[rt].sumr , t[rt << 1].sumr + t[rt << 1 | 1].suml)); } void update(int L , int R , int add , int rt) { if(t[rt].l >= L && t[rt].r <= R) { t[rt].cover = t[rt].sum = t[rt].suml = t[rt].sumr = add; return ; } if(t[rt].l == t[rt].r - 1)return ; int mid = (t[rt].l + t[rt].r) >> 1; if(L <= mid)update(L , R , add , rt << 1); if(mid < R)update(L , R , add , rt << 1 | 1); PushUp(rt); } int query(int rt , int l) { if(t[rt].cover == 1)return t[rt].sum; if(t[rt].cover == 0)return 0; int mid = (t[rt].l + t[rt].r) >> 1; if(l < mid) { if(mid - l <= t[rt << 1].sumr) { return t[rt << 1].sumr + t[rt << 1 | 1].suml; } return query(rt << 1 , l); } else { if(l - mid + 1 <= t[rt << 1 | 1].suml) { return t[rt << 1 | 1].suml + t[rt << 1].sumr; } return query(rt << 1 | 1 , l); } } char str[5]; int n , m; int main() { //ios::sync_with_stdio(false); #ifdef Online_Judge freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif // Online_Judge while(~scanf("%d%d" , &n , &m)) { init(); // outstars build(0 , n , 1); // outstars while(!s.empty())s.pop(); while(m--) { scanf("%s" ,str); int x; if(str[0] == 'D') { scanf("%d" , &x); update(x - 1 , x , 0 , 1); s.push(x); } else if(str[0] == 'R') { if(s.empty())break; x = s.top(); s.pop(); update(x - 1 , x , 1, 1); } else { scanf("%d" , &x); int ans = query(1 , x - 1); printf("%d\n" , ans); } } } return 0; }