// yuki 953 席 // 2019.1.12 bal4u #include #if 1 int getchar_unlocked(void); int putchar_unlocked(int c); #define gc() getchar_unlocked() #define pc(c) putchar_unlocked(c) #else #define gc() getchar() #define pc(c) putchar(c) #endif int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } void out(int n) { // 非負整数の表示(出力) int i; char b[30]; if (!n) pc('0'); else { i = 0; while (n) b[i++] = n % 10 + '0', n /= 10; while (i--) pc(b[i]); } pc('\n'); } #define MAX 100010 #define MAN 0 #define SEAT 1 typedef struct { int a, n; } QUE; QUE que[2][MAX]; int qsize[2]; #define PARENT(i) ((i)>>1) #define LEFT(i) ((i)<<1) #define RIGHT(i) (((i)<<1)+1) int pqcmp(int id, int u, int v); void min_heapify(int id, int i) { int l, r, mi; l = LEFT(i), r = RIGHT(i); if (l < qsize[id] && pqcmp(id, l, i)) mi = l; else mi = i; if (r < qsize[id] && pqcmp(id, r, mi)) mi = r; if (mi != i) { QUE qt = que[id][i]; que[id][i] = que[id][mi]; que[id][mi] = qt; min_heapify(id, mi); } } void deq(int id) { que[id][0] = que[id][--qsize[id]]; min_heapify(id, 0); } void enq(int id, int a, int n) { int i, mi; i = qsize[id]++; que[id][i].a = a, que[id][i].n = n; while (i > 0 && pqcmp(id, i, mi = PARENT(i))) { QUE qt = que[id][i]; que[id][i] = que[id][mi]; que[id][mi] = qt; i = mi; } } typedef struct { int n; char f; } T; T seat[MAX]; int N, K1, K2; int pqcmp(int id, int u, int v) { if (id == MAN) return que[id][u].a < que[id][v].a; return seat[que[id][u].n].n < seat[que[id][v].n].n; } int sq[MAX], top, end; int next_seat() { int n, ans; top = end = 0; while (qsize[SEAT]) { n = que[SEAT][0].n, deq(SEAT); if (n == 1) { if (seat[n+1].f == 0) { ans = n; goto OK; } } else if (n == N) { if (seat[n-1].f == 0) { ans = n; goto OK; } } else { if (seat[n-1].f == 0 && seat[n+1].f == 0) { ans = n; goto OK; } } sq[end++] = n; } ans = sq[top++]; OK: while (top < end) enq(SEAT, 0, sq[top++]); //printf("next_seat %d\n", ans); return ans; } int main() { int i, j, n; N = in(), K1 = in(), K2 = in(); n = 0; seat[K1].n = ++n, seat[K2].n = ++n; enq(SEAT, 0, K1), enq(SEAT, 0, K2); j = 2; for (i = 1; j < N; ++i) { if (K2 < K1) { if (K1 + i <= N) { seat[K1+i].n = ++n, ++j; enq(SEAT, 0, K1+i); } if (K2 - i >= 1) { seat[K2-i].n = ++n, ++j; enq(SEAT, 0, K2-i); } } else { if (K1 - i >= 1) { seat[K1-i].n = ++n, ++j; enq(SEAT, 0, K1-i); } if (K2 + i <= N) { seat[K2+i].n = ++n, ++j; enq(SEAT, 0, K2+i); } } } int Q = in(); while (Q--) { int a = in(), b = in(); if (qsize[SEAT] == 0 && que[MAN][0].a > a) a = que[MAN][0].a; while (qsize[MAN] > 0 && que[MAN][0].a <= a) { n = que[MAN][0].n; deq(MAN), seat[n].f = 0; enq(SEAT, 0, n); } n = next_seat(); seat[n].f = 1, enq(MAN, a+b, n); out(n); } return 0; }