-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplacesort.cpp
54 lines (37 loc) · 883 Bytes
/
replacesort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll maxn = 1e5 + 8;
int main(int argc, char const *argv[]) {
ll n, q, k;
cin >> n >> q >> k;
ll a[maxn];
for (ll i = 1; i <= n; i++) {
cin >> a[i];
}
// Edge cases on edge of array
a[0] = 0;
a[n + 1] = k + 1;
ll psum[maxn];
ll ssum[maxn];
for (ll i = 1; i <= n; i++) {
psum[i] = a[i + 1] - a[i] - 1;
ssum[i] = a[i] - a[i - 1] - 1;
}
ll tsum[maxn];
tsum[0] = 0;
for (ll i = 1; i <= n; i++) {
tsum[i] = tsum[i - 1] + psum[i] + ssum[i];
}
for (ll i = 0; i < q; i++) {
ll l, r;
cin >> l >> r;
ll tot = tsum[r] - tsum[l - 1];
tot -= ssum[l];
tot -= psum[r];
tot += a[l] - 1;
tot += k - a[r];
cout << tot << endl;
}
return 0;
}