ACM/ICPC Asia Regional Changchun Online Pro 1008Elven Postman

Source: Internet
Author: User

Elven Postman

Time limit:1500/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 0 Accepted Submission (s): 0


Problem descriptionelves 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.

Inputfirst you is given an integerT(t≤ten) indicating the number of test cases.

For each test case, there is a numbern(n≤) On a line representing the number of rooms in this tree.NIntegers representing the sequence written at the root follow, respectivelya1,.. . ,an wherea1,.. . ,an∈{1,.. . ,N} .

On the next line, there is a numberQRepresenting the number of mails to be sent. After that, there'll be q integers x1,.. . ,xq indicating the destination , number of each mail.

Outputfor each query, output a sequence of move (EOrWThe postman needs to do to deliver the mail. For thatE means 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 all the starts from the root regardless of the the the-had just visited.

Sample Input242 1 4 331 2 366 5 4 3 2111 The sample Outpute weeeeee sequence gives the DFS order, so just calculate the node number of the left and right subtree of each node. Specifies that the right tree stands for ' E ' direction. Because Roomid has a small priority access, all when the current node Roomid is A[u], you only need to know how many sub-nodes in the right tree of A[u] are smaller than the roomid that you have previously visited. This information can be maintained in a tree-like array. To know the number of nodes on the left tree is to maintain a message at DFS, the total number of nodes in the current tree tot. Take the inquiry offline, DFS when logging the path, arrive at a point of inquiry to record the answer.
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e3+ -;intN;intC[MAXN];#defineLowbit (x) (x&-x)intSumintx) {    intRET =0;  while(X >0) {ret+ = C[x]; X-=lowbit (x); }    returnret;}voidAddintXintd) {     while(x <=N) {C[x]+ = D; X + =lowbit (x); }}stringANS[MAXN];intQRY[MAXN];BOOLMARK[MAXN];intA[MAXN];CharPATH[MAXN];BOOLVIS[MAXN];voidDfsintU =1,intf =0,intD =1,inttot =N) {    if(Vis[u] | | tot <=0)return; Vis[u]=true; if(Mark[a[u]]) {ans[a[u]].assign (path+1, path+d); } Add (A[u],1); if(Tot = =1)return; intct = a[u]-sum (a[u]) +1; intRST = u+CT; if(rst>u+1&& U <N) {path[d]='E'; DFS (U+1, u,d+1, ct-1); }    if(Rst>u && rst<=N) {Path[d]='W'; DFS (Rst,u,d+1, tot-CT); }}intMain () {//freopen ("In.txt", "R", stdin);    intT scanf"%d",&u); a[0] =0; path[0] ='*';  while(t--) {scanf ("%d",&N);  for(inti =1; I <= N; i++) scanf ("%d", A +i); intQ; scanf ("%d",&q); memset (C,0,sizeof(C)); memset (Mark,0,sizeof(Mark));  for(inti =0; i < Q; i++) {scanf ("%d", qry+i); Mark[qry[i]]=true; } memset (Vis,0,sizeof(VIS));        DFS ();  for(inti =0; i < Q; i++) {printf ("%s\n", Ans[qry[i]].c_str ()); }    }    return 0;}

ACM/ICPC Asia Regional Changchun Online Pro 1008Elven Postman

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.