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
#include <cstdio>
#include <unordered_map>
#include <algorithm>
#include <vector>

#define lld long long int
#define MAXN 524288
#define TEMPIDS 1000000002

using namespace std;

int n, m, p2n = 1, p2 = 0;
int t[MAXN], tids[MAXN];

vector<int> tv[21]; // here we update values for now

vector<pair<int,int>> pairMap(1); // get index - TEMPIDS
//reverse map

int mainID = 1;

int cmdx(int a, int b) {
	while (a > 0) {
		if (pairMap[a].first != pairMap[b].first) {
			a = pairMap[a].first; 
			b = pairMap[b].first;
		} else 
		if (pairMap[a].second != pairMap[b].second) {
			a = pairMap[a].second;
			b = pairMap[b].second;
		} else {
			return 0;
		}
	}
	if (a < b) return 1;
	if (b < a) return -1;
	return 0;
}

bool cmdxx(int a, int b) {
	int res = cmdx(tids[a], tids[b]);
	if (res == -1) {
		return 0;
	} 
	if (res == 1) {
		return 1;
	}
	return a < b;
}

int main() {
	scanf("%d%d", &n, &m);
	while (p2n < n) {
		p2n = p2n * 2;
		p2++;
	}

	for (int i = 0; i < n; i++) {
		int a;
		scanf("%d", &a);
		tv[0].push_back(a - TEMPIDS);
	}
	for (int i = n; i < p2n; i++) tv[0].push_back(0);

	for (int p = 1; p <= p2; p++) {
		for (int i = 0; 2 * i < tv[p - 1].size(); i++) {
			pairMap.push_back(make_pair(tv[p - 1][2 * i], tv[p - 1][2 * i + 1]));
			tv[p].push_back(mainID++);
		}
	}
	tids[0] = tv[p2][0];

	for (int i = 1; i < m; i++) {
		t[i] = i;
		int a, b;
		scanf("%d%d", &a, &b);
		a--;
		tv[0][a] = b - TEMPIDS;
		for (int p = 1; p <= p2; p++) {
			a = a / 2;
			pairMap.push_back(make_pair(tv[p - 1][2 * a], tv[p - 1][2 * a + 1]));
			tv[p][a] = mainID++;
		}
		tids[i] = tv[p2][0];
	}
	sort(t, t + m, cmdxx);
	for (int i = 0; i < m; i++) {
		printf("%d ", t[i] + 1);
	}
	printf("\n");
	return 0;
}