#define _USE_MATH_DEFINES #include using namespace std; vector order; struct Comp { bool operator() (const pair& x, const pair& y) const { if (x.first != y.first) return x.first < y.first; return order[x.second] < order[y.second]; } }; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, k1, k2; cin >> n >> k1 >> k2; order = vector(n + 1); order[k1] = 0; order[k2] = 1; if (k2 > k1) { int c = 2; for (int i = k1 - 1; i >= 1; i--) { order[i] = c; c += 2; } c = 3; for (int i = k2 + 1; i <= n; i++) { order[i] = c; c += 2; } } else { int c = 2; for (int i = k1 + 1; i <= n; i++) { order[i] = c; c += 2; } c = 3; for (int i = k2 - 1; i >= 1; i--) { order[i] = c; c += 2; } } set, Comp> available; vector occupy(n + 5); auto whenCome = [&] (int idx) { occupy[idx] = true; available.erase(make_pair(0, idx)); available.erase(make_pair(1, idx)); if (idx - 1 >= 1 && !occupy[idx - 1]) { available.erase(make_pair(0, idx - 1)); available.insert(make_pair(1, idx - 1)); } if (idx + 1 <= n && !occupy[idx + 1]) { available.erase(make_pair(0, idx + 1)); available.insert(make_pair(1, idx + 1)); } }; auto whenLeave = [&] (int idx) { occupy[idx] = false; if (!occupy[idx - 1] && !occupy[idx + 1]) available.insert(make_pair(0, idx)); else available.insert(make_pair(1, idx)); if (!occupy[idx - 1]) { if (idx - 1 >= 1 && !occupy[idx - 2]) { available.erase(make_pair(1, idx - 1)); available.insert(make_pair(0, idx - 1)); } } if (!occupy[idx + 1]) { if (idx + 1 <= n && !occupy[idx + 2]) { available.erase(make_pair(1, idx + 1)); available.insert(make_pair(0, idx + 1)); } } }; auto come = [&] () { if (available.empty()) return -1; int idx = available.begin()->second; whenCome(idx); return idx; }; for (int i = 1; i <= n; i++) available.insert(make_pair(0, i)); int q; cin >> q; vector a(q), b(q); for (int i = 0; i < q; i++) cin >> a[i] >> b[i]; auto aa = a; { priority_queue, greater> que; for (int i = 0; i < q; i++) { if (i == 86515 || i == 86515 - 1) { //cerr << i << " " << a[i] << " " << que.size() << " " << (que.empty() ? -1LL : que.top()) << endl; while (!que.empty() && que.top() <= a[i]) que.pop(); //cerr << i << " " << a[i] << " " << que.size() << " " << (que.empty() ? -1LL : que.top()) << endl; if ((int) que.size() < n) { if (!que.empty() && que.top() > a[i]) { a[i] = a[i - 1]; } que.push(a[i] + b[i]); continue; } long long mx = que.top(); while (!que.empty() && que.top() <= mx) que.pop(); a[i] = mx; //cerr << a[i] << " " << a[i] + b[i] << endl; que.push(a[i] + b[i]); } else { while (!que.empty() && que.top() <= a[i]) que.pop(); //cout << i << " " << que.size() << " " << (que.empty() ? -1LL : que.top()) << endl; if ((int) que.size() < n) { que.push(a[i] + b[i]); continue; } long long mx = que.top(); while (!que.empty() && que.top() <= mx) que.pop(); a[i] = mx; que.push(a[i] + b[i]); } } } priority_queue, vector>, greater>> que; vector ans(q); for (int i = 0; i < q; i++) { while (!que.empty() && que.top().first <= a[i]) { int p = que.top().second; que.pop(); whenLeave(ans[p]); } int idx = come(); ans[i] = idx; que.emplace(a[i] + b[i], i); } for (int i = 1; i < q; i++) { // if (a[i - 1] > a[i]) cerr << i << endl; // assert(a[i - 1] <= a[i]); } // vector c_ans(q); // for (int i = 0; i < q; i++) cin >> c_ans[i]; // for (int i = 0; i < q; i++) { // cout << ans[i] << " " << c_ans[i] << " " << " " << aa[i] << " " << a[i] << " " << a[i] + b[i] << " correct = " << (ans[i] == c_ans[i]) << '\n'; // } // return 0; for (auto& x : ans) cout << x << '\n'; return 0; }