#include <iostream> #include <algorithm> using namespace std; #define P 1000000007 int D; typedef unsigned long long ull; struct tree{ ull val; tree *left; tree *right; }; struct praca{ int numer; tree *t; }; ull hsh(ull a, ull b) { return a * 2654435761ull + b * 2654435741ull; } void make_tree(int deepth, tree *t) { if (deepth == 0) { t -> val = 1; t -> left = NULL; t -> right = NULL; } else { t -> left = new tree; t -> right = new tree; make_tree(deepth - 1, t -> left); make_tree(deepth - 1, t -> right); t -> val = hsh (t -> left -> val, t -> right -> val); } } void to_arr(int n, bool a[]) { for (int i = 0; i < D; i++) { a[i] = n % 2; n /= 2; } } int log(int n, int acc) { if (n == 1) return acc; else return log(n / 2 + n % 2, acc + 1); } int log(int n) { return log(n, 0); } void update_tree(int deepth, tree *t, bool index[], ull x) { if (deepth == 0) t -> val = x; else { deepth--; tree *l = t -> left; tree *r = t -> right; if (index[deepth]) update_tree(deepth, r, index, x); else update_tree(deepth, l, index, x); t -> val = hsh (l -> val, r -> val); } } ull read_tree(int deepth, tree *t, bool index[]) { if (deepth == 0) return t -> val; else { deepth--; if (index[deepth]) return read_tree(deepth, t -> right, index); else return read_tree(deepth, t -> left, index); } } void copy_tree(int deepth, tree *org, tree *copy, bool index[], ull new_val) { if (deepth == 0) { copy -> val = new_val; copy -> left = NULL; copy -> right = NULL; } else { deepth--; if (index[deepth]) { copy -> left = org -> left; copy -> right = new tree; copy_tree(deepth, org -> right, copy -> right, index, new_val); } else { copy -> right = org -> right; copy -> left = new tree; copy_tree(deepth, org -> left, copy -> left, index, new_val); } copy -> val = hsh (copy -> left -> val, copy -> right -> val); } } bool mycomp (int deepth, tree *a, tree *b) { if (deepth == 0) { return (a -> val < b -> val); } else { if (a -> val == b -> val) return false; if (a -> left -> val != b -> left -> val) return mycomp(deepth - 1, a -> left, b -> left); return mycomp (deepth - 1, a -> right, b -> right); } } bool mycompprac(praca a, praca b) { if (mycomp(D, a.t, b.t)) { return true; } /*else { if (mycomp(D, b.t, a.t)) return false; else { return a.numer < b.numer; } }*/ return false; } int main() { int n, m; cin>>n>>m; praca k[m]; k[0].t = new tree; k[0].numer = 1; D = log(n) + 1; make_tree(D, k[0].t); for (int i = 0; i < n; i++) { int t; bool in[D]; to_arr(i, in); cin>>t; update_tree(D, k[0].t, in, t); } for (int i = 1; i < m; i++) { int p, val; bool in[D]; cin>>p>>val; p--; to_arr(p, in); k[i].t = new tree; k[i].numer = i + 1; copy_tree(D, k[i - 1].t, k[i].t, in, val); } stable_sort(k, k + m, mycompprac); for(int i = 0; i < m; i++) cout<<k[i].numer<<" "; cout<<endl; }
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | #include <iostream> #include <algorithm> using namespace std; #define P 1000000007 int D; typedef unsigned long long ull; struct tree{ ull val; tree *left; tree *right; }; struct praca{ int numer; tree *t; }; ull hsh(ull a, ull b) { return a * 2654435761ull + b * 2654435741ull; } void make_tree(int deepth, tree *t) { if (deepth == 0) { t -> val = 1; t -> left = NULL; t -> right = NULL; } else { t -> left = new tree; t -> right = new tree; make_tree(deepth - 1, t -> left); make_tree(deepth - 1, t -> right); t -> val = hsh (t -> left -> val, t -> right -> val); } } void to_arr(int n, bool a[]) { for (int i = 0; i < D; i++) { a[i] = n % 2; n /= 2; } } int log(int n, int acc) { if (n == 1) return acc; else return log(n / 2 + n % 2, acc + 1); } int log(int n) { return log(n, 0); } void update_tree(int deepth, tree *t, bool index[], ull x) { if (deepth == 0) t -> val = x; else { deepth--; tree *l = t -> left; tree *r = t -> right; if (index[deepth]) update_tree(deepth, r, index, x); else update_tree(deepth, l, index, x); t -> val = hsh (l -> val, r -> val); } } ull read_tree(int deepth, tree *t, bool index[]) { if (deepth == 0) return t -> val; else { deepth--; if (index[deepth]) return read_tree(deepth, t -> right, index); else return read_tree(deepth, t -> left, index); } } void copy_tree(int deepth, tree *org, tree *copy, bool index[], ull new_val) { if (deepth == 0) { copy -> val = new_val; copy -> left = NULL; copy -> right = NULL; } else { deepth--; if (index[deepth]) { copy -> left = org -> left; copy -> right = new tree; copy_tree(deepth, org -> right, copy -> right, index, new_val); } else { copy -> right = org -> right; copy -> left = new tree; copy_tree(deepth, org -> left, copy -> left, index, new_val); } copy -> val = hsh (copy -> left -> val, copy -> right -> val); } } bool mycomp (int deepth, tree *a, tree *b) { if (deepth == 0) { return (a -> val < b -> val); } else { if (a -> val == b -> val) return false; if (a -> left -> val != b -> left -> val) return mycomp(deepth - 1, a -> left, b -> left); return mycomp (deepth - 1, a -> right, b -> right); } } bool mycompprac(praca a, praca b) { if (mycomp(D, a.t, b.t)) { return true; } /*else { if (mycomp(D, b.t, a.t)) return false; else { return a.numer < b.numer; } }*/ return false; } int main() { int n, m; cin>>n>>m; praca k[m]; k[0].t = new tree; k[0].numer = 1; D = log(n) + 1; make_tree(D, k[0].t); for (int i = 0; i < n; i++) { int t; bool in[D]; to_arr(i, in); cin>>t; update_tree(D, k[0].t, in, t); } for (int i = 1; i < m; i++) { int p, val; bool in[D]; cin>>p>>val; p--; to_arr(p, in); k[i].t = new tree; k[i].numer = i + 1; copy_tree(D, k[i - 1].t, k[i].t, in, val); } stable_sort(k, k + m, mycompprac); for(int i = 0; i < m; i++) cout<<k[i].numer<<" "; cout<<endl; } |