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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "bits/stdc++.h"

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#ifndef LOCAL
#undef assert
#define assert(...)
#endif

using ull = unsigned long long;
using ll = long long;
using ld = long double;

constexpr int nax = 600 * 1000 + 105;
constexpr int lognax = 22;
constexpr int infty = 1000 * 1000 * 1000 + 5;
constexpr int mod = 1000 * 1000 * 1000 + 7;

constexpr int p_cnt = 4;
constexpr int ps[p_cnt] = {20156573, 687132497, 52213741, 20310613};
constexpr int mods[p_cnt] = {1000 * 1000 * 1000 + 7, 1000 * 1000 * 1000 + 9,
                             999999103, 1000 * 1000 * 1000 + 103};

bool IsPrime(int p) {
  for (int i = 2; i * i <= p; i++) {
    if (p % i == 0) {
      return false;
    }
  }
  return true;
}

ll pot[p_cnt][nax];

struct Hasz {
  int val[p_cnt];

  Hasz() = default;
  Hasz(int digit, int p) {
    assert(0 <= p and p < nax);
    val[0] = (digit * pot[0][p]) % mods[0];
    val[1] = (digit * pot[1][p]) % mods[1];
    val[2] = (digit * pot[2][p]) % mods[2];
    val[3] = (digit * pot[3][p]) % mods[3];
  }

  void Dodaj(const Hasz& r) {
    val[0] += r.val[0];
    val[1] += r.val[1];
    val[2] += r.val[2];
    val[3] += r.val[3];
    val[0] -= ((mods[0] - 1 - val[0]) >> 31) & mods[0];
    val[1] -= ((mods[1] - 1 - val[1]) >> 31) & mods[1];
    val[2] -= ((mods[2] - 1 - val[2]) >> 31) & mods[2];
    val[3] -= ((mods[3] - 1 - val[3]) >> 31) & mods[3];
    /*
    if (val[0] >= mods[0]) val[0] -= mods[0];
    if (val[1] >= mods[1]) val[1] -= mods[1];
    if (val[2] >= mods[2]) val[2] -= mods[2];
    if (val[3] >= mods[3]) val[3] -= mods[3];
    */
  }

  bool operator==(const Hasz& h) const {
    return val[0] == h.val[0] and
           val[1] == h.val[1] and
           val[2] == h.val[2] and
           val[3] == h.val[3];
  }
};

debug& operator<<(debug& deb, const Hasz& h) {
  return deb << "H{" << h.val[0] << "}";
}

void HaszPreprocessing() {
  for (int i = 0; i < p_cnt; i++) {
    assert(IsPrime(ps[i]));
    assert(IsPrime(mods[i]));
    assert(ps[i] < mods[i]);
    assert(mods[i] < (numeric_limits<int>::max() / 2));
    pot[i][0] = 1;
  }
  for (int j = 1; j < nax; j++) {
    pot[0][j] = (pot[0][j - 1] * ps[0]) % mods[0];
    pot[1][j] = (pot[1][j - 1] * ps[1]) % mods[1];
    pot[2][j] = (pot[2][j - 1] * ps[2]) % mods[2];
    pot[3][j] = (pot[3][j - 1] * ps[3]) % mods[3];
  }
}

struct Drzewo {
  Hasz hasz;
  int l, r;
};

constexpr int drz_cnt = nax * 4 + nax * lognax + 105;
int next_drz = 0;
Drzewo drz[drz_cnt];

int NewDrz() {
  assert(next_drz < drz_cnt);
  return next_drz++;
}

int BudujDrzewo(int p, int k, int* ts) {
  debug() << "BudujDrzewo(" imie(p) imie(k) imie(range(ts, ts + (k - p + 1))) ")";
  const int w = NewDrz();
  Drzewo& d = drz[w];
  if (p == k) {
    d.l = ts[p];
    d.r = -1;
    d.hasz = Hasz(ts[p], p);
    return w;
  }
  d.l = BudujDrzewo(p, (p + k) / 2, ts);
  d.r = BudujDrzewo((p + k) / 2 + 1, k, ts);
  d.hasz = drz[d.l].hasz;
  d.hasz.Dodaj(drz[d.r].hasz);
  return w;
}

