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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(u) (u).begin(),(u).end()

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

typedef pair<int, int> PII;

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

const int MR = 5e5 + 10;

const int pr = 70032301, q = 1000000007;

// persistent segment tree
struct node
{
	int v;
	int p, k;

	node *left, *right;
};

node *tree[MR];

int t[MR], potp[MR];

int p, k, v;
void build(node *root, int p, int k)
{

	root->p = p;
	root->k = k;

	if (p < k)
	{
		if (root->left == NULL)
			root->left = new node();
		build(root->left, p, (p + k) / 2);

		if (root->right == NULL)
			root->right = new node();
		build(root->right, (p + k) / 2 + 1, k);

		root->v = (root->left->v + root->right->v*(LL)potp[root->left->k - root->left->p + 1]) % q;

		return;
	}

	root->v = t[p - 1];
}

// zwroc hasz na danym przedziale
int ans(node *root)
{
	if (root->p > k || root->k < p)
		return 0;
	if (root->p < p || root->k > k)
	{
		int vl = ans(root->left);
		int vr = ans(root->right);

		if (vr && root->left->k >= p)
			vr = vr*(LL)potp[root->left->k - p + 1] % q;
		return vl + vr;
	}

	return root->v;
}



// przed modyfikacja musisz wstawic segment tree dla nowego wierzcholka
node* insert(node *root)
{
	// if out of range, return this node
	if (root->k < p || root->p > p)
		return root;

	node *res = new node();

	res->p = root->p;
	res->k = root->k;
	res->v = root->v;

	if (root->p < root->k)
	{
		res->left = insert(root->left);
		res->right = insert(root->right);
	}

	return res;
}

void addNew(int nr, int par)
{
	tree[nr] = insert(tree[par]);
}

void modify(node *root)
{
	if (root->p > p || root->k < p)
		return;
	if (root->p < p || root->k > p)
	{
		modify(root->left);
		modify(root->right);

		root->v = (root->left->v + root->right->v*(LL)potp[root->left->k - root->left->p + 1]) % q;

		return;
	}

	root->v = v;
}

//int verRes;
//void verify(node *root)
//{
//	if (root->k < p || root->p > p)
//		return;
//	if (root->p < p || root->k > p)
//	{
//		verify(root->left);
//		verify(root->right);
//		return;
//	}
//
//	verRes = root->v;
//
//}

//bool check(int v1, int v2, int k)
//{
//	vector<int> pos;
//	REP(i, 5)
//		pos.push_back(rand()*(LL)rand() % k + 1);
//
//	for (int i : pos)
//	{
//		p = i;
//		verify(tree[v1]);
//		int prev = verRes;
//		verify(tree[v2]);
//		if (prev != verRes)
//			return 0;
//	}
//
//	return 1;
//}

int n;
int find(const node *t1, const node *t2)
{
	// zwroc pierwszy indeks, na ktorym rozni sie hash
	if (t1->v == t2->v)
		return t1->k + 1;

	if (t1->p == t1->k)
		return t1->p;

	int left = find(t1->left, t2->left);

	if (left <= t1->left->k)
		return left;

	return find(t1->right, t2->right);
}

inline bool cmp(const int &i1, const int &i2)
{
	int ind = find(tree[i1], tree[i2]);
	//printf("cmp %d %d, roznia sie na pos: %d\n", i1, i2, ind);
	// jesli hasze sa takie same, to posortuj po numerach
	if (ind > n)
		return i1 < i2;

	p = k = ind;

	int v1 = ans(tree[i1]), v2 = ans(tree[i2]);
	return v1 < v2;
}

int srt[MR], cp[MR];

void mergesort(int p, int k)
{
	if (p >= k - 1)
		return;
	if (p == k - 2)
	{
		if (cmp(srt[p + 1], srt[p]))
			swap(srt[p + 1], srt[p]);
		return;
	}

	int half = (p + k) / 2;
	mergesort(p, half);
	mergesort(half, k);

	int b1 = p, b2 = half, wsk = 0;

	while (b1 < half && b2 < k)
		if (cmp(srt[b1], srt[b2]))
			cp[wsk++] = srt[b1++];
		else
			cp[wsk++] = srt[b2++];

	while (b1 < half)
		cp[wsk++] = srt[b1++];
	while (b2 < k)
		cp[wsk++] = srt[b2++];

	REP(i, wsk)
		srt[p + i] = cp[i];
}

int main()
{
	/*n = 5e5;
	int m = 5e5;

	REP(i, n)
	t[i] = rand()*(LL)rand() % 1000000000 + 1;*/

	int m;
	scanf("%d%d", &n, &m);
	REP(i, n)
	{
		scanf("%d", &t[i]);
		t[i]++;
	}

	potp[0] = 1;
	FORQ(i, 1, n)
		potp[i] = (potp[i - 1] * (LL)pr) % q;

	int sz = 1;
	while (sz < n)
		sz *= 2;
	tree[0] = new node();
	build(tree[0], 1, sz);

	FOR(i, 1, m)
	{
		scanf("%d%d", &p, &v); v++;
		/*p = rand()*(LL)rand() % n + 1;
		v = rand()*(LL)rand() % 1000000000 + 1;*/
		addNew(i, i - 1);
		modify(tree[i]);
		srt[i] = i;
	}

	/*REP(i, 500000)
	{
	p = rand()*(LL)rand() % n + 1;
	k = rand()*(LL)rand() % n + 1;
	int id1 = rand()*(LL)rand() % m;
	int id2 = rand()*(LL)rand() % m;
	if (p > k)
	swap(p, k);
	ans(tree[id1]);
	BS(id1, id2);
	}*/

	mergesort(0, m);

	REP(i, m)
		printf("%d ", srt[i] + 1);
	printf("\n");

	return 0;
}