HDU 1540 tunnel warfare [Line Segment tree]

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1540

During the War of Resistance Against Japanese Aggression, each village was connected by a tunnel (the village was placed online) and there were three operations:

First, a village is destroyed by an enemy;

Solution 2: repair the previous destroyed village;

Third: query the number of villages directly or indirectly connected to the village (including the village itself );


This topic uses the line segment tree. Each node contains the consecutive range ls starting from the left end, the continuous range RS from the right end to the left, and the maximum continuous interval mas Of the range;

The Code is as follows:

# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # define n 50050 # define L (x) x <1 # define R (X) (x <1) | 1 using namespace STD; struct node {int L, R; int ls, RS, MAS; // ls indicates the continuous Interval Length starting from the left endpoint, RS indicates the right} A [n <2]; int sta [N]; void Init (INT l, int R, int numeric) {A [numeric]. L = L; A [Signature]. R = r; A [region]. ls = A [Signature]. rs = A [Signature]. mas = r-L + 1; if (L! = R) {int mid = (L + r)> 1); Init (L, mid, L (bytes); Init (Mid + 1, R, R (random) ;}} void insert (INT random, int X, int flag) {if (a [random]. L = A [Signature]. r) {If (FLAG) A [else]. ls = A [Signature]. rs = A [Signature]. mas = 1; // fix else a [failed]. ls = A [Signature]. rs = A [Signature]. mas = 0; // destroy return;} int mid = (a [Break]. L + A [Signature]. r)> 1; if (x> mid) insert (R (rows), X, flag); else insert (L (rows), X, flag ); A [Signature]. ls = A [L (bytes)]. ls; A [callback]. rs = A [R (random)]. RS; A [callback]. mas = max (A [L (inflow) ]. Mas, a [R (milliseconds)]. MAS), a [L (batch)]. RS + A [R (random)]. ls); if (a [L (random)]. ls = A [L (bytes)]. r-A [L (random)]. L + 1) A [Signature]. ls + = A [R (random)]. ls; if (a [R (random)]. rs = A [R (random)]. r-A [R (random)]. L + 1) A [Signature]. RS + = A [L (random)]. RS;} int query (INT distinct, int X) {if (a [distinct]. L = A [Signature]. r | A [snapshot]. mas = 0 | A [runtime]. mas = A [runtime]. r-A [Signature]. L + 1) return a [distinct]. MAS; int mid = (a [separator]. L + A [Signature]. r)> 1; if (x <= mid) {If (x> = A [L (distinct)]. r-A [L (random)]. RS + 1) // return query (L (I NX), x) + query (R (latency), Mid + 1); return a [L (latency)]. RS + A [R (random)]. ls; else return query (L (distinct), x);} else {If (x <= A [R (distinct)]. L + A [R (random)]. ls-1) // return query (R (rows), x) + query (L (rows), mid); return a [L (rows)]. RS + A [R (random)]. ls; else return query (R (latency), x) ;}} int main () {int n, m; while (~ Scanf ("% d", & N, & M) {int tail = 0; Init (1, n, 1); For (INT I = 0; I <m; I ++) {char temp [2]; scanf ("% s", temp); If (temp [0] = 'D ') {int X; scanf ("% d", & X); insert (1, x, 0); STA [tail ++] = x ;} else if (temp [0] = 'R') {If (tail-1> = 0) insert (1, Sta [-- tail], 1 );} else if (temp [0] = 'q') {int X; scanf ("% d", & X); printf ("% d \ n ", query (1, x) ;}} return 0 ;}


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.