"Poj 2892" tunnel Warfare binary + Tree array

Source: Internet
Author: User

Tunnel Warfare
Time limit:1000ms Memory limit:131072k
Total submissions:7576 accepted:3127

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 was directly or indirectly connected with including itself.R: The village destroyed last 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

Hint

An illustration of the sample input:

  OOOOOOO

D 3 ooxoooo

D 6 Ooxooxo

D 5 Ooxoxxo

R Ooxooxo

R ooxoooo

Source
POJ monthly–2006.07.30, Updog

title link : http://poj.org/problem?id=2892

Test Instructions : There are n villages for m operations, there are three kinds of operations

D x: Means to destroy a village

Q x: Indicates the number of villages that query the contiguous existence of a village, if the village does not exist for 0

R: means to repair the last destroyed village

Ideas :
A tree array maintains something like a prefix, and the dichotomy determines whether there are villages in succession
O (mlogn^2)

To find the left as an example

Created with Rapha?l 2.1.0 start rx=x; lx=1; if (_get (RX)-_get (mid-1) ==rx-mid+1) confirmation? Ans=mid; rx=mid-1; RX>=LX? again end; lx=mid+1; Yes No Yes No

Decision Condition: if (_get (RX)-_get (mid-1) ==rx-mid+1) right is legal!
Or if (_get (x)-_get (mid-1) ==x-mid+1)

while(rx>=lx)    {        mid=(rx+lx)>>1;        if(_get(rx)-_get(mid-1)==rx-mid+1)        {            ans=mid;             rx=mid-1;        }         else lx=mid+1;    }    int t1=ans;

* Two-point posture: while (RX>=LX) if (C (mid)) Ans=mid;

Code :

#include <iostream>#include <stdio.h>#include <string.h>#include <stack>using namespace STD;intN,m;intc[50005];Chars[3];intAa Stack<int>PrevoidAddintXintY) { while(x<=n)        {c[x]+=y;    x+=x& (-X); }}int_get (intx) {intret=0; while(x>0) {ret+=c[x];    x-=x& (-X); }returnRET;}intSolveintx) {if(_get (x)-_get-X1)==0)return 0;intRx=x;intlx=1;intMidintAns while(RX&GT;=LX) {mid= (RX+LX) >>1;if(_get (Rx)-_get (mid-1) ==rx-mid+1) {Ans=mid; rx=mid-1; }ElseLx=mid+1; }intT1=ans;    Rx=n; Lx=x;//int cnt=0;     while(RX&GT;=LX) {mid= (RX+LX) >>1;if(_get (mid)-_get (lx-1) ==mid-lx+1) {lx=mid+1;        Ans=mid; }Elserx=mid-1; }returnans-t1+1;}intMain () {scanf("%d%d", &n,&m); for(intI=1; i<=n;i++) Add (i,1); for(intI=1; i<=m;i++) {scanf('%s ', s);if(s[0]!=' R ')scanf("%d", &AMP;AA);if(s[0]==' D ') {Add (aa,-1);        Pre.push (AA); }Else if(s[0]==' Q ')        {printf("%d\n", Solve (AA)); }Else if(s[0]==' R ')         {intTmp=pre.top ();            Pre.pop (); Add (TMP,1); }    }           }

"Poj 2892" tunnel Warfare binary + Tree 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.