import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; auto K1 = RD-1; auto K2 = RD-1; auto L = min(K1, K2); auto R = max(K1, K2); auto Q = RD; auto event = new long[][](Q); foreach (i; 0..Q) { auto a = RD; auto b = RD; event[i] = [a*2+1, i, b*2]; } auto cnt = new long[](N); long[] list_l, list_r; foreach (i; 0..L+1) list_l ~= i; foreach (i; R..N) list_r ~= i; auto rbt_l = new RedBlackTree!(long, "a > b")(list_l.dup); auto rbt_l2 = new RedBlackTree!(long, "a > b")(list_l.dup); auto rbt_r = new RedBlackTree!(long, "a < b")(list_r.dup); auto rbt_r2 = new RedBlackTree!(long, "a < b")(list_r.dup); auto ans = new long[](Q); long[][] wait; long lastT; auto heap = new BinaryHeap!(Array!(long[]), "a[0] > b[0]"); foreach (e; event) heap.insert(e); while (!heap.empty) { auto e = heap.front; heap.popFront; void inImpl(long id, long b) { long getP(ref RedBlackTree!(long, "a > b") tl, ref RedBlackTree!(long, "a < b") tr) { long res; long d1 = long.max, d2 = long.max; if (!tl.empty) d1 = L - tl.front; if (!tr.empty) d2 = tr.front - R; if (d1 == long.max && d2 == long.max) return long.max; if (d1 == d2) res = K1 == L ? L-d1 : d2+R; else if (d1 < d2) res = L-d1; else res = d2+R; return res; } auto p = getP(rbt_l, rbt_r); if (p == long.max) p = getP(rbt_l2, rbt_r2); if (p == long.max) { wait ~= [id, b]; return; } debug writeln("p:", p); ans[id] = p; heap.insert([lastT+b-1, id, -1]); foreach (i; p-1..p+2) { if (i < 0 || i >= N) continue; ++cnt[i]; if (cnt[i] == 1) { if (i <= L) rbt_l.removeKey(i); else rbt_r.removeKey(i); } } if (p <= L) rbt_l2.removeKey(p); else rbt_r2.removeKey(p); } while (e[0] != lastT && !wait.empty && (!rbt_l2.empty || !rbt_r2.empty)) { auto e2 = wait.front; wait.popFront; inImpl(e2[0], e2[1]); } lastT = e[0]; debug writeln(e, cnt); if (e[2] != -1) { inImpl(e[1], e[2]); } else { auto p = ans[e[1]]; foreach (i; p-1..p+2) { if (i < 0 || i >= N) continue; --cnt[i]; if (cnt[i] == 0) { if (i <= L) rbt_l.insert(i); else rbt_r.insert(i); } } if (p <= L) rbt_l2.insert(p); else rbt_r2.insert(p); } } foreach (e;ans) writeln(e+1); stdout.flush; debug readln; }