This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define fi first
#define se second
#define left BAO
#define right ANH
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz
#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
template<class X, class Y>
    bool minimize(X &x, const Y &y) {
        if (x > y)
        {
            x = y;
            return true;
        }
        return false;
    }
template<class X, class Y>
    bool maximize(X &x, const Y &y) {
        if (x < y)
        {
            x = y;
            return true;
        }
        return false;
    }
const int MOD = 1e9 + 7; //998244353
template<class X, class Y>
	void add(X &x, const Y &y) {
		x = (x + y);
		if(x >= MOD) x -= MOD;
	}
template<class X, class Y> 
	void sub(X &x, const Y &y) {
		x = (x - y);
		if(x < 0) x += MOD;
	}
/* Author : Le Ngoc Bao Anh, A5K37 Le Quy Don High School for Gifted Student, Da Nang */
/*       University of Wollongong       */
const ll INF = 1e9;
const int N = 3e5 + 10;
struct SegTree {
	int n;
	bool dbg = false;
	struct Node {
		ll lazy = 0;
		ll s = 0;
		ll val = INF * INF;
		ll sum = 0;
		ll cnt = 0;
		Node() {
			sum = 0;
			lazy = s = cnt = 0;
			val = INF * INF;
		}
	};
	vector<Node> node;
	SegTree(int _n = 0) {
		n = _n;
		node.resize(4 * n + 7);
		for(int i = 1; i <= 4 * n; i++) {
			node[i] = Node();
			node[i].val = 0;
		}
	}
private:
	void Down(int l, int r, int id) {
		ll x = node[id].lazy;
		node[id << 1].lazy += x;
		node[id << 1 | 1].lazy += x;
		node[id << 1].val += x;
		node[id << 1 | 1].val += x;
		int mid = (l + r) >> 1;
		node[id << 1].s += x * (mid - l + 1);
		node[id << 1 | 1].s += x * (r - mid);
		node[id << 1].sum += x * (mid - l + 1);
		node[id << 1 | 1].sum += x * (r - mid);
		node[id].lazy = 0;
	}
	pll walk(int l, int r, ll val, int id) {
		if(node[id].val >= val) {
			return {node[id].s, r - l + 1};
		}
		if(l == r) return {0, 0};
		Down(l, r, id);
		int mid = (l + r) >> 1;
		pll curr = walk(mid + 1, r, val, id << 1 | 1);
		if(curr.se == (r - mid)) {
			pll nxt = walk(l, mid, val, id << 1);
			return {curr.fi + nxt.fi, curr.se + nxt.se};
		} else return curr;
	}
	Node merge(const Node &a, const Node &b, int l, int r, int id) {
		Node ans = Node();
		if(a.val == INF * INF) return b;
		if(b.val == INF * INF) return a;
		int mid = (l + r) >> 1;
		if(a.val >= b.val) {
			ans.s = b.s + b.val * a.cnt;
		} else {
			pll curr = walk(l, mid, b.val, id << 1);
			ans.s = b.s + a.s - curr.fi + curr.se * b.val;
		}
		ans.sum = a.sum + b.sum;
		ans.cnt = a.cnt + b.cnt;
		ans.val = min(a.val, b.val);
		return ans;
	}
	void Update(int l, int r, int lo, int hi, ll val, int id) {
		if(l > hi || r < lo) return;
		if(lo <= l && r <= hi) {
			node[id].cnt = r - l + 1;
			node[id].s += val * (r - l + 1);
			node[id].val += val;
			node[id].lazy += val;
			node[id].sum += val * (r - l + 1);
			return;
		}
		Down(l, r, id);
		int mid = (l + r) >> 1;
		Update(l, mid, lo, hi, val, id << 1);
		Update(mid + 1, r, lo, hi, val, id << 1 | 1);
		node[id] = merge(node[id << 1], node[id << 1 | 1], l, r, id);
	}
	Node Get(int l, int r, int lo, int hi, int id) {
		if(l > hi || r < lo) return Node();
		if(lo <= l && r <= hi) return node[id];
		Down(l, r, id);
		int mid = (l + r) >> 1;
		Node left = Get(l, mid, lo, hi, id << 1);
		Node right = Get(mid + 1, r, lo, hi, id << 1 | 1);
		return merge(left, right, l, r, id);
	}
public:
	void update(int l, int r, ll val) {
		Update(1, n, l, r, val, 1);
	}
	pll get(int l, int r) {
		Node ans = Get(1, n, l, r, 1);
		return {ans.val, ans.sum - ans.s};
	}
};
ll a[N], b[N], ans[N], pref[N], t[N];
vector<pii> query[N];
void BaoJiaoPisu() {
	int n; ll d; cin >> n >> d;
	SegTree IT = SegTree(n);
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		IT.update(i, i, a[i]);
	}
	for(int i = 2; i <= n; i++) {
		b[i] = ((a[i] % d) + d - (a[i - 1] % d)) % d;  
		IT.update(i, n, -b[i]);
	}	
	int q; cin >> q;	
	for(int i = 1; i <= q; i++) {
		int l, r; cin >> l >> r;
		query[l].pb({r, i});
	}
	for(int i = 1; i <= n; i++) {
		IT.update(i, n, b[i]);	
		IT.update(i, n, -(a[i] % d));
		for(auto [r, id] : query[i]) {
			pll curr = IT.get(i, r);
			if(curr.fi < 0) {
				ans[id] = -1;
			} else {
				ll s = IT.get(i, r).se;
				assert(s % d == 0);
				ans[id] = s / d;
			}
		}
		IT.update(i, n, a[i] % d);
	}
	for(int i = 1; i <= q; i++) cout << ans[i] << '\n';
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1, ddd = 0;
    // cin >> tc;
    while(tc--) {
        //ddd++;
        //cout << "Case #" << ddd << ": ";
        BaoJiaoPisu();
    }
}
Compilation message (stderr)
Main.cpp: In function 'void BaoJiaoPisu()':
Main.cpp:214:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  214 |   for(auto [r, id] : query[i]) {
      |            ^
Main.cpp: In function 'int main()':
Main.cpp:233:17: warning: unused variable 'ddd' [-Wunused-variable]
  233 |     int tc = 1, ddd = 0;
      |                 ^~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |