P2342 Stacked Blocks

Source: Internet
Author: User

P2342 Stacked Blocks
      • by
      • Submit
    • Topic provider wwqk4444
    • Tagged tree array segment tree usaco
    • Difficulty popularization +/raise

Submit a discussion of the problem record

Recent discussions
    • No discussion at the moment
Topic Background cube Stacking, 2004 Open Topic Description

John and Bessie are stacking bricks. There are 30000 blocks, numbered 1 to 30000. At first, these bricks were placed in

Ground, naturally divided into n heaps. Bessie accepted John's instructions and stacked some of the bricks on top of the other bricks. Once the two

Blocks of bricks, each other will never separate, so the last stacked blocks will be more and more high. John, let Becky.

The operation is divided into two types:

? The first is the move operation, in the form "move X to Y above". X and Y represent the number of two blocks, meaning

It is the pile of bricks of x that is stacked on top of the pile of bricks where y is located;

? The second is a statistical operation in the form of "count the number of bricks below Z". Z represents the number of a block, meaning

Think that Bessie needs to report how many blocks there are under the block numbered Z.

Please write a program to help Bessie answer every statistic question.

Input output Format input format:

? First line: Single integer: p,1≤p≤10^5

? The second line to P + 1 lines: Each line describes a command, if the letter at the beginning of this line is M, represents a moving life

, the following two integers represent the x and Y in the above, and if the first letter is C, represents a statistic command. Behind

Integer represents the z in the above, ensuring that all move commands are meaningful and that X and Y do not already appear in the same heap

Muli

Output format:

? For each statistic command, the output is answered correctly, and the result of each query is separated by a newline character

Input and Output Sample input example # #:
6M 1 6C 1M 2 4M 2 6C 3C 4
Sample # # of output:
102
Description

The first query, 1 below only one 6;

There are no bricks under 3 at the time of query, and when the third query

4 There are two blocks below: 1 and 6

AC Code + Puzzle:

/*The correct explanation: With the underlying elements as a set of representative elements, to ensure that the cnt[] update to the end of the time will not be more, because cnt[fa[x]] must be 0 fa[x]=y; the father of X is y;  (initialization is itself) top[x]=t; indicates that the representative number above x is t;  (initialization is itself) cnt[x]=t; indicates that the number of blocks below X is t;  (initialization is 0) when the Find () update, two heap merges, using backtracking, update top[x]=top[t];cnt[x]=cnt[t]+cnt[x];(Drawing) PS: Every time you merge or query, find (), or WA. */#include<cstdio>#include<iostream>using namespacestd;#defineN 30010intFa[n],cnt[n],top[n];inlineintRead () {registerintx=0, f=1; RegisterCharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlineChar inch(){     for(RegisterCharCh=getchar (); Ch=getchar ())if(ch>='A'&&ch<='Z')returnch;}intFindintx) {    if(fa[x]==x)returnx; intt=Fa[x]; FA[X]=find (fa[x]); FA[X]=Fa[t]; TOP[X]=Top[t]; CNT[X]=cnt[t]+Cnt[x]; returnfa[x];}intMain () {intN,x,y,a,b;Charch; N=read ();  for(intI=1; i<=30000; i++) fa[i]=top[i]=i;  for(intI=1; i<=n;i++){        if((ch=inch())=='M') {a=read (); b=read (); X=find (a), y=find (b); FA[X]=Y;find (Top[y]); CNT[X]=cnt[top[y]]+1; Top[y]=Top[x]; }        Else{x=read (); find (x); printf ("%d\n", cnt[x]); }    }    return 0;}

P2342 Stacked Blocks

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.