-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClosest-Cities.cpp
84 lines (76 loc) · 1.69 KB
/
Closest-Cities.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
// Function to calculate the left array
void Left(ll a[], ll left[], map<int, int>& m, int n) {
left[0] = 0;
for (int i = 1; i < n; i++) {
if (m[i] == -1)
left[i] = 1 + left[i - 1];
else
left[i] = left[i - 1] + abs(a[i] - a[i - 1]);
}
}
// Function to calculate the right array
void Right(ll a[], ll right[], map<int, int>& m, int n) {
right[n - 1] = 0;
for (int i = n - 2; i >= 0; i--) {
if (m[i] == 1) {
right[i] = 1 + right[i + 1];
}
else {
right[i] = right[i + 1] + abs(a[i] - a[i + 1]);
}
}
}
// Function to solve the problem
void solve() {
ll n;
cin >> n;
ll a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
ll left[n], right[n];
map<int, int> m;
m[0] = 1;
m[n - 1] = -1;
for (int i = 1; i < n - 1; i++) {
ll leftDiff = abs(a[i] - a[i - 1]);
ll rightDiff = abs(a[i] - a[i + 1]);
if (leftDiff < rightDiff) {
m[i] = -1;
}
else {
m[i] = 1;
}
}
Left(a, left, m, n);
Right(a, right, m, n);
ll q;
cin >> q;
while (q--) {
ll x, y;
cin >> x >> y;
if (x == y) {
cout << 0 << endl;
}
else {
if (y > x) {
cout << right[x - 1] - right[y - 1] << endl;
}
else {
cout << left[x - 1] - left[y - 1] << endl;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
while (t--) {
solve();
}
return 0;
}