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
#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define FORD(i,a,b) for(int i = (a); i >= (b); i--)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define M_PI 3.14159265358979323846
template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); }
template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); }

template<class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
	while (*sdbg != ',')cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...);
}

template<class T> ostream& operator<<(ostream& os, vector<T> V) {
	os << "["; for (auto& vv : V) os << vv << ","; os << "]";
	return os;
}

template<class T> ostream& operator<<(ostream& os, set<T> V) {
	os << "["; for (auto& vv : V) os << vv << ","; os << "]";
	return os;
}

template<class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
	return os << "(" << P.st << "," << P.nd << ")";
}


#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#endif

int p1 = 1e9 + 7;

struct IT {
	vector<vector<PII>> t;
	int s;
	IT(int n) {
		s = 1;
		while (s < n) {
			s *= 2;
		}
		t.resize(2 * s);
	}
	void build() {
		FORD(i, s - 1, 1) {
			vector<PII> & res = t[i];
			vector<PII> & lson = t[2 * i];
			vector<PII> & rson = t[2 * i + 1];
			int idx1 = 0, idx2 = 0;
			while (idx1 != SZ(lson) || idx2 != SZ(rson)) {
				if (idx1 == SZ(lson)) {
					res.push_back(rson[idx2++]);
					continue;
				}
				if (idx2 == SZ(rson)) {
					res.push_back(lson[idx1++]);
					continue;
				}
				if (lson[idx1].first == rson[idx2].first) {
					PII p = {
						lson[idx1].first,
						lson[idx1].second + rson[idx2].second
					};
					res.push_back(p);
					idx1++;
					idx2++;
				} else
				if (lson[idx1].first < rson[idx2].first) {
					res.push_back(lson[idx1++]);
				}
				else {
					res.push_back(rson[idx2++]);
				}
			}
		}
		FORD(i, s - 1, 1) {
			vector<PII> & res = t[i];
			FOR(j, 1, SZ(res) - 1) {
				res[j].second += res[j - 1].second;
			}
		}
	}

	int localGet(int x, int czas) {
		auto it = upper_bound(ALL(t[x]), MP(czas, LLONG_MAX));
		if (it == t[x].begin()) {
			return 0;
		}
		it--;
		return it->second;
	}

	int get(int l, int r, int czas) {
		l += s;
		r += s;
		int res = 0;
		while (l < r) {
			if (l & 1) {
				res += localGet(l++, czas);
			}
			if (!(r & 1)) {
				res += localGet(r--, czas);
			}
			l /= 2;
			r /= 2;
		}
		if (l == r) {
			res += localGet(l, czas);
		}
		return res;
	}

	void put(int x, int v, int czas) {
		x += s;
		t[x].push_back({czas, v});
	}
};

int32_t main(int32_t argc, char ** argv) {
	ios_base::sync_with_stdio(0);
	const int mx = 5e5;
	const int p1 = 1e9 + 7;
	VI pot(mx + 1);
	pot[0] = 1;
	FOR(i, 1, mx) {
		pot[i] = pot[i - 1] * p1;
	}

	int n, m;
	cin >> n >> m;
	vector<int> inp(n);
	//set<int> wszystkie;
	REP(i, n) {
		cin >> inp[i];
	}
	vector<vector<PII>> pom(n);
	REP(i, n) {
		pom[i].push_back({0, inp[i]});
	}
	auto getVal = [&](int uczen, int poz) {
		auto it = upper_bound(ALL(pom[poz]), MP(uczen, LLONG_MAX));
		it--;
		return it->second;
	};

	vector<PII> inp2(m - 1);
	REP(i, m - 1) {
		cin >> inp2[i].st >> inp2[i].nd;
		inp2[i].first--;
	}
	IT tr(m);
	VI tmp = inp;
	REP(i, m - 1) {
		int poz = inp2[i].first;
		int val = inp2[i].nd;
		pom[poz].push_back({ i + 1, val });
		int diff = val - tmp[poz];
		tmp[poz] = val;
		diff *= pot[poz];
		tr.put(i + 1, diff, poz);
	}
	tr.build();
	VI perm(m);
	REP(i, m) {
		perm[i] = i;
	}
	auto getFirstDiffPoz = [&](int a, int b) {
		int ta = min(a, b) + 1;
		int tb = max(a, b);
		int l = 0;
		int r = n;
		while (l < r) {
			int m = (l + r) / 2;
			int tmp = tr.get(ta, tb, m);
			if (!tmp) {
				l = m + 1;
			}
			else {
				r = m;
			}
		}
		return l;
	};

	getFirstDiffPoz(0, 3);
	sort(ALL(perm), [&](int a, int b) {
		int res = getFirstDiffPoz(a, b);
		if (res == n) {
			return a < b;
		}
		int v1 = getVal(a, res);
		int v2 = getVal(b, res);
		return v1 < v2;
	});

	REP(i, m) {
		cout << perm[i] + 1;
		cout << " ";
	}
	cout << "\n";
	return 0;
}