hdu--1540 Tunnel Warfare (segment tree + interval merge)

Source: Internet
Author: User

Description

During the War of Resistance against Japan, tunnel warfare was carried off extensively in the vast areas of the North China Pl Ain. 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 E Vents. 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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4

Sample Output

1024
Test instructions: There are n villages on a tunnel, for M operations, the D operation is to destroy the village X,q operation is to ask village X to be continuous with how many villages, R operation is to reconstruct the last destroyed village. Print each query result
Train of thought: first achievement, A[k].lazy for 1 description [A[K].L,A[K].R] within the village all intact, A[k].lazy 0 description [A[K].L,A[K].R] within the area of the village is destroyed. The maximum number of consecutive villages from the left endpoint to the right number, the maximum contiguous number of villages from the right endpoint to the left, and the largest contiguous number of villages in the interval are represented by LM,RM and M respectively. A single-point update is then performed, with pushup updating the parent node. Finally, to ask, using the idea of two points, respectively, Saozi right subtree statistics.
AC Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5 using namespacestd;6 Const intmaxn=50000+Ten;7 intNUM[MAXN];8 structNote9 {Ten     intL,r,lm,rm,m,lazy; One}a[maxn<<2]; A intMaxintAintb) - { -     if(a>b) the         returnA; -     Else -         returnb; - } + intBuildintLintRintk) - { +A[k].l=l,a[k].r=r,a[k].lm=a[k].rm=a[k].m= (r-l+1); Aa[k].lazy=1; at     if(l==R) -         return 0; -         intMid= (L+R)/2; -Build (l,mid,k*2); -Build (mid+1, r,k*2+1); -     return 0; in } - intPushup (intk) to { +     if(a[k*2].rm< (a[k*2].r-a[k*2].l+1)) -a[k].lm=a[k*2].lm; the     Else *a[k].lm=a[k*2].rm+a[k*2+1].lm; $     if(a[k*2+1].lm<a[k*2+1].r-a[k*2+1].l+1)Panax Notoginsenga[k].rm=a[k*2+1].rm; -     Else thea[k].rm=a[k*2+1].lm+a[k*2].rm; +A[k].m=max (Max (a[k].lm,a[k].rm), a[k*2+1].lm+a[k*2].rm); A     if(a[k].m==a[k].r-a[k].l+1) thea[k].lazy=1; +     Else -a[k].lazy=0; $     return 0; $ } - intInsintDintNintk) - { the     if(d==a[k].l&&a[k].r==a[k].l) -     {Wuyia[k].rm=a[k].lm=a[k].m=N; thea[k].lazy=N; -         return 0; Wu     } -     //pushdown (k) About     if(d<=a[k*2].R) $INS (d,n,k*2); -     if(d>a[k*2].R) -INS (d,n,k*2+1); - Pushup (k); A     return 0; + } the intSeaintNintk) - { $     if(a[k].lazy==1|| a[k].r==a[k].l) the     { the         returna[k].m; the     } the     if(n<=a[k*2].R) -     { in         if(n>=a[k*2].r-a[k*2].rm+1) the             returnSea (n,k*2) +sea (a[k*2+1].l,k*2+1); the         Else About             returnSea (n,k*2); the     } the     Else the     { +         if(n<=a[k*2+1].lm+a[k*2+1].l-1) -             returnSea (n,k*2+1) +sea (a[k*2].r,k*2); the         ElseBayi             returnSea (n,k*2+1); the     } the     return 0; - } - intMain () the { the     intn,m,k,b; the      while(~SCANF ("%d%d",&n,&m)) the     { -Build1N1); thek=0; the          while(m--) the         {94             Chars[5]; thescanf"%s", s); the             if(s[0]=='D') the             {98scanf"%d",&num[k]); AboutIns (Num[k],0,1); -k++;101             }102             Else if(s[0]=='Q')103             {104scanf"%d",&b); theprintf"%d\n", Sea (b,1));106             }107             Else108             {109k--; theIns (Num[k],1,1);111             } the         }113     } the  the     return 0; the}
View Code

hdu--1540 Tunnel Warfare (segment tree + interval merge)

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.