#include "bits/stdc++.h" using namespace std; using Trees = vector<vector<pair<int,int>>>; void PrintTree(const Trees& T, int b, int node) { if (b == 0) { cerr << T[b][node].first; } else { PrintTree(T, b-1, T[b][node].first); PrintTree(T, b-1, T[b][node].second); } } int Set(Trees& T, int b, int node, int p, int x) { if (b == 0) { T[b].emplace_back(x, 0); } else { if (p < (1<<b) / 2) { int ll = Set(T, b - 1, T[b][node].first, p, x); T[b].emplace_back(ll, T[b][node].second); } else { int rr = Set(T, b - 1, T[b][node].second, p - (1<<b)/2, x); T[b].emplace_back(T[b][node].first, rr); } } return (int) T[b].size() - 1; } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> ts(n); for (int i = 0; i < n; ++i) { cin >> ts[i]; } vector<int> ps(m - 1); vector<int> xs(m - 1); for (int i = 0; i < m - 1; ++i) { cin >> ps[i] >> xs[i]; --ps[i]; } int B = 0; while ((1<<B) < n) ++B; Trees T(B+1); for (int i = 0; i < n; ++i) { T[0].emplace_back(ts[i], 0); } for (int i = n; i < (1<<B); ++i) { T[0].emplace_back(-1, 0); } for (int b = 1; b <= B; ++b) { for (int i = 0; i * (1 << b) < (1<<B); ++i) { T[b].emplace_back(2*i, 2*i+1); } } for (int i = 0; i < m - 1; ++i) { Set(T, B, (int) T[B].size()-1, ps[i], xs[i]); } vector<vector<int>> H; H.emplace_back(T[0].size()); for (int i = 0; i < (int) T[0].size(); ++i) { H[0][i] = T[0][i].first; } for (int b = 1; b <= B; ++b) { vector<pair<pair<int, int>, int>> v; for (int i = 0; i < (int) T[b].size(); ++i) { auto ch = T[b][i]; auto hs = make_pair(H[b-1][ch.first], H[b-1][ch.second]); v.emplace_back(make_pair(hs, i)); } sort(v.begin(), v.end()); H.emplace_back(T[b].size()); int h = 0; for (int i = 0; i < (int) v.size(); ++i) { H[b][v[i].second] = h; if (i + 1 < (int) v.size() && v[i].first != v[i+1].first) { ++h; } } } vector<int> us(m); iota(us.begin(), us.end(), 0); stable_sort(us.begin(), us.end(), [&H, B](int i1, int i2) -> bool { return H[B][i1] < H[B][i2]; }); for (int u : us) { cout << u + 1<< ' '; } cout << '\n'; }
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 | #include "bits/stdc++.h" using namespace std; using Trees = vector<vector<pair<int,int>>>; void PrintTree(const Trees& T, int b, int node) { if (b == 0) { cerr << T[b][node].first; } else { PrintTree(T, b-1, T[b][node].first); PrintTree(T, b-1, T[b][node].second); } } int Set(Trees& T, int b, int node, int p, int x) { if (b == 0) { T[b].emplace_back(x, 0); } else { if (p < (1<<b) / 2) { int ll = Set(T, b - 1, T[b][node].first, p, x); T[b].emplace_back(ll, T[b][node].second); } else { int rr = Set(T, b - 1, T[b][node].second, p - (1<<b)/2, x); T[b].emplace_back(T[b][node].first, rr); } } return (int) T[b].size() - 1; } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> ts(n); for (int i = 0; i < n; ++i) { cin >> ts[i]; } vector<int> ps(m - 1); vector<int> xs(m - 1); for (int i = 0; i < m - 1; ++i) { cin >> ps[i] >> xs[i]; --ps[i]; } int B = 0; while ((1<<B) < n) ++B; Trees T(B+1); for (int i = 0; i < n; ++i) { T[0].emplace_back(ts[i], 0); } for (int i = n; i < (1<<B); ++i) { T[0].emplace_back(-1, 0); } for (int b = 1; b <= B; ++b) { for (int i = 0; i * (1 << b) < (1<<B); ++i) { T[b].emplace_back(2*i, 2*i+1); } } for (int i = 0; i < m - 1; ++i) { Set(T, B, (int) T[B].size()-1, ps[i], xs[i]); } vector<vector<int>> H; H.emplace_back(T[0].size()); for (int i = 0; i < (int) T[0].size(); ++i) { H[0][i] = T[0][i].first; } for (int b = 1; b <= B; ++b) { vector<pair<pair<int, int>, int>> v; for (int i = 0; i < (int) T[b].size(); ++i) { auto ch = T[b][i]; auto hs = make_pair(H[b-1][ch.first], H[b-1][ch.second]); v.emplace_back(make_pair(hs, i)); } sort(v.begin(), v.end()); H.emplace_back(T[b].size()); int h = 0; for (int i = 0; i < (int) v.size(); ++i) { H[b][v[i].second] = h; if (i + 1 < (int) v.size() && v[i].first != v[i+1].first) { ++h; } } } vector<int> us(m); iota(us.begin(), us.end(), 0); stable_sort(us.begin(), us.end(), [&H, B](int i1, int i2) -> bool { return H[B][i1] < H[B][i2]; }); for (int u : us) { cout << u + 1<< ' '; } cout << '\n'; } |