#include using namespace std; using LL = long long int; #define incID(i, l, r) for(int i = (l) ; i < (r); ++i) #define incII(i, l, r) for(int i = (l) ; i <= (r); ++i) #define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i) #define decII(i, l, r) for(int i = (r) ; i >= (l); --i) #define inc(i, n) incID(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec(i, n) decID(i, 0, n) #define dec1(i, n) decII(i, 1, n) #define inID(v, l, r) ((l) <= (v) && (v) < (r)) #define inII(v, l, r) ((l) <= (v) && (v) <= (r)) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() auto setmin = [](auto & a, auto b) { return (b < a ? a = b, true : false); }; auto setmax = [](auto & a, auto b) { return (b > a ? a = b, true : false); }; auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); }; auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); }; auto fl = [](auto a, auto b) { assert(b != 0); return a / b - (a % b != 0 && ((a >= 0) != (b >= 0)) ? 1 : 0); }; auto ce = [](auto a, auto b) { assert(b != 0); return a / b + (a % b != 0 && ((a >= 0) == (b >= 0)) ? 1 : 0); }; auto mo = [](auto a, auto b) { assert(b != 0); a %= b; if(a < 0) { a += abs(b); } return a; }; LL gcd(LL a, LL b) { return (b == 0 ? a : gcd(b, a % b)); } LL lcm(LL a, LL b) { return a / gcd(a, b) * b; } #define bit(b, i) (((b) >> (i)) & 1) #define SI(v) static_cast(v.size()) #define RF(e, v) for(auto & e: v) #define until(e) while(! (e)) #define if_not(e) if(! (e)) #define ef else if #define UR assert(false) // ---- ---- #define GPQ priority_queue template class LPQ : public GPQ, greater> { }; int main() { int n; cin >> n; vector pos(n), ord(n, -1); cin >> pos[0] >> pos[1]; pos[0]--; pos[1]--; ord[pos[0]] = 0; ord[pos[1]] = 1; incID(i, 2, n) { int x = pos[i - 1]; int y = pos[i - 2]; if(x < y) { x--; y++; } else { y--; x++; } pos[i] = (inID(y, 0, n) && ord[y] == -1 ? y : x); ord[pos[i]] = i; } vector state(n, 0); set> SEAT; inc(i, n) { SEAT.emplace(0, i); } LPQ> out; auto check = [&](int pos) { if_not(inID(pos, 0, n)) { return; } SEAT.erase({ 0, ord[pos] }); SEAT.erase({ 1, ord[pos] }); if(state[pos] == 2) { return; } bool x = (pos == 0 || state[pos - 1] <= 1); bool y = (pos == n - 1 || state[pos + 1] <= 1); state[pos] = (x && y ? 0 : 1); SEAT.emplace(state[pos], ord[pos]); }; auto IN = [&](int pos) { // cout << "IN: " << pos + 1 << endl; assert(state[pos] <= 1); state[pos] = 2; check(pos ); check(pos - 1); check(pos + 1); }; auto OUT = [&](int pos) { // cout << "OUT: " << pos + 1 << endl; assert(state[pos] == 2); state[pos] = 0; check(pos ); check(pos - 1); check(pos + 1); }; auto print = [&]() { inc(i, n) { cout << "OoX"[state[i]]; } cout << endl; }; LL time = 0; int q; cin >> q; inc(i, q) { LL a, b; cin >> a >> b; setmax(time, a); { while(SI(out) > 0 && out.top().FI <= time) { OUT(out.top().SE); out.pop(); } } if(SI(SEAT) == 0) { time = out.top().FI; while(SI(out) > 0 && out.top().FI == time) { OUT(out.top().SE); out.pop(); break; } // test } // cout << "t = " << time << endl; // print(); int p = pos[SEAT.begin() -> SE]; IN(p); out.emplace(time + b, p); // print(); cout << p + 1 << "\n"; // cout << endl; } return 0; }