POJ Topic 2892 Tunnel Warfare (segment tree single point update query, the maximum continuous interval length of the single point)

Source: Internet
Author: User

Tunnel Warfare
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 7307 Accepted: 2997

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 , indicating the number of villages and events. Each of the nextm lines describes an event.

There is three different events described in different format shown below:

    1. D x: The x-th village was destroyed.
    2. Q x: The Army commands requested the number of villages that x-th Village is directly or indirectly con Nected with including itself.
    3. 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

Hint

An illustration of the sample input:

      Oooooood 3   ooxooood 6   ooxooxod 5   ooxoxxor     ooxooxor     ooxoooo

Source

POJ monthly--2006.07.30, Updog

AC Code

#include <stdio.h> #include <string.h> #define MAX (A, B) (a>b?a:b) struct s{int rl,ll,ml;} Node[50500<<2];int stack[50500],top;void Build (int l,int r,int tr) {node[tr].ll=node[tr].rl=node[tr].ml=r-l+1; if (l==r) Return;int mid= (l+r) >>1;build (l,mid,tr<<1); build (mid+1,r,tr<<1|1);} void update (int pos,int l,int r,int tr,int val) {if (l==r) {if (val) node[tr].ll=node[tr].rl=node[tr].ml=1;elsenode[tr]. Ll=node[tr].rl=node[tr].ml=0;return;} int mid= (L+R) >>1;if (pos<=mid) {update (pos,l,mid,tr<<1,val);} Elseupdate (Pos,mid+1,r,tr<<1|1,val);node[tr].ll=node[tr<<1].ll;node[tr].rl=node[tr<<1|1].rl; Node[tr].ml=max (NODE[TR&LT;&LT;1].ML,NODE[TR&LT;&LT;1|1].ML); Node[tr].ml=max (node[tr].ml,node[tr<<1].rl+ NODE[TR&LT;&LT;1|1].LL); if (node[tr<<1].ll==mid-l+1) node[tr].ll+=node[tr<<1|1].ll;if (node[tr< <1|1].rl==r-(mid+1) +1) Node[tr].rl+=node[tr<<1].rl;} int query (int pos,int l,int r,int tr) {if (l==r| | node[tr].ml==0| | node[tr].ml==r-l+1) {return node[tr].ml;} int mid= (L+R) >>1;if (pos<=mid) {if (pos>=mid-node[tr<<1].rl+1) return query (pos,l,mid,tr<<1) +query (mid+1,mid+1,r,tr<<1|1); return query (pos,l,mid,tr<<1);} Else{if (POS&LT;=MID+1+NODE[TR&LT;&LT;1|1].LL-1) return query (pos,mid+1,r,tr<<1|1) +query (mid,l,mid,tr< <1); return query (POS,MID+1,R,TR&LT;&LT;1|1);}} int main () {int n,m;while (scanf ("%d%d", &n,&m)!=eof) {build (1,n,1); Top=0;while (m--) {char s[2];int x;scanf ("%s ", s), if (s[0]== ' D ') {scanf ("%d ", &x); Stack[top++]=x;update (x,1,n,1,0);} Else{if (s[0]== ' Q ') {int x;scanf ("%d", &x), int ans=query (x,1,n,1);p rintf ("%d\n", ans);} Else{int x=stack[top-1];top--;update (x,1,n,1,1);}}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ Topic 2892 Tunnel Warfare (segment tree single point update query, the maximum continuous interval length of the single point)

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.