Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=5444
Elven Postmandescription
Elves is very peculiar creatures. As we all know, they can live for a very long time and their magical prowess is not something to be taken lightly. Also, they live on trees. However, there is something on them you are not know. Although delivering stuffs through magical teleportation is extremely convenient (much like emails). They still sometimes prefer other more "traditional" methods.
So, as a elven postman, it's crucial to understand how to deliver the mail to the correct the tree. The Elven tree always branches to no more than and paths upon intersection, either in the east direction or the west. It coincidentally looks awfully like a binary tree we human computer scientist know. Not only then, when numbering the rooms, they always number the "the" number from the East-most position to the west. For rooms in the east is usually more preferable and more expensive due to they have the privilege to see the sunrise, Which matters a lot in elven culture.
Anyways, the elves usually wrote down all the rooms with a sequence at the root of the "the Tree So" the postman may know how To deliver the mail. The sequence is written as follows, it'll go straight to visit the East-most and write down every the IT encounter Ed along the The. After the first reached, it'll then go to the next unvisited east-most, writing down every unvisited n the "as well" until all rooms is visited.
Your task is to determine how to reach a certain-given the sequence written on the root.
For instance, the sequence 2, 1, 4, 3 would is written on the root of the following tree.
Input
First you is given an integer $T \ (T \leq) $ indicating the number of test cases.
For each test case, there is a number $n \ (n \leq +) $ on a line representing the number of rooms in this tree. n integers representing the sequence written at the root follow, respectively $a _1, ..., a_n$ where $a _1, ..., a_n∈ \{1, ..., n\}$.
On the next line, there are a number $q $ representing the number of mails to be sent. After that, there'll be $q $ integers $x _1, ..., x_q$ indicating the destination the number of each mail.
Output
For each query, output a sequence of move ($E $ or $W $) The postman needs to make to deliver the mail. For this $E $ means that the postman should move up the eastern branch and $W $ the western one. If the destination is on the root, just output a blank line would suffice.
Note that for simplicity, we assume the postman always starts from the root regardless of the "the" he had just visited.
Sample Input
2
4
2 1 4 3
3
1 2 3
6
6 5 4 3 2 1
1
1
Sample Output
E
WE
Eeeee
Directly with the binary tree simulation can be.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <set>using std::set;using std::sort;using std::p air;using std::swap; Using Std::multiset; #define PB (E) push_back (e) #define SZ (c) (int) (c). Size () #define MP (A, b) Make_pair (A, B) #define ALL (c (c). Begin (), (c). End () #define ITER (c) Decltype ((c). Begin ()) #define CLS (arr, Val) memset (arr, Val, sizeof (arr)) #define Cpresent (c, E) (Find (All (c), (e))! = (c). End ()) #define REP (i, n) for (int i = 0; i < (int) n; i++) #define TR (c, I) for (it ER (c) i = (c). Begin (); I! = (c). end (); ++i) const int N = 1010;const int INF = ~0u >> 1;typedef unsigned long long ull;struct Node {int v; Node *ch[2];inline void set (int dat, Node *p) {v = dat, ch[0] = ch[1] = p;}}; struct BST {Node *root, *tail, *null, Stack[n];inline void init () {tail = &stack[0];null = Tail++;null->set (INF, NU LL); root = null;} inline Node *newnode (int dat) {node *p = tail++;p->set (DAT, null); return p;} inline void Insert (Node *&x, int dat) {if (x = = null) {x = NewNode (DAT); return;} Insert (X->ch[dat > X->v], DAT);} inline void Insert (int dat) {insert (root, DAT);} inline void find (int dat) {Node *x = root;while (x->v! = dat) {if (Dat < x->v) {Putchar (' E '); x = X->ch[0];} el SE {putchar (' W '); x = X->ch[1];}} Putchar (' \ n ');}} Go;int Main () {#ifdef localfreopen ("In.txt", "R", stdin), Freopen ("OUT.txt", "w+", stdout), #endifint t, N, V, q;scanf ("%d" , &t), while (t--) {go.init (), scanf ("%d", &n), Rep (i, N) {scanf ("%d", &v); Go.insert (v);} scanf ("%d", &q), while (q--) {scanf ("%d", &v); Go.find (v);}} return 0;}
Hdu 5444 Elven Postman