-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncreasing Array Queries.cpp
More file actions
executable file
·113 lines (109 loc) · 2.27 KB
/
Copy pathIncreasing Array Queries.cpp
File metadata and controls
executable file
·113 lines (109 loc) · 2.27 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int M = 1 << 19;
pair<LL, LL> t[2 * M + 5];
LL d[M];
void update(int a, int b, LL v) {
a += M;
b += M;
t[a].first += v;
if (a != b) t[b].first += v;
int pow = 1;
while (a / 2 != b / 2) {
if ((a & 1) == 0) {
t[a + 1].second += v;
}
if ((b & 1) == 1) {
t[b - 1].second += v;
}
a /= 2;
b /= 2;
t[a].first = t[a * 2].first + t[a * 2 + 1].first + t[a * 2].second * pow + t[a * 2 + 1].second * pow;
t[b].first = t[b * 2].first + t[b * 2 + 1].first + t[b * 2].second * pow + t[b * 2 + 1].second * pow;
pow *= 2;
}
while (a != 1) {
a /= 2;
t[a].first = t[a * 2].first + t[a * 2 + 1].first + t[a * 2].second * pow + t[a * 2 + 1].second * pow;
pow *= 2;
}
}
LL query(int a, int b) {
a += M;
b += M;
LL res = t[a].first;
if (a != b) res += t[b].first;
int pow = 1;
int diff = b - a + 1;
int counta = 1;
int countb = 1;
while (a / 2 != b / 2) {
res += t[a].second * counta + t[b].second * countb;
if ((a & 1) == 0) {
res += t[a + 1].first + t[a + 1].second * pow;
counta += pow;
}
if ((b & 1) == 1) {
res += t[b - 1].first + t[b - 1].second * pow;
countb += pow;
}
a /= 2;
b /= 2;
pow *= 2;
}
res += t[a].second * counta;
if (a != b) res += t[b].second * countb;
a /= 2;
while (a != 0) {
res += t[a].second * diff;
a /= 2;
}
return res;
}
int main() {
int n, q;
ios_base::sync_with_stdio(false);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> d[i];
}
vector<tuple<int, int, int>> queries;
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
queries.emplace_back(a, b, i);
}
sort(queries.begin(), queries.end());
reverse(queries.begin(), queries.end());
vector<LL> res(q);
vector<pair<int, int>> ranges;
ranges.emplace_back(n, n);
int id = n;
for (auto [a, b, i] : queries) {
while (a < id) {
int j = id - 1;
if (d[j] <= d[id]) {
ranges.emplace_back(j, j);
} else {
auto tmp = ranges.back();
while (!ranges.empty()) {
auto [A, B] = ranges.back();
if (d[j] <= d[A]) {
break;
}
ranges.pop_back();
update(A, B, d[j] - d[A]);
tmp = make_pair(j, B);
}
ranges.push_back(tmp);
}
id--;
}
res[i] = query(a, b);
}
for (auto x : res) {
cout << x << "\n";
}
return 0;
}