#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int A[1000009], L[1000009], R[1000009], T[2020202];
void init(int idx, int s, int e) {
if(s == e) {
T[idx] = A[s];
return;
}
int m = s+e >> 1;
init(idx*2, s, m);
init(idx*2+1, m+1, e);
T[idx] = min(T[idx*2], T[idx*2+1]);
}
void upd(int idx, int s, int e, int p, int v) {
if(p < s || e < p) return;
if(s == e) {
T[idx] = v;
return;
}
int m = s+e >> 1;
upd(idx*2, s, m, p, v);
upd(idx*2+1, m+1, e, p, v);
T[idx] = min(T[idx*2], T[idx*2+1]);
}
int get(int idx, int s, int e, int l, int r) {
if(r < s || e < l) return INF;
if(l <= s && e <= r) return T[idx];
int m = s+e >> 1;
return min(get(idx*2, s, m, l, r), get(idx*2+1, m+1, e, l, r));
}
long long f(int l, int r) {
return 1LL*r*(r+1)/2 - 1LL*l*(l-1)/2;
}
int main() {
vector<int> I, X;
int N; scanf("%d", &N);
for(int i=1; i<=N; i++) {
scanf("%d", &A[i]);
X.push_back(A[i]);
I.push_back(i);
}
init(1, 1, N);
for(int i=1; i<=N; i++) L[i] = max(L[i-1], A[i]);
for(int i=N; i>=1; i--) R[i] = max(R[i+1], A[i]);
sort(I.begin(), I.end(), [&](int p, int q) { return A[p] < A[q]; });
sort(X.begin(), X.end()); X.resize(unique(X.begin(), X.end()) - X.begin());
set<int> U;
int i = 0, j = 0;
long long ans = 0;
for(auto& h: X) if(++j < X.size()) {
while(i < N && A[I[i]] <= h) {
upd(1, 1, N, I[i], INF);
if(L[I[i]] != A[I[i]] && R[I[i]] != A[I[i]]) U.insert(I[i]);
++i;
}
while(U.size() && L[*U.begin()] == h) U.erase(U.begin());
while(U.size() && R[*U.rbegin()] == h) U.erase(prev(U.end()));
int g = X[j] - h;
if(U.size()) {
int l = get(1, 1, N, 1, *U.begin()), r = get(1, 1, N, *U.rbegin(), N);
int v = min(get(1, 1, N, *U.begin(), N), get(1, 1, N, 1, *U.rbegin()));
if(U.size() == 1) ans += 1LL * (l + r) * g + f(h, X[j] - 1);
else ans += 1LL * (v + l + r) * g + 1LL * U.size() * f(h, X[j] - 1) + 1LL * (2*U.size() - 3) * f(h+1, X[j]);
}
/*
printf("h: %d, nxt_h: %d, ans: %lld\nU: ", h, X[j], ans);
for(auto& it: U) printf("%d ", it);
puts("");
*/
}
printf("%lld", ans);
return 0;
}
Compilation message
Main.cpp: In function 'void init(int, int, int)':
Main.cpp:12:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
12 | int m = s+e >> 1;
| ~^~
Main.cpp: In function 'void upd(int, int, int, int, int)':
Main.cpp:24:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
24 | int m = s+e >> 1;
| ~^~
Main.cpp: In function 'int get(int, int, int, int, int)':
Main.cpp:33:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int m = s+e >> 1;
| ~^~
Main.cpp: In function 'int main()':
Main.cpp:57:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(auto& h: X) if(++j < X.size()) {
| ~~~~^~~~~~~~~~
Main.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | int N; scanf("%d", &N);
| ~~~~~^~~~~~~~~~
Main.cpp:45:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d", &A[i]);
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
3 ms |
740 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
3 ms |
740 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
3 ms |
740 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
3 ms |
740 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
3 ms |
740 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
3 ms |
740 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
3 ms |
740 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |