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!
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.
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 k表示破坏村庄k,R表示恢复上次破坏的村庄,Q k表示查询包括k在内连续村庄的长度。
题解:分别维护左端点序列maxo[]和右端点序列mino[].
破坏/修复一个村子k的时候更新对应段的最大/最小值。破坏的时候把对应点的mino[]和maxo[]变成k。修复的时候把mino[]变成n+1,maxo[]变成0.
这样查询k时[1,k]中maxo[]的最大值代表k的左端点。[k,n]中的mino[]最小值代表k的右端点。
就相当于对坏掉的村庄做处理。查询村庄的时候分别找到左边和右边离它最近的。
比如查询k就找[1,k]中被破坏掉的最右边的点和[k,n]中被破坏掉的最左边的点。
#include<bits/stdc++.h> using namespace std; const int maxn = 50010; #define lson l , m , num << 1 #define rson m + 1 , r , num << 1|1 int n , m , k , maxo[maxn << 2] , mino[maxn << 2];char s[2]; void up(int num){maxo[num] = max(maxo[num << 1] , maxo[num << 1|1]); mino[num] = min(mino[num << 1] ,mino[num << 1|1]);} void built(int l , int r , int num){ if(l == r) {maxo[num] = 0 ; mino[num] = n + 1;return;} int m = (l + r)>>1; built(lson);built(rson); up(num); return; } void des(int k , int l , int r , int num){ if(l == r){maxo[num] = k; mino[num] = k;return;} int m = (l + r)>>1; if(k <= m) des(k,lson); if(k > m)des(k,rson);up(num); } void red(int k , int l , int r , int num){ if(l == r){maxo[num] = 0; mino[num] = n + 1;return;} int m = (l + r)>>1; if(k <= m) red(k,lson); if(k > m)red(k,rson);up(num); } int getl(int L , int R , int l , int r , int num){ if(L <= l && r <= R){return maxo[num];} int ans = 0;int m = (l + r) >> 1; if(L <= m) ans = max(ans , getl(L , R , lson)); if(R > m) ans = max(ans , getl(L , R , rson)); return ans; } int getr(int L , int R , int l , int r , int num){ if(L <= l && r <= R){return mino[num];} int ans = n + 1;int m = (l + r) >> 1; if(L <= m) ans = min(ans , getr(L , R , lson)); if(R > m) ans = min(ans , getr(L , R , rson)); return ans; } int main() { while(~scanf("%d%d",&n,&m)){ built(1 , n , 1); stack<int>q; while(m--){ scanf("%s",s); if(s[0] == 'D'){ scanf("%d",&k); des(k , 1 , n , 1); q.push(k); } if(s[0] == 'R'){ if(q.empty()){ continue; } k = q.top(); q.pop(); red(k , 1 , n , 1); } if(s[0] == 'Q'){ scanf("%d",&k); int ll = getl(1 , k , 1 , n , 1); int rr = getr(k , n , 1 , n , 1); int ans = rr - ll - 1; if(ans < 0) ans = 0; printf("%d ",ans); } } } }