/* -*- coding: utf-8 -*- * * 2961.cc: No.2961 Shiny Monster Master - yukicoder */ #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int MAX_R = 1000000; const int BN = 30; /* typedef */ /* global variables */ int as[MAX_N]; int cs[MAX_R][BN]; /* subroutines */ int calc(int u, int v, int r) { int sum = 0; for (int i = BN - 1; i >= 0; i--) if (((v - u) >> i) & 1) { sum += cs[u % r][i]; u += (1 << i); } return sum; } /* main */ int main() { int r, n; scanf("%d%d", &r, &n); for (int i = 0; i < n; i++) scanf("%d", as + i); sort(as, as + n); for (int u = 0, i = 0; u < r && i < n; u++) if (u == as[i]) cs[u][0] = 1, i++; for (int i = 0, bi = 1; i < BN - 1; i++, bi <<= 1) for (int u = 0; u < r; u++) cs[u][i + 1] = cs[u][i] + cs[(u + bi) % r][i]; int qn; scanf("%d", &qn); while (qn--) { int li, ri; scanf("%d%d", &li, &ri); int s = calc(li, ri + 1, r); printf("%d\n", s); } return 0; }