Cube stacking
Time limit:2000 ms |
|
Memory limit:30000 K |
Total submissions:18820 |
|
Accepted:6530 |
Case time limit:1000 ms |
Description
Farmer John and Betsy are playing a game with N (1 <=n <= 30,000) identical cubes labeled 1 through N. they start with N stacks, each containing a single cube. farmer John asks Betsy to perform P (1 <= P <= 100,000) operation. there are two types of operations:
Moves and counts.
* In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube y.
* In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value.
Write a program that can verify the results of the game.
Input
* Line 1: A single integer, P
* Lines 2 .. P + 1: Each of these lines describes a legal operation. line 2 describes the first operation, etc. each line begins with a 'M' for a move operation or a 'C' for a count operation. for move operations, the line also contains two integers: X and Y. for Count operations, the line also contains a single INTEGER: X.
Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself.
Output
Print the output from each of the count operations in the same order as the input file.
Sample Input
6M 1 6C 1M 2 4M 2 6C 3C 4
Sample output
102
# Include <iostream> # include <cstring> using namespace STD; const int maxn = 100005; int set [maxn]; // set [k] To the bottom-layer element of the stack where K is located, int CNT [maxn]; // CNT [k] to k... set [k] Number of elements int top [maxn]; // top [k] is the top-level element int main () {int set_find (int x) of K's stack ); void set_join (int x, int y); int P; // number of operations CIN> P; ////// initialization ////////// memset (set,-1, sizeof (SET); memset (CNT, 0, sizeof (CNT); int I; for (I = 0; I <maxn; I ++) Top [I] = I; // initialize top [], the top element of all stacks is the element itself while (p --) // controls the number of inputs {char s; // operation control letter CIN> S; if (S = 'M') {int X, Y; CIN> x> Y; set_join (x, y ); // move the element containing element X to the top of the stack containing element y} If (S = 'C') {int X; // calculate the number of elements with X as the top element of the stack. Cin> X; set_find (X ); // calculate the number of elements in the stack containing X. cout <CNT [x] <Endl ;}} return 0 ;} //////// calculate the number of elements under X in the stack containing X /// // int set_find (int x) {If (set [x] <0) // no other elements return X under X; If (set [set [x]> = 0) // if there are still elements under set [X], adjust the element at the bottom of the stack where X is located {int fa = set [x]; // set [x] = set_find (set [x]), the bottom element of the stack with FA as P; // if there are other elements under set [X, then, the base element CNT [x] = CNT [fa] + CNT [x] of X is adjusted. // accumulate the number of elements from FA to set [x]} return set [X];} //// // move the stack where X is located to the top of the stack where Y is located // void set_join (int x, int y) {x = set_find (x); // X indicates the bottom element of the stack where X is located. Y = set_find (y ); // y is the bottom element of the stack where Y is located. Set [x] = y; // move stack X to the top of stack y, update the bottom-stack element set_find (top [y]) of X; // refresh the number of elements between the top element and Y of the stack where Y is located. CNT [x] = CNT [top [y] + 1; top [y] = top [X]; // update the top element of Y stack to the top element of x}