Poj2892 Tunnel Warface

Source: Internet
Author: User

Poj2892 Tunnel Warface

 

Tunnel Warfare
Time Limit:1000 MS   Memory Limit:131072 K
Total Submissions:7434   Accepted:3070

 

Description

During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. generally speaking, ages connected by tunnels lay in a line. cannot the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the versions and destroyed the parts of tunnels in them. the Eighth Route Army commanders requested the latest connection state of the tunnels and ages. if some versions are severely isolated, restoration of connection must be done immediately!

Input

The first line of the input contains two positive integersNAndM(N,M≤ 50,000) indicating the number of versions and events. Each of the nextMLines describes an event.

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

D x: X-Th village was destroyed. Q x: The Army commands requested the number of commands ages 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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4

Sample Output

1024

Hint

An authentication of the sample input:

      OOOOOOOD 3   OOXOOOOD 6   OOXOOXOD 5   OOXOXXOR     OOXOOXOR     OOXOOOO

Source

POJ Monthly -- 2006.07.30, updog

 

 

 

Application of the Balance Tree

The Balance Tree stores all the destroyed nodes.

For blow-up and repair operations, insert or delete them directly in the Balance Tree.

For the query operation, first determine whether the node is blown up, if the blow up answer is 0, otherwise in the Balance Tree to find the precursor x and subsequent y, the answer is y-x-1.

Another method is the binary + tree-like array, which means the write speed is slower...


 

 

 

#include
 
  #include
  
   #include
   
    #include
    
     #include
     
      #include#include
      
       #define F(i,j,n) for(int i=j;i<=n;i++)#define D(i,j,n) for(int i=j;i>=n;i--)#define LL long long#define MAXN 50005#define pa pair
       
        #define INF 1000000000using namespace std;int n,m,x,tot=0,rt=0,l[MAXN],r[MAXN],rnd[MAXN],v[MAXN];char op;bool f[MAXN];stack
        
          st;inline int read(){int ret=0,flag=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}return ret*flag;}inline void rturn(int &k){int tmp=l[k];l[k]=r[tmp];r[tmp]=k;k=tmp;}inline void lturn(int &k){int tmp=r[k];r[k]=l[tmp];l[tmp]=k;k=tmp;}inline void ins(int &k,int x){if (!k){k=++tot;v[k]=x;l[k]=r[k]=0;rnd[k]=rand();return;}if (x
         
          =v[k]) return suc(r[k],x);else {int tmp=suc(l[k],x);return tmp==n+1?v[k]:tmp;}}inline int getans(int x){if (f[x]) return 0;return suc(rt,x)-pre(rt,x)-1;}int main(){memset(f,false,sizeof(f));n=read();m=read();while (m--){op=getchar();while (op<'A'||op>'Z') op=getchar();if (op=='D') {x=read();f[x]=true;st.push(x);ins(rt,x);}else if (op=='Q') {x=read();printf("%d\n",getans(x));}else {del(rt,st.top());f[st.top()]=false;st.pop();}}}
         
        
       
      
     
    
   
  
 


 

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.