We are asked: "report the between the maximum number and minimum number whose indices lie within x and y (both inclusive)". It seems it means report the difference between maximum and minimum in the range [x, y] inclusive. So for each query (x, y), output max - min.
Given constraints: n, q up to 100k. So we need an efficient solution: segment tree for range min and max, or Sparse Table (since no updates). But note: input constraints: 0 < n,q < 100000 (strictly less than 100k, but can be 99999). Also 0 < element < 1e9, 0 < x,y < n, and list is 0-indexed. So queries are inclusive.
We can use segment tree with O(log n) per query -> O((n+q) log n) which is fine. Or we can precompute RMQ with sparse table: O(n log n) preprocess and O(1) per query. But careful: we need both min and max. Two sparse tables. That would be O(n log n) memory which with n=100k and log2(100k)~17, 2100k17 = 3.4 million, fine. However segment tree easier.
Edge cases: x may be > y? The problem says "two indices x and y, you have to report the between the maximum number and minimum number whose indices lie within x and y (both inclusive)". It doesn't specify x <= y but typical queries assume x <= y? But it says "leftmost and rightmost indices of the query" in the note: "Next q lines consists of two integer x and y , denoting the leftmost and rightmost indices of the query". So it implies x is leftmost, y is rightmost, so x <= y. But we could still handle if x > y by swapping.
Now reading example: Input: 5 2, array 1 2 3 4 5, queries: 1 2 -> range indices 1 and 2 (values 2,3) -> max-min=1; second: 4 4 -> only 5 -> diff 0. So output 1 and 0.
So straightforward.
We need to output answer for each query on new line.
Implementation in C++? Usually competitive programming solutions in C++.
But the user didn't specify language; we are to produce a solution. As ChatGPT, I should produce a solution in a typical language (C++ or Python). Given constraints, Python might be okay if using efficient structures like segment tree with O(log n) but maybe slower but still within limits if optimized (n=100k, q=100k -> 200k log2(100k) ~ 200k*17=3.4M operations, which is fine in Python). However Python's overhead for segment tree can be okay. But many platforms have time limit; using Python can be borderline but often acceptable if using built-in functions like for range min and max? But we need to answer many queries; scanning each range is O(length) which is too slow. So we need a data structure.
We can use Python with segment tree, but I'm comfortable with C++. Let's produce a C++ solution.
Plan: read n, q. Read array of size n. Build segment tree storing pairs (min, max) for each segment. Or we can have two separate segment trees but that's wasteful. One segment tree node holds min and max.
Segment tree implementation: build using recursion or iterative (like using size = next power of two). Use iterative segment tree (Fenwick? Fenwick can't directly get min and max easily). Segment tree iterative: store arrays minv and maxv of size 2N. Build: for i in 0..n-1: minv[N+i] = maxv[N+i] = arr[i]; for others maybe large. Then for i = N-1 down to 1: minv[i] = min(minv[2i], minv[2i+1]); maxv[i] = max(maxv[2i], maxv[2*i+1]).
Query range [l, r] inclusive: l += N; r += N; initialize min_res = INF, max_res = -INF. while l <= r: if (l%2==1) min_res = min(min_res, minv[l]), max_res = max(max_res, maxv[l]), l++; if (r%2==0) min_res = min(min_res, minv[r]), max_res = max(max_res, maxv[r]), r--; l/=2; r/=2. Then answer = max_res - min_res.
That works.
We need to be careful about 0-indexing. Input indices are given as 0-indexed. So we use directly.
Edge: (0 < x,y < n) says they are positive? Actually they said 0 < x,y < n, meaning both indices between 0 and n-1 inclusive? Actually "0 < x,y < n" means x and y are both less than n and greater than 0. But they could be 0? The condition says 0 < x,y < n; but the example uses 1 and 2, and 4 and 4. So maybe they intend that x and y are inclusive and 0 <= x,y < n. Probably it's a typo. We'll assume they are within [0, n-1]. And x <= y? But we can handle.
We'll produce code accordingly.
Now think about memory: N = 1 << ceil_pow2(n). For n up to 100k, next power is 131072, so arrays size 2*131072 = 262144 for each, times 2 => fine.
Now complexity: O(n + q log n) ~ fine.
Alternatively, we could use Sparse Table: precompute logs, then for each query compute min and max via two queries each: int k = log2(r-l+1); min = min(st_min[l][k], st_min[r-2^k+1][k]); similarly max. That's O(1) per query. But building O(n log n). Both fine.
I'll implement segment tree for simplicity.
Now produce final answer with code and explanation.
But as ChatGPT, the user might want a solution explanation and code. The question: "One day maths teacher gave the following task...", it's a typical programming problem. I will output the solution in a clear manner.
Let's write the answer.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9+5;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
vector<int> arr(n);
for (int i = 0; i < n; ++i) cin >> arr[i];
// build segment tree
int N = 1;
while (N < n) N <<= 1;
vector<int> minv(2*N, INF), maxv(2*N, -INF);
for (int i = 0; i < n; ++i) {
minv[N+i] = arr[i];
maxv[N+i] = arr[i];
}
for (int i = N-1; i >= 1; --i) {
minv[i] = min(minv[2*i], minv[2*i+1]);
maxv[i] = max(maxv[2*i], maxv[2*i+1]);
}
// process queries
while (q--) {
int x, y;
cin >> x >> y;
if (x > y) swap(x, y); // just in case
int l = x + N;
int r = y + N;
int mn = INF, mx = -INF;
while (l <= r) {
if (l & 1) {
mn = min(mn, minv[l]);
mx = max(mx, maxv[l]);
l++;
}
if (!(r & 1)) {
mn = min(mn, minv[r]);
mx = max(mx, maxv[r]);
r--;
}
l >>= 1;
r >>= 1;
}
cout << (mx - mn) << '\n';
}
return 0;
}
But note: INF should be larger than max element (1e9). So 1e9+5 is okay.
Potential issue: When using vector for minv and maxv, minv's initial INF must be larger than any possible value. Since elements < 1e9, INF=1e9+5 works. But we might use INT_MAX? But that's safe.
Alternatively, we could use long long if needed, but difference fits within int? max element up to 1e9, difference could be up to 1e9, fits in int (2^31-1 ~2.1e9). So int is fine.
Edge: if query range is empty? Not possible.
Now produce final answer.
But we also might note that we could use Sparse Table for O(1) queries, but segment tree is fine.
Now finalize.
I'll provide explanation and code. We need to answer multiple range queries on a static array: for each interval [x, y] (0-indexed) we must output the difference between the maximum and minimum element in that interval.
Constraints: n, q ≤ 100 000, so we need an efficient data structure. A segment tree storing both minimum and maximum for each segment allows each query to be answered in O(log n) time, which is fast enough.
Algorithm
- Read
n, q and the array a[0..n-1].
- Build a segment tree using an iterative (bottom‑up) method:
- Let
N be the smallest power of two ≥ n.
- Allocate two arrays
minv[2*N] and maxv[2*N].
- Fill leaves: for
i = 0..n-1 set minv[N+i] = maxv[N+i] = a[i]; the remaining leaves stay at neutral values (INF for min, -INF for max).
- Compute internal nodes: for
i = N-1 .. 1:
minv[i] = min(minv[2*i], minv[2*i+1])
maxv[i] = max(maxv[2*i], maxv[2*i+1]).
- For each query
x, y (ensuring x ≤ y):
- Set
l = x + N, r = y + N.
- Initialise
mn = INF, mx = -INF.
- While
l ≤ r:
- If
l is odd, update mn and mx from node l and increment l.
- If
r is even, update mn and mx from node r and decrement r.
- Shift both indices:
l /= 2, r /= 2.
- Output
mx - mn.
Complexity
- Building the tree: O(n)
- Each query: O(log n)
- Total: O(n + q log n), well within limits for n,q ≤ 100 000.
C++ Code
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5; // larger than any array element
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
// Build segment tree (iterative)
int N = 1;
while (N < n)
N <<= 1;
vector<int> minv(2 * N, INF);
vector<int> maxv(2 * N, -INF);
for (int i = 0; i < n; ++i) {
minv[N + i] = a[i];
maxv[N + i] = a[i];
}
for (int i = N - 1; i >= 1; --i) {
minv[i] = min(minv[2 * i], minv[2 * i + 1]);
maxv[i] = max(maxv[2 * i], maxv[2 * i + 1]);
}
// Process queries
while (q--) {
int x, y;
cin >> x >> y;
if (x > y) swap(x, y); // ensure x ≤ y
int l = x + N, r = y + N;
int mn = INF, mx = -INF;
while (l <= r) {
if (l & 1) {
mn = min(mn, minv[l]);
mx = max(mx, maxv[l]);
++l;
}
if (!(r & 1)) {
mn = min(mn, minv[r]);
mx = max(mx, maxv[r]);
--r;
}
l >>= 1;
r >>= 1;
}
cout << (mx - mn) << '\n';
}
return 0;
}