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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <algorithm>
#include <iostream>
#include <numeric>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

namespace {

constexpr int unregistered = -1;
constexpr int registered = -2;

struct Node;

struct NodePtr {
  int i = -1;

  NodePtr() = default;
  NodePtr(int i_): i{i_}
  {
  }

  Node& operator*() const;

  Node* operator->() const
  {
    return &operator*();
  }

  explicit operator bool() const
  {
    return i >= 0;
  }

  friend bool operator==(NodePtr a, NodePtr b)
  {
    return a.i == b.i;
  }

  friend bool operator!=(NodePtr a, NodePtr b)
  {
    return !(a == b);
  }
};

struct Node {
  NodePtr left;
  NodePtr right;
  int data;

  Node(NodePtr left_, NodePtr right_, int data_): left{left_}, right{right_}, data{data_}
  {
  }

  bool leaf() const
  {
    return !left && !right;
  }
};

vector<Node> nodes;
Node& NodePtr::operator*() const
{
  return nodes[i];
}

NodePtr new_internal_node(NodePtr left, NodePtr right)
{
  nodes.emplace_back(left, right, unregistered);
  return NodePtr{int(nodes.size() - 1)};
}

NodePtr new_leaf(int data)
{
  nodes.emplace_back(-1, -1, data);
  return NodePtr{int(nodes.size() - 1)};
}

template <typename I>
NodePtr create_tree(I begin, I end)
{
  int n = end - begin;
  if (n == 1) return new_leaf(*begin);
  auto middle = begin + n / 2;
  return new_internal_node(create_tree(begin, middle), create_tree(middle, end));
}

int compare_data(int a, int b)
{
  if (a < b) return -1;
  if (a > b) return 1;
  return 0;
}

int compare_trees(NodePtr a, NodePtr b)
{
  return compare_data(a->data, b->data);
}

void register_tree(vector<vector<NodePtr>>& levels, NodePtr root, int depth=0)
{
  if (root->data != unregistered) return;
  root->data = registered;

  if (int(levels.size()) <= depth) levels.resize(depth + 1);

  levels[depth].emplace_back(root);
  register_tree(levels, root->left, depth + 1);
  register_tree(levels, root->right, depth + 1);
}

NodePtr update_tree(NodePtr root, int n, int i, int x)
{
  if (root->leaf()) {
    if (root->data == x) return root;
    return new_leaf(x);
  }
  auto left = root->left;
  auto right = root->right;
  auto n2 = n / 2;
  if (i < n2) left = update_tree(left, n2, i, x);
  else right = update_tree(right, n - n2, i - n2, x);
  if (left == root->left && right == root->right) return root;
  return new_internal_node(left, right);
}

void compress(vector<int>& initial, vector<pair<int, int>>& changes)
{
  vector<int> interesting; interesting.reserve(initial.size() + changes.size());
  for (auto const& x: initial) interesting.emplace_back(x);
  for (auto const& c: changes) interesting.emplace_back(c.second);

  sort(interesting.begin(), interesting.end());
  interesting.erase(unique(interesting.begin(), interesting.end()), interesting.end());

  auto find = [&](int& x) {
    x = lower_bound(interesting.begin(), interesting.end(), x) - interesting.begin();
  };
  for (auto& x: initial) find(x);
  for (auto& c: changes) find(c.second);
}

template <int N, typename I, typename O>
void counting_sort(I begin, I end, O obegin)
{
  int n = end - begin;
  if (n == 0) return;
  int m = get<N>(*begin);
  for (auto it = begin; it != end; ++it) {
    m = max(m, get<N>(*it));
  }
  vector<int> counts(m + 1);
  for (auto it = begin; it != end; ++it) {
    ++counts[get<N>(*it)];
  }
  for (int i = 1; i <= m; ++i) {
    counts[i] += counts[i - 1];
  }
  for (int i = n - 1; i >= 0; --i) {
    auto& x = begin[i];
    auto& k = get<N>(x);
    --counts[k];
    obegin[counts[k]] = move(x);
  }
}

vector<int> solve(vector<int> initial, vector<pair<int, int>> changes)
{
  compress(initial, changes);

  int h = 1;
  int p = 1;
  while (p < int(initial.size())) {
    ++h;
    p *= 2;
  }
  initial.resize(p);

  nodes.reserve(2 * initial.size() - 1 + h * changes.size());

  vector<NodePtr> trees; trees.reserve(changes.size() + 1);
  trees.emplace_back(create_tree(initial.begin(), initial.end()));
  for (auto const& c: changes) {
    trees.emplace_back(update_tree(trees.back(), initial.size(), c.first, c.second));
  }

  vector<vector<NodePtr>> levels(h - 1);
  for (auto const& t: trees) {
    register_tree(levels, t);
  }

  for (auto l = levels.rbegin(); l != levels.rend(); ++l) {
    vector<tuple<int, int, NodePtr>> level; level.reserve(l->size());
    for (NodePtr n: *l) {
      level.emplace_back(n->left->data, n->right->data, n);
    }
    decltype(level) tmp(level.size());
    counting_sort<1>(level.begin(), level.end(), tmp.begin());
    counting_sort<0>(tmp.begin(), tmp.end(), level.begin());
    auto it = level.begin();
    int id = 0;
    while (it != level.end()) {
      auto it2 = it;
      while (it2 != level.end() && get<0>(*it) == get<0>(*it2) && get<1>(*it) == get<1>(*it2)) {
        get<2>(*it2)->data = id;
        ++it2;
      }
      it = it2;
      ++id;
    }
  }

  vector<int> perm(trees.size());
  iota(perm.begin(), perm.end(), 0);
  sort(perm.begin(), perm.end(), [&](int a, int b) {
    auto r = compare_trees(trees[a], trees[b]);
    if (r != 0) return r < 0;
    return a < b;
  });
  return perm;
}

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  vector<int> initial(n);
  for (auto& x: initial) cin >> x;

  vector<pair<int, int>> changes(m - 1);
  for (auto& c: changes) {
    cin >> c.first >> c.second;
    --c.first;
  }

  auto res = solve(move(initial), move(changes));

  bool first = true;
  for (int x: res) {
    if (first) first = false;
    else cout << ' ';
    cout << x + 1;
  }
  cout << endl;

  return 0;
}