HDU 1540 Tunnel Warfare segment tree: Single point update, interval merge

Source: Internet
Author: User

Tunnel Warfare

Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)


Problem descriptionduring the War of Resistance against Japan, tunnel warfare was carried off extensively in the vast area S 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 Input7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4

Sample Output1024

SOURCEPOJ Monthly: The problem of interval merging
//1085422276#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<cmath>#include<map>#include<bitset>#include<Set>#include<vector>#include<stack>///#include<bits/stdc++.h>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#defineMeminf (a) memset (A,127,sizeof (a));#defineMemfy (a) memset (A,-1,sizeof (a))#defineTS printf ("111111\n");#definefor (I,A,B) for (int i=a;i<=b;i++)#defineForj (I,A,B) for (int i=a;i>=b;i--)#defineREAD (A, b) scanf ("%d%d", &a,&b)#defineMoD 530600414#defineEPS 0.0000000001#defineINF 1000000000.0inline ll read () {ll x=0, f=1; CharCh=GetChar ();  while(ch<'0'|| Ch>'9')    {        if(ch=='-') f=-1; CH=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); }    returnx*F;}//****************************************#defineMAXN 500000+5structss{intL,r,l,r,v;} TR[MAXN];intN,m,lflag,rflag;voidPushup (intk) {    if(tr[k<<1]. l+tr[k<<1|1]. r== (tr[k].r-tr[k].l+1) {tr[k]. L=TR[K]. R= (tr[k].r-tr[k].l+1); }    Else {        if(tr[k<<1]. L>= (tr[k<<1].r-tr[k<<1].l+1) ) Tr[k]. L=tr[k<<1]. l+tr[k<<1|1].        L ElseTR[K]. l=tr[k<<1].        L if(tr[k<<1|1]. r>=tr[k<<1|1].r-tr[k<<1|1].l+1) Tr[k]. R=tr[k<<1|1]. r+tr[k<<1].        R ElseTR[K]. r=tr[k<<1|1].    R }}voidBuildintKintSintt) {TR[K].L=s; TR[K].R=T; TR[K]. L=TR[K]. r=t-s+1; if(s==t) {return ; }    intMid= (s+t) >>1; Build (k<<1, S,mid); Build (k<<1|1, mid+1, t);}voidUpdateintKintXintc) {    if(tr[k].l==x&&tr[k].r==x) {tr[k]. L=TR[K]. R=c;return ; }    intMid= (TR[K].L+TR[K].R) >>1; if(x<=mid) Update (k<<1, X,c); ElseUpdate (k<<1|1, X,c); Pushup (k);}intAskintKintx) {    if(tr[k].l==tr[k].r&&tr[k].l==x) {if(Tr[k]. L) rflag=lflag=1; returnTr[k].    L }    intMid= (TR[K].L+TR[K].R) >>1; intA=0, b=0; if(x<=mid) {A=ask (k<<1, x); }    Else{B=ask (k<<1|1, x); }    intans=a+C; if(A &&Rflag) {ans+=tr[k<<1|1].         L if(tr[k<<1|1]. l<tr[k<<1|1].r-tr[k<<1|1].l+1) Rflag=0; }     if(B &&Lflag) {ans+=tr[k<<1].         R if(tr[k<<1]. r<tr[k<<1].r-tr[k<<1].l+1) Lflag=0; }     returnans;}intMain () {Charch[321]; intx;  while(SCANF ("%d%d", &n,&m)! =EOF) {Stack<int>P; intlast=0; Build (1,1, N); For (I,1, M) {scanf ("%s", CH); if(ch[0]=='D') {cin>>x;                Q.push (x); Update (1X0); }            Else if(ch[0]=='Q') {cin>>x; Rflag=lflag=0; cout<<ask (1, x) <<Endl; }            Else{ Last=Q.top ();                Q.pop (); Update (1, Last,1); }        }    }    return 0;}
Code

HDU 1540 Tunnel Warfare segment tree: Single point update, 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.