-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1709B Also Try Minecraft.cpp
53 lines (51 loc) · 1.05 KB
/
1709B Also Try Minecraft.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
/*
author : MishkatIT
created : Saturday 2023-07-01-21.00.28
*/
#include<bits/stdc++.h>
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define debug(_) cout << #_ << " is " << _ << '\n';
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
const ll N = 1e5 + 10;
const ll inf = 1e9;
const ll linf = 1e18;
int main()
{
fio;
int n, m;
cin >> n >> m;
vector<int>v(n);
for (auto& i : v)
{
cin >> i;
}
vector<ll> pref(n + 10), suf(n + 10);
for (int i = 2; i <= n; i++)
{
pref[i] = pref[i - 1];
if(v[i - 2] > v[i - 1])
pref[i] += v[i - 2] - v[i - 1];
}
for (int i = n - 2; i >= 0; i--)
{
suf[i] = suf[i + 1];
if(v[i] < v[i + 1])
suf[i] += v[i + 1] - v[i];
}
while(m--)
{
int x, y;
cin >> x >> y;
if(x < y)
{
cout << pref[y] - pref[x] << '\n';
}
else
{
cout << suf[y - 1] - suf[x - 1] << '\n';
}
}
return 0;
}