P1503 Devil into the village

Source: Internet
Author: User

Topic Background

Xiao Card the TV in the living room of his new home. TV is playing put Shiba still replay "Liang Jian", the play in the Li Yunlong led independent regiment in a county met a devil squad, so independent Regiment and the devil launched guerrilla warfare.

Title Description

Description of the county seat there are n tunnels connected to the house, the first I only with the i-1 and the first i+1 connected. Here's a message from M.

1, the message is D x: The Devil destroyed the house X, the tunnel was blocked.

2, the message is R: Villagers will be the devil's last destroyed house repaired.

3, the message is Q x: A soldier was trapped in the X house.

Li Yunlong was nervous about receiving information, and he wanted to know how many houses each of the containment soldiers could reach.

input/output format Input Format:

The first row of 2 integers n,m (n,m<=50000).

The next M-line, like the title of the three kinds of information about a total of M-bar.

output Format:

For each containment soldier, output the number of houses the soldier can reach.

input/Output sample Input Sample # #:
7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
Sample # # of output:
1024
Description

If a soldier is trapped in a destroyed house, it can only wait for death ...

Solution:

The subject does not spin treap.

The implementation process is a little cumbersome, but the idea is simple:

At the beginning of the sequence is a treap, each deletion of a node is equivalent to the node to the root and separate the left and right sub-tree (note that a node is repeatedly deleted), with the stack record after each separate two new trees, then the query is the current node is the size of the tree, repair node is a stack of the process of merging subtrees.

Code:

/*Code by 520--10.3*/#include<bits/stdc++.h>#defineIl inline#definell Long Long#defineRE Register#definefor (i,a,b) for (RE int (i) = (a);(i) <= (b);(i) + +)#defineBor (i,a,b) for (RE int (i) = (b);(i) >= (a);(i)--)using namespacestd;Const intn=100005;intN,m,stk[n],top,lsx[n],lsy[n];BOOLVis[n];intcnt,root,siz[n],ch[n][2],fa[n],date[n],rnd[n];ilintNewNodeintV) {siz[++cnt]=1, fa[cnt]=0, Date[cnt]=v,rnd[cnt]=rand ();returnCNT;} IlvoidUpintRT) {Siz[rt]=siz[ch[rt][0]]+siz[ch[rt][1]]+1; if(ch[rt][0]) fa[ch[rt][0]]=RT; if(ch[rt][1]) fa[ch[rt][1]]=RT;}intMergeintXinty) {    if(!x| |! Yreturnx+y; if(Rnd[x]<rnd[y]) {ch[x][1]=merge (ch[x][1],y), up (x);returnx;} Else{ch[y][0]=merge (x,ch[y][0]), up (y);returny;}}voidSplitintRtintVint&x,int&y) {    if(!RT) {x=y=0;return;} if(date[rt]<=v) X=rt,split (ch[rt][1],v,ch[x][1],y), up (x); ElseY=rt,split (ch[rt][0],v,x,ch[y][0]), Up (y);} IlvoidInsintV) {intx=0, y=0; split (root,v,x,y); root=Merge (merge (X,newnode (v)), y);}intFindintx) {return!FA[X]?X:find (fa[x]);}intMain () {scanf ("%d%d",&n,&m); For (I,1, N) ins (i); Charopt[2]; intx, y; For (I,1, M) {scanf ("%s", opt); if(opt[0]=='D') {scanf ("%d",&x); stk[++top]=x;vis[x]=1;            Split (Find (x), x,lsx[top],lsy[top]); Split (Lsx[top],x-1, Lsx[top],y); Fa[lsx[top]]=0, fa[lsy[top]]=0; }        Else if(opt[0]=='R') Merge (merge (Lsx[top],stk[top]), Lsy[top]), vis[stk[top--]]=0; Else{scanf ("%d",&x); if(Vis[x]) {printf ("0\n");Continue;} Y=siz[find (x)]; printf ("%d\n", y); }    }    return 0; }

P1503 Devil into the village

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.