#define _CRT_SECURE_NO_WARNINGS #include "cstdio" class tree { struct node { node *left; node *right; int w; }; node *root; node* subtree(int n, int *tab) { node *res = new node(); res->w = -1; if (n == 0) { res->w = *tab; return res; } res->left = subtree(n - 1, tab); res->right = subtree(n - 1, tab + (1<< (n - 1))); return res; } void print(node *no) { if (no->w > 0) printf("%d ", no->w); else { print(no->left); print(no->right); } } void toss(node *no) { if(no->w<0) { toss(no->left); toss(no->right); node *tmp=no->left; no->left=no->right; no->right=tmp; } } void free(node *no) { if(no->w==-1) { free(no->left); free(no->right); } delete no; } public: ~tree() { } tree(int n, int *tab) { root = new node(); root->w=-1; root->left = subtree(n - 1, tab); root->right = subtree(n - 1, tab + (1 << (n - 1))); } void print() { print(root); } void toss() { toss(root); } }; int main(void) { int n, t; scanf("%d %d", &n,&t); int pow = 2; for (int i = 1; i < n; ++i) pow *= 2; int *tab = new int[pow]; for (int i = 0; i < pow; ++i) { int a; scanf("%d", &tab[i]); } tree *drzewo = new tree(n,tab); if(t%2)drzewo->toss(); drzewo->print(); delete drzewo; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #define _CRT_SECURE_NO_WARNINGS #include "cstdio" class tree { struct node { node *left; node *right; int w; }; node *root; node* subtree(int n, int *tab) { node *res = new node(); res->w = -1; if (n == 0) { res->w = *tab; return res; } res->left = subtree(n - 1, tab); res->right = subtree(n - 1, tab + (1<< (n - 1))); return res; } void print(node *no) { if (no->w > 0) printf("%d ", no->w); else { print(no->left); print(no->right); } } void toss(node *no) { if(no->w<0) { toss(no->left); toss(no->right); node *tmp=no->left; no->left=no->right; no->right=tmp; } } void free(node *no) { if(no->w==-1) { free(no->left); free(no->right); } delete no; } public: ~tree() { } tree(int n, int *tab) { root = new node(); root->w=-1; root->left = subtree(n - 1, tab); root->right = subtree(n - 1, tab + (1 << (n - 1))); } void print() { print(root); } void toss() { toss(root); } }; int main(void) { int n, t; scanf("%d %d", &n,&t); int pow = 2; for (int i = 1; i < n; ++i) pow *= 2; int *tab = new int[pow]; for (int i = 0; i < pow; ++i) { int a; scanf("%d", &tab[i]); } tree *drzewo = new tree(n,tab); if(t%2)drzewo->toss(); drzewo->print(); delete drzewo; return 0; } |