int Ustaw(int w, int p, int k, int pos, int t) {
  assert(p <= pos and pos <= k);
  Drzewo& d = drz[w];
  const int nw = NewDrz();
  Drzewo& nd = drz[nw];
  if (p == k) {
    nd.l = t;
    nd.r = -1;
    nd.hasz = Hasz(t, p);
    return nw;
  }
  const int s = (p + k) / 2;
  if (pos <= s) {
    nd.l = Ustaw(d.l, p, s, pos, t);
    nd.r = d.r;
  } else {
    nd.l = d.l;
    nd.r = Ustaw(d.r, s + 1, k, pos, t);
  }
  nd.hasz = drz[nd.l].hasz;
  nd.hasz.Dodaj(drz[nd.r].hasz);
  return nw;
}

int IsLess(int wa, int wb) {
  Drzewo& da = drz[wa];
  Drzewo& db = drz[wb];
  assert((da.r == -1) == (db.r == -1));
  if (da.r == -1) {
    debug() << imie(wa) imie(wb) imie(nax);
    return da.l < db.l;
  }
  if (drz[da.l].hasz == drz[db.l].hasz) {
    return IsLess(da.r, db.r);
  } else {
    return IsLess(da.l, db.l);
  }
}

void Algorithm(vector<int>& ts, vector<pair<int, int>>& zmiany) {
  debug() << "Algorithm(" imie(ts) imie(zmiany) ")";
  int n2 = 1;
  while (n2 < (int) ts.size()) {
    n2 *= 2;
  }
  assert(n2 < nax);
  while (ts.size() < n2) {
    ts.push_back(0);
  }
  assert(ts.size() == n2);
  vector<int> drzewa = {BudujDrzewo(0, n2 - 1, ts.data())};
  for (const auto& z : zmiany) {
    const int pos = z.first;
    const int t = z.second;
    debug() << imie(pos) imie(t);
    assert(0 <= pos and pos < n2);
    const int last_drzewo = drzewa.back();
    drzewa.push_back(Ustaw(last_drzewo, 0, n2 - 1, pos, t));
  }
  vector<int> ms;
  for (int i = 0; i < (int) drzewa.size(); i++) {
    ms.push_back(i);
  }
  sort(ms.begin(), ms.end(),
       [&drzewa, n2](int a, int b) -> bool {
         debug() << "Cmp(" imie(a) imie(b) ")";
         const int da = drzewa[a];
         const int db = drzewa[b];
         if (drz[da].hasz == drz[db].hasz) {
           debug() << "  -> hasze równe";
           return a < b;
         }
         debug() << "  -> porównanie";
         return IsLess(da, db);
       });
  for (int m : ms) {
    printf("%d ", m + 1);
  }
  printf("\n");
}

int main() {
  HaszPreprocessing();

  int n, m;
  scanf("%d%d", &n, &m);

  vector<int> ts;
  vector<pair<int, int>> zmiany;

  vector<int> all_ts;
  for (int i = 0; i < n; i++) {
    int t;
    scanf("%d", &t);
    all_ts.push_back(t);
    ts.push_back(t);
  }
  for (int i = 1; i < m; i++) {
    int p, t;
    scanf("%d%d", &p, &t);
    p--;
    all_ts.push_back(t);
    zmiany.emplace_back(p, t);
  }

  sort(all_ts.begin(), all_ts.end());
  all_ts.resize(unique(all_ts.begin(), all_ts.end()) - all_ts.begin());

  for (int& t : ts) {
    t = lower_bound(all_ts.begin(), all_ts.end(), t) - all_ts.begin();
  }
  for (pair<int, int>& z : zmiany) {
    z.second =
        lower_bound(all_ts.begin(), all_ts.end(), z.second) - all_ts.begin();
  }

  Algorithm(ts, zmiany);
  return 0;
}