hdu1540 Tunnel Warfare Segment tree/tree-shaped array

Source: Internet
Author: User

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!

Test instructions is a line with a lot of villages lined up, now there are three operations, one is to blow up a village, it is not connected with both sides, one is to repair a village, and one is to query a village, it to the left and right side of the total number of villages can be connected to each other.

Although it is also a line tree, many of the problems on the Internet are written with the line segment tree node record left continuous length and right continuous length, I just learned the line segment tree when the idea of not familiar with line tree, also follow the understanding of writing, but I have always felt that this practice is troublesome, easy to write wrong, probably on the internet is the mutual copy

My idea is to query how many villages can be connected to a village, in fact, it is to query it on both sides of the nearest destroyed village is which, for example, 5th village, the nearest destroyed village is 3rd and 8th, then it is connected to the village of 4~7, is (8-3-1) a village. So I just let the ruined village show up in the line tree, and I can solve the problem as long as I can query it. For example, the two villages closest to village 5th should look like: The 1~4 number is destroyed and the number is the largest, and the 6~n is destroyed and the number is the smallest. Then I actually only ask for the maximum and minimum values within the two intervals, which can be used to write the most basic single-point modification, the interval to find the most value of the line segment tree.

The specific methods are:

First all of the initial values in the maximum tree are-1, each point is destroyed, it is updated to its number in its position, such as the destruction of 3rd points, the position of 3 is updated to 3, so that the destroyed point in the line segment tree value is its number, can be used to directly query the maximum value, but not destroyed is-1, will not affect. The repair village is updated back to-1.

The minimum tree is the opposite, the initial value is a maximum value (I use 0x3f3f3f3f), the destruction point is also updated to the number, and the restoration also updates the maximum value.

Then the query directly to the left interval of the maximum and the right interval of the minimum value on it.

Details are:

Since no village has been destroyed on one side, all are-1 or maximum, which requires a special sentence. In order to solve this problem skillfully, I was at the beginning of the initialization, by the way to destroy the No. 0 point and N+1 Point, these two nonexistent points, so that the query can not be a special sentence to do so directly.

Of course, a tree-like array can do the same things.

1#include <stdio.h>2#include <string.h>3 Const intmaxm=5e4+5;4 Const intinf=0x3f3f3f3f;5 6 intma[maxm<<2],mi[maxm<<2];7 intsta[maxm],cnt;8 intMaxx,minn;9 Chars[Ten];Ten  One intMaxintAintb) {returnA>b?a:b;} A intMinintAintb) {returnA<b?a:b;} -  - voidUpdateintOintLintRintIndintc) { the     if(l==R) { -         if(c==1){ -ma[o]=-1; -mi[o]=INF; +         } -         Elsema[o]=mi[o]=l; +         return; A     } at     intm=l+ (r-l) >>1); -     if(ind<=m) Update (o<<1, l,m,ind,c); -     ElseUpdate (o<<1|1, m+1, r,ind,c); -Ma[o]=max (ma[o<<1],ma[o<<1|1]); -Mi[o]=min (mi[o<<1],mi[o<<1|1]); - } in  - voidQuery1 (intOintLintRintQlintqr) { to     if(ql<=l&&qr>=R) { +maxx=Max (Maxx,ma[o]); -         return; the     } *     intm=l+ (r-l) >>1); $     if(ql<=m) Query1 (o<<1, L,M,QL,QR);Panax Notoginseng     if(qr>=m+1) Query1 (o<<1|1, m+1, R,QL,QR); - } the  + voidQuery2 (intOintLintRintQlintqr) { A     if(ql<=l&&qr>=R) { theminn=min (Minn,mi[o]); +         return; -     } $     intm=l+ (r-l) >>1); $     if(ql<=m) Query2 (o<<1, L,M,QL,QR); -     if(qr>=m+1) Query2 (o<<1|1, m+1, R,QL,QR); - } the  - intMain () {Wuyi     intn,m; the      while(SCANF ("%d%d", &n,&m)! =EOF) { -Cnt=0; Wumemset (ma,-1,sizeof(MA)); -memset (MI,0x3f,sizeof(MI)); AboutUpdate1,0, n+1,0,0); $Update1,0, n+1, n+1,0); -          for(intI=1; i<=m;++i) { -scanf"%s", s); -             if(s[0]=='D'){ A                 intIND; +scanf"%d",&IND); theUpdate1,0, n+1, Ind,0); -sta[++cnt]=IND; $             } the             Else if(s[0]=='Q'){ the                 intIND; thescanf"%d",&IND); themaxx=-1; -minn=INF; inQuery1 (1,0, n+1,0, Ind); theQuery2 (1,0, n+1, ind,n+1); the                 if(Minn==maxx) printf ("0\n"); About                 Elseprintf"%d\n", minn-maxx-1); the             } the             Else if(s[0]=='R'){ theUpdate1,0, n+1, sta[cnt--],1); +             } -         } the     }Bayi     return 0; the}
View Code

hdu1540 Tunnel Warfare Segment tree/tree-shaped array

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.