Tunnel Warfare
Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Link: Hdu 1540 POJ 2892
Problem Description During The War of Resistance against Japan, tunnel warfare was carried off extensively in the vast is As of North China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the ends, every village was directly connected with the 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 is severely isolated, restoration of connection must is done immediately! Inputthe first line of the input contains, 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 is 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 is directly or indirectly connected with includ ING itself.
R:the village destroyed was rebuilt. Outputoutput the answer to all 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
Test instructions
Given N points, the number of points is from 1 to n,m times ' Q ' or ' d ' or ' R ' operations, "D X" represents the destruction of this point x, "R" is the point of damage before repair, "Q x" indicates the length of the continuous interval where the enquiry point x is located. It is assumed that each point is good at first.
Analysis:
First, the operation is analyzed: The D operation can be regarded as the interval in point x out truncation, R operation is to the X "including point X" to merge. Each time you merge two intervals [A, b], [c,d], the maximum continuous interval length for the new interval [a,d] is:
len (a,d) = max{Len (A, A, b), Len (c,d), End (+) + Begin (c)}; <== has a little thought of division.
End (b) represents the maximum continuous interval length at the end of B, and begin (c) represents the maximum continuous interval length starting with C, where we know that merging two intervals is not a simple interval addition, but rather maintains a maximum continuous interval length that starts with the first element of the interval and The maximum contiguous interval length at the end of the last element of the interval.
Well, the problem is better now, each node of the segment tree contains three information, the maximum continuous interval length in ln, beginning with the first element of the interval,and the maximum continuous interval length rn at the end of the last element of the interval . , and the maximum continuous length mnof the interval.
The next step is the Q operation, which asks the length of the continuous interval where x is located. At the recursive left son node, notice whether X is in the range of the left son Rn at the end of the last element of the left son interval, and if so, then The contiguous interval of x may contain part of the right son, Recursive right son's time, same!
/****************************>>>>headfiles<<<<****************************/#include <set> #include <map> #include <list> #include <cmath> #include <queue> #include <vector > #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include < iostream> #include <sstream> #include <algorithm>using namespace std;/**************************** >>>>>define<<<<<*****************************/#define FST First#define snd Second#define root 1,n,1#define lson l,mid,rt<<1#define Rson mid+1,r,rt<& Lt;1|1#define PB (a) push_back (a) #define MP (A, B) Make_pair (A, b) #define CASE (T) for (scanf ("%d", &am P T); t--;) #define FIN freopen ("Input.txt", "R", stdin) #define FOUT freopen ("Output.txt", "w", stdout)//#prag MA COMMENT (linker, "/stack:1024000000,1024000000") typedef __inT64 ll;const int INF = 0x3f3f3f3f;/****************************>>>>separator<<<<********** /const int maxn = 50000 + 5;int N, m;struct node{Int LN, RN, MN;} segtree[maxn << 4];inline vo ID pushup (const int& RT, const int& L, const int& r) {segtree[rt].ln = Segtree[rt << 1].ln; Segtree[rt].rn = segtree[rt << 1 | 1].rn; SEGTREE[RT].MN = max (segtree[rt << 1].rn + segtree[rt << 1 | 1].ln, max (Segtree[rt &L t;< 1].mn, segtree[rt << 1 | 1].mn)); int mid = (L + r) >> 1; if (segtree[rt << 1].mn = = mid-l + 1) segtree[rt].ln + = Segtree[rt << 1 | 1].ln; if (segtree[rt << 1 | 1].mn = = R-(mid + 1) + 1) segtree[rt].rn + = Segtree[rt << 1].rn;} void Build (int l, int r, int rt) {Segtree[rt].ln = Segtree[rt].rn = segtree[rt].mn = r-l + 1; if (L = = r) {return; } int mid = (L + r) >> 1; Build (Lson); Build (Rson);} void Update (int pos, int val, int l, int r, int rt) {if (L = = r) {segtree[rt].ln = Segtree[rt].rn = Segtree[r T].mn = val; Return } int mid = (L + r) >> 1; if (POS <= mid) Update (POS, Val, Lson); Else Update (POS, Val, Rson); Pushup (RT, L, R);} int Query (int pos, int l, int r, int rt) {if (r-l + 1 = = Segtree[rt].mn | | r = = L | | segtree[rt].mn = 0) { return segtree[rt].mn; } int mid = (L + r) >> 1, ret = 0; if (pos <= mid) {if (pos >= mid-segtree[rt << 1].rn + 1) ret = query (pos, Lson) + query ( Mid + 1, Mid + 1, R, RT << 1 | 1); else ret = Query (pos, Lson); } else {if (pos <= (mid + 1) + segtree[rt << 1 | 1].ln-1) ret = Query (pos, Rson) + Quer Y (Mid, L, Mid, RT << 1); else ret = Query (pos, Rson); } return ret;} int Destroy[maxn];int Main () {//fin; while (~SCANF ("%d%d", &n, &amP M) {Build (root); Char op[5]; int x, cnt = 0; for (int i = 0; i < M; i++) {scanf ("%s", Op); if (op[0] = = ' Q ') {scanf ("%d", &x); printf ("%d\n", Query (x, root)); } else if (op[0] = = ' D ') {scanf ("%d", &x); Update (x, 0, root); destroy[cnt++] = x; } else {x = destroy[--cnt]; Update (x, 1, root); } } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1540/poj 2892 Tunnel Warfare "segment tree interval Merging"