#![allow(non_snake_case, unused_must_use, unused_imports)] use std::io::{self, prelude::*}; fn main() { let (stdin, stdout) = (io::read_to_string(io::stdin()).unwrap(), io::stdout()); let (mut stdin, mut buffer) = (stdin.split_whitespace(), io::BufWriter::new(stdout.lock())); macro_rules! input { ($t: tt, $n: expr) => { (0..$n).map(|_| input!($t)).collect::>() }; (Chars) => { input!(String).chars().collect::>() }; (Usize1) => { stdin.next().unwrap().parse::().unwrap() - 1 }; ($t: ty) => { stdin.next().unwrap().parse::<$t>().unwrap() }; } let R = input!(usize); let N = input!(usize); let A = input!(usize, N); let mut bit = binary_indexed_tree::BinaryIndexedTree::new(R); for a in A { bit.add(a, 1); } for _ in 0..input!(u32) { let l = input!(usize); let r = input!(usize); let ql = l % R; let c1 = N * (l / R) + if ql > 0 { bit.sum(0..ql) } else { 0 }; let qr = r % R; let c2 = N * (r / R) + bit.sum(0..=qr); writeln!(buffer, "{}", c2 - c1); } } #[rustfmt::skip] pub mod binary_indexed_tree {#[derive(Clone, PartialEq, Eq, Debug)]pub struct BinaryIndexedTree { tree: Vec, pub size: usize,}impl< T: Default + Clone + Copy + PartialOrd + Ord + std::ops::AddAssign + std::ops::Sub + std::ops::Add, > BinaryIndexedTree{ pub fn new(size: usize) -> Self { return Self { tree: vec![T::default(); size + 1], size, }; } pub fn add(&mut self, i: usize, w: T) { assert!(i < self.size); self._add(i + 1, w); } pub fn prefix_sum(&self, i: usize) -> T { assert!(i < self.size, "size = {}, index = {}", self.size, i); self._sum(i + 1) } pub fn sum>(&self, range: R) -> T { let left = match range.start_bound() { std::ops::Bound::Included(&l) => l, std::ops::Bound::Excluded(&l) => l + 1, std::ops::Bound::Unbounded => 0, }; let right = match range.end_bound() { std::ops::Bound::Included(&r) => r, std::ops::Bound::Excluded(&r) => r - 1, std::ops::Bound::Unbounded => self.tree.len() - 2, }; if left == 0 { return self.prefix_sum(right); } else { return self.prefix_sum(right) - self.prefix_sum(left - 1); } } pub fn upper_bound(&self, w: T) -> usize { let mut d = self.tree.len().next_power_of_two() / 2; let mut j = 0; let mut u = T::default(); while d != 0 { if j + d < self.tree.len() { let v = u + self.tree[j + d]; if v <= w { u = v; j += d; } } d /= 2; } j } pub fn from(array: &[T]) -> Self { let mut tree = vec![T::default(); array.len() + 1]; for i in 1..tree.len() { let x = array[i - 1]; tree[i] += x; let j = i + (i & i.wrapping_neg()); if j < tree.len() { let v = tree[i]; tree[j] += v; } } Self { tree, size: array.len(), } } fn _add(&mut self, mut i: usize, w: T) { while i < self.tree.len() { self.tree[i] += w; i += i & i.wrapping_neg(); } } fn _sum(&self, mut i: usize) -> T { let mut ret = T::default(); while i > 0 { ret += self.tree[i]; i -= i & i.wrapping_neg(); } return ret; }}impl< T: Default + Clone + Copy + PartialOrd + Ord + std::ops::AddAssign + std::ops::Sub + std::fmt::Display + std::ops::Add, > std::fmt::Display for BinaryIndexedTree{ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "{}", (0..self.size) .map(|i| self.sum(i..=i)) .map(|v| v.to_string()) .collect::>() .join(" ") ) }}}