// Sandglass
// * frank_c1
// * 2017 / 11/ 07
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)(1e5) + 5;
pair<int, pair<int, int> > w[maxn];
int res[maxn], ri[maxn];
int main() {
int X, K;
scanf("%d%d", &X, &K);
for (int i = 1; i <= K; ++i) scanf("%d", &ri[i]);
int m; scanf("%d", &m);
for (int i = 1; i <= m; ++i) {
int t, a; scanf("%d%d", &t, &a); w[i] = make_pair(t, make_pair(a, i));
}
sort(w + 1, w + m + 1);
int v = 0, l = 0, r = X, ps = 1;
for (int i = 0; i <= K; ++i) {
for (; ps <= m && (i == K || w[ps].first < ri[i + 1]); ++ps) {
int t = w[ps].first - ri[i], a = w[ps].second.first, id = w[ps].second.second;
if (i & 1) {
if (l < 0) res[id] = min(X, v + t);
else {
int nv = v, nl = l, nr = r;
int c = min(t, X - (nv + nr - nl)); nv += c; t -= c;
c = min(t, nr - nl); nv += c; nr -= c; t -= c; if (nl == nr) nl = nr = -1;
if (nl < 0) res[id] = nv; else if (a <= nl) res[id] = nv; else if (a <= nr) res[id] = nv + a - nl; else res[id] = nv + nr - nl;
}
} else {
if (l < 0) res[id] = max(0, v - t);
else {
int nv = v, nl = l, nr = r;
int c = min(t, nv); nv -= c; t -= c;
c = min(t, nr - nl); nl += c; t -= c; if (nl == nr) nl = nr = -1;
if (nl < 0) res[id] = nv; else if (a <= nl) res[id] = nv; else if (a <= nr) res[id] = nv + a - nl; else res[id] = nv + nr - nl;
}
}
}
if (i < K) {
int t = ri[i + 1] - ri[i];
if (i & 1) {
if (l < 0) v = min(X, v + t);
else {
int c = min(t, X - (v + r - l)); v += c; t -= c;
c = min(t, r - l); v += c; r -= c; t -= c; if (l == r) l = r = -1;
}
} else {
if (l < 0) v = max(0, v - t);
else {
int c = min(t, v); v -= c; t -= c;
c = min(t, r - l); l += c; t -= c; if (l == r) l = r = -1;
}
}
}
}
for (int i = 1; i <= m; ++i) printf("%d\n", res[i]);
return 0;
}
./Main.cpp: In function ‘int main()’:
./Main.cpp:14:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &X, &K);
^
./Main.cpp:15:50: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= K; ++i) scanf("%d", &ri[i]);
^
./Main.cpp:16:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
int m; scanf("%d", &m);
^
./Main.cpp:18:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
int t, a; scanf("%d%d", &t, &a); w[i] = make_pair(t, make_pair(a, i));
^