HDU 1540 Tunnel Warfare (line segment Tree single point update + interval merge), hduwarfare
Problem DescriptionDuring the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. generally speaking, ages connected by tunnels lay in a line. cannot the two at the ends, every village was directly connected with two neighboring ones.
Frequently the invaders launched attack on some of the versions and destroyed the parts of tunnels in them. the Eighth Route Army commanders requested the latest connection state of the tunnels and ages. if some versions are severely isolated, restoration of connection must be done immediately!
InputThe first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of versions 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 versions that x-th village was directly or indirectly connected with including itself.
R: The village destroyed last was rebuilt.
OutputOutput the answer to each of the Army commanders 'request in order on a separate line.
Sample Input
7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample Output
1024
Merge line segment tree segments: the reconstruction process shows that the segments are stored. For each interval (l, r), lsum indicates the maximum continuous value from left. Rsum: indicates the maximum continuous value from the right, maum: The maximum continuous value of the interval.
# Include <cstdio> # include <cstring> # include <algorithm> # include <vector> # include <string> # include <iostream> # include <queue> # include <cmath> # include <map> # include <stack> # include <bitset> using namespace std; # define REPF (I, a, B) for (int I = a; I <= B; ++ I) # define REP (I, n) for (int I = 0; I <n; ++ I) # define CLEAR (a, x) memset (a, x, sizeof a) typedef long LL; typedef pair <int, int> p Il; const int maxn = 50000 + 10; int lsum [maxn <2], rsum [maxn <2], msum [maxn <2]; int s [maxn], n, m, top; void pushup (int rs, int s) {lsum [rs] = lsum [rs <1]; rsum [rs] = rsum [rs <1 | 1]; if (lsum [rs <1] = (s-(s> 1 ))) lsum [rs] + = lsum [rs <1 | 1]; if (rsum [rs <1 | 1] = (s> 1 )) rsum [rs] + = rsum [rs <1]; msum [rs] = max (msum [rs <1], msum [rs <1 | 1]), rsum [rs <1] + lsum [rs <1 | 1]);} void build (int l, int r, int rs) {lsum [rs] = rsum [rs] = msum [rs] = r -L + 1; if (l = r) return; int mid = (l + r)> 1; build (l, mid, rs <1 ); build (mid + 1, r, rs <1 | 1); pushup (rs, r-l + 1);} void update (int x, int c, int l, int r, int rs) {if (l = r) {lsum [rs] = rsum [rs] = msum [rs] = c; return ;} int mid = (l + r)> 1; if (x <= mid) update (x, c, l, mid, rs <1 ); if (x> mid) update (x, c, mid + 1, r, rs <1 | 1); pushup (rs, r-l + 1 );} int query (int x, int l, int r, int rs) {if (l = r | msum [rs] = 0 | msum [rs] = r-l + 1) return msum [Rs]; int mid = (l + r)> 1; if (x <= mid) // points x in the left interval {if (x> = mid-rsum [rs <1] + 1) // return rsum [rs <1] + query (mid + 1, mid + 1, r, rs <1 | 1) in the right continuous interval ); else return query (x, l, mid, rs <1);} else {if (x <= mid + lsum [rs <1 | 1]) return lsum [rs <1 | 1] + query (mid, l, mid, rs <1); else return query (x, mid + 1, r, rs <1 | 1) ;}} int main () {char str [3]; int x; while (~ Scanf ("% d", & n, & m) {build (1, n, 1); top = 0; while (m --) {scanf ("% s", str); if (str [0]! = 'R') scanf ("% d", & x); if (str [0] = 'D') {update (x, 0, 1, n, 1 ); s [top ++] = x;} if (str [0] = 'R') {x = s [-- top]; update (x, 1, 1, n, 1);} if (str [0] = 'q') printf ("% d \ n", query (x, 1, n, 1 ));}} return 0 ;}