#include #include #include #include #include #include #include template using MinHeap = std::priority_queue, std::greater>; using lint = long long; void solve() { int n, k1, k2; std::cin >> n >> k1 >> k2; bool first1 = true; if (k1 > k2) { first1 = false; std::swap(k1, k2); } // k1 < k2 int m; std::cin >> m; MinHeap> ts; std::vector bs(m); for (int i = 1; i <= m; ++i) { lint a; std::cin >> a >> bs[i - 1]; ts.emplace(a, i); } std::vector i2d(n + 2, n); for (int i = 1; i <= n; ++i) { i2d[i] = std::abs(i - k1) + std::abs(i - k2) - ((i <= k1) == first1); } ++i2d[n + 1]; std::vector d2i(n + 2, 0); for (int i = 0; i <= n + 1; ++i) d2i[i2d[i]] = i; // for (auto i : d2i) std::cerr << i << " "; // std::cerr << "\n"; std::set s1; for (int i = 0; i <= n + 1; ++i) s1.insert(i2d[i]); auto s2 = s1; auto get = [&]() -> int { int d = *s1.begin(); if (d >= n) { d = *s2.begin(); if (d >= n) return -1; } int i = d2i[d]; for (int j = i - 1; j <= i + 1; ++j) { if (1 <= j && j <= n) s1.erase(i2d[j]); } s2.erase(d); return i; }; auto check = [&](int i) -> bool { if (i == 0 || i == n + 1) return false; for (int j = i - 1; j <= i + 1; ++j) { if (!s2.count(i2d[j])) return false; } return true; }; auto dump = [&]() { std::cerr << "s1: "; for (auto d : s1) if (d < n) std::cerr << d2i[d] << " "; std::cerr << "\n"; std::cerr << "s2: "; for (auto d : s2) if (d < n) std::cerr << d2i[d] << " "; std::cerr << "\n"; }; std::vector ans(m, -1); std::queue que; lint pt = -1; while (!ts.empty()) { // std::cerr << "\n"; auto [t, x] = ts.top(); // std::cerr << "t: " << t << "\n"; ts.pop(); // dump(); if (x > 0) que.push(x); if (t != pt) { while (!que.empty()) { int i = get(); if (i == -1) break; int x = que.front(); que.pop(); ans[x - 1] = i; ts.emplace(t + bs[x - 1], -x); // std::cerr << "sit: " << i << "\n"; } } pt = t; if (x < 0) { int i = ans[-x - 1]; // std::cerr << "free: " << i << "\n"; s2.insert(i2d[i]); for (int j = i - 1; j <= i + 1; ++j) { if (check(j)) s1.insert(i2d[j]); } } } for (auto a : ans) { std::cout << a << "\n"; } } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }