Hdu1540:tunnel Warfare (segment tree interval merging)

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

Input the first line of the input contains the 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.

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


Test instructions: D for destroying the village, R for repairing the last destroyed village, Q for asking what is the maximum continuous interval, including x?

Idea: The comments in the code have been more detailed, so not much to explain what, in the line segment tree interval, we have three variables to record the left continuous interval, the right continuous interval and the maximum continuous interval

#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <

Stdlib.h> using namespace std;

const int MAXN = 50000+10;
int n,m;
    The int s[maxn],top;//s is the analog stack struct node {int l,r;

int ls,rs,ms;//ls, maximum continuous interval at left, RS maximum continuous interval at right end, maximum continuous interval in MS interval} a[maxn<<2];
    void init (int l,int r,int i) {a[i].l = l;
    A[I].R = R;
    A[i].ls = a[i].rs = a[i].ms = r-l+1;
        if (l!=r) {int mid = (l+r) >>1;
        Init (l,mid,i*2);
    Init (mid+1,r,2*i+1); }} void Insert (int i,int t,int x) {if (a[i].l = = A[I].R) {if (x==1) a[i].ls = a[i].rs = A[i]
    . Ms = 1;//Repair Else A[i].ls = a[i].rs = a[i].ms = 0;//break return;
    } int mid = (A[I].L+A[I].R) >>1;
    if (t<=mid) insert (2*I,T,X);
    else insert (2*I+1,T,X); A[i].ls = a[2*i].ls;//Left interval a[i].rs = a[2*i+1].rs;//right Interval a[i].ms = MAX (max (a[2*i].ms,a[2*i+1].ms), a[2*i].rs+a[2*i+1]. LS);//The maximum interval within the father's interval must be the maximal interval of the left subtree, the maximal interval of the right subtree, the middle interval of the merging of the subtree, and the largest interval value if (a[2*i].ls = = a[2*i].r-a[2*i].l+1)//Left dial hand tree interval is full,
    Father left interval to add right child's left interval a[i].ls + = A[2*i+1].ls;
if (a[2*i+1].rs = = a[2*i+1].r-a[2*i+1].l+1)//a[i].rs + = a[2*i].rs; } int query (int i,int t) {if (a[i].l = = A[I].R | | a[i].ms = = 0 | | a[i].ms = = a[i].r-a[i].l+1)//to the leaf node or the access interval is empty or full
    To go down the return a[i].ms;
    int mid = (A[I].L+A[I].R) >>1; if (T<=mid) {if (t>=a[2*i].r-a[2*i].rs+1)//Because T<=mid, look at the left subtree, a[2*i].r-a[2*i].rs+1 represents the left boundary value of the continuous interval to the right of the left dial hand tree, if T is in the left sub-tree
        In the right interval, it depends on how long the left interval of the right subtree is and returns the return query (2*i,t) +query (2*i+1,mid+1); else return query (2*i,t), or//if it is not within the right boundary of the left subtree, you only need to look at the left subtree} else {if (t<=a[2*i+1].l+a[2*i+1].l
        S-1)//Return query (2*i+1,t) +query (2*i,mid);
    else return query (2*I+1,T);
    }} int main () {int i,j,x;
    Char ch[2];
        while (~SCANF ("%d%d", &n,&m)) {top = 0;
 Init (1,n,1);       while (m--) {scanf ("%s", ch);
                if (ch[0] = = ' D ') {scanf ("%d", &x);
                s[top++] = x;
            Insert (1,x,0);
                } else if (ch[0] = = ' Q ') {scanf ("%d", &x);
            printf ("%d\n", Query (1,x));
                    } else {if (x>0) {x = S[--top];
                Insert (1,x,1);
}}}} 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.