Kuangbin Topic 7 hdu1540 tunnel warfare (prefix suffix line segment tree)

Source: Internet
Author: User
During 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


Indicates the first recognized line segment tree, And the prefix and suffix for the interval maintenance indicate that it is powerless, but it is stuck in the end. Note should be clear...
It is clear about the structure of the Line Segment tree and the splicing of the left and right children with the suffix of the interval prefix... Not clear ....



1 # include <iostream> 2 # include <stdio. h> 3 # include <math. h> 4 # include <string. h> 5 # include <stdlib. h> 6 # include <string> 7 # include <vector> 8 # include <set> 9 # include <map> 10 # include <queue> 11 # include <algorithm> 12 # include <sstream> 13 # include <stack> 14 using namespace STD; 15 # define fo freopen ("in.txt", "r", stdin); 16 # define rep (I, A, n) for (INT I = A; I <N; I ++) 17 # define per (I, A, n) f Or (INT I = n-1; I> = A; I --) 18 # define Pb push_back 19 # define MP make_pair 20 # define all (x ). begin (), (x ). end () 21 # define Fi first 22 # define se second 23 # define SZ (x) (INT) (x ). size () 24 # define debug (x) cout <"&" <x <"&" <Endl; 25 # define lowbit (X) (X &-x) 26 # define MEM (a, B) memset (a, B, sizeof (a); 27 typedef vector <int> VI; 28 typedef long ll; 29 typedef pair <int, int> PII; 30 Const ll mod = 1000000007; 31 const int INF = 0x3f3f3f3f; 32 ll powmod (ll a, LL B) {ll res = 1; A % = MOD; For (; B; B >>= 1) {If (B & 1) RES = res * A % MOD; A = A * A % MOD;} return res ;} 33 ll gcd (ll a, LL B) {return B? Gcd (B, A % B): A;} 34 // head 35 36 const int maxn = 50010; 37 int pre [maxn <2], SUF [maxn <2], Maxx [maxn <2], n, m, St [maxn]; // maintain interval prefix 1, interval suffix 1, st simulation stack 38 39 void pushup (int rt, int X) {40 pre [RT] = pre [RT <1]; // The prefix 1 of rt is the prefix 1 41 SUF [RT] = SUF [RT <1 | 1]; // RT suffix 1 is the suffix of the right child 1 42 Maxx [RT] = max (Maxx [RT <1], Maxx [RT <1 | 1]); // maximum RT conditions: 43 Maxx [RT] = max (Maxx [RT], pre [RT <1 | 1] + SUF [RT <1]); 44 If (SUF [RT <1 | 1] = (x> 1) SUF [RT] + = SUF [RT <1]; // If the suffix of the right child is 1, you can splice the suffix of the left child with 45 if (pre [RT <1] = (X-(x> 1 ))) pre [RT] + = pre [RT <1 | 1]; // If the left child prefix is all 1, you can splice the right child prefix 46} 47 48 void build (int rt, int L, int R) {49 pre [RT] = SUF [RT] = Maxx [RT] = R-L + 1; // here set to R-L + 1 and general pushup effect as 50 int mid = (L + r)> 1; 51 if (L! = R) {52 build (RT <1, L, mid); 53 build (RT <1 | 1, Mid + 1, R ); 54} 55} 56 57 void updata (int rt, int L, int R, int POs, int Val) {58 If (L = r) {// modify 59 Pre [RT] = SUF [RT] = Maxx [RT] = val; 60 return; 61} 62 int mid = (L + r)> 1; 63 If (Pos <= mid) updata (RT <1, L, mid, POs, Val); 64 else updata (RT <1 | 1, mid + 1, R, POs, Val); 65 pushup (RT, R-L + 1); 66} 67 68 int query (int rt, int L, int R, int POS) {69 If (L = r | Maxx [RT] = 0 | Maxx [R T] = (R-L + 1) // breakpoint | Maxx is 0 | Maxx Max 70 return Maxx [RT]; 71 int mid = (L + r)> 1; 72 If (Pos <= mid) {// If the left subtree 73 If (Pos> = mid-Suf [RT <1] + 1) return pre [RT <1 | 1] + SUF [RT <1]; // If the suffix is greater than that of the left child, 1 = contains 74 else return query (RT <1, L, mid, POS ); // otherwise, search for the left subtree 75} else {// if the right subtree 76 if (Pos <= Mid + 1 + pre [RT <1 | 1]-1) return pre [RT <1 | 1] + SUF [RT <1]; // If the prefix 1 = contains two parts: 77 else return query (RT <1 | 1, Mid + 1, R, POS ); // otherwise, the right subtree 78} 79} 80 81 int main () {82 while (~ Scanf ("% d", & N, & M) {83 int cur = 0; 84 build (1, 1, n); 85 int Pos; 86 while (M --) {87 char s [3]; 88 scanf ("% s", S); 89 If (s [0] = 'D ') {90 scanf ("% d", & Pos); 91 st [cur ++] = Pos; 92 updata (1, 1, n, POs, 0 ); 93} else if (s [0] = 'q') {94 scanf ("% d", & Pos); 95 printf ("% d \ n ", query (1, 1, n, POS); 96} else {97 Pos = sT [-- cur]; 98 updata (1, 1, n, POs, 1 ); 99} 100} 101} 102}

 



Kuangbin Topic 7 hdu1540 tunnel warfare (prefix suffix line segment tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.