#include <bits/extc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#define int long long
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define debug(x) do{auto y = x; cout << #x << " = " << y << endl;}while(0)
// #define endl "\n"
#define unordered_map __gnu_pbds::gp_hash_table
using pii = pair<int, int>;
const int inf = 1ll << 60;
const int MOD = 1e9 + 7;
int fastlog2(int x) {
return 63 - __builtin_clzll(x);
}
struct sparsetable {
vector<vector<int>> t;
sparsetable() {}
sparsetable(vector<int>& a) {
int n = a.size();
t.resize(fastlog2(n) + 1, vector<int>(n));
t[0] = a;
for(int i = 1; i <= fastlog2(n); i++) {
for(int j = 0; j + (1 << i) <= n; j++) {
t[i][j] = max(t[i - 1][j], t[i - 1][j + (1 << (i - 1))]);
}
}
}
int query(int ql, int qr) {
int lg = fastlog2(qr - ql);
return max(t[lg][ql], t[lg][qr - (1 << lg)]);
}
};
int n;
sparsetable st;
int eval(pii f, int x) {
if(x >= f.f) return inf - x;
return (n - x) * st.query(x, f.f) + f.s;
}
struct lichao {
int tl, tr;
pii f;
lichao *lc, *rc;
void insert(pii f2) {
int tm = (tl + tr) / 2;
if(eval(f2, tm) < eval(f, tm)) swap(f, f2);
if(tl + 1 == tr) return;
if(eval(f2, tl) <= eval(f, tl)) {
if(!lc) {
lc = new lichao{tl, tm, f2};
} else {
lc->insert(f2);
}
}
if(eval(f2, tr - 1) <= eval(f, tr - 1)) {
if(!rc) {
rc = new lichao{tm, tr, f2};
} else {
rc->insert(f2);
}
}
}
int query(int x) {
int tm = (tl + tr) / 2;
int res = eval(f, x);
if(x < tm && lc) return min(res, lc->query(x));
if(x >= tm && rc) return min(res, rc->query(x));
return res;
}
};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
vector<int> t(n);
for(int i = 0; i < n; i++) {
cin >> t[i];
}
st = sparsetable(t);
vector<int> dp(n);
lichao lct{0, n, {n, 0}};
for(int i = n - 1; i >= 0; i--) {
dp[i] = lct.query(i);
lct.insert({i, dp[i]});
}
cout << dp[0] << endl;
}
# |
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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
560 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
560 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
345 ms |
173156 KB |
Output is correct |
17 |
Correct |
813 ms |
173248 KB |
Output is correct |
18 |
Correct |
410 ms |
173148 KB |
Output is correct |
19 |
Correct |
599 ms |
176604 KB |
Output is correct |
20 |
Correct |
440 ms |
173388 KB |
Output is correct |
21 |
Correct |
590 ms |
174808 KB |
Output is correct |
22 |
Correct |
505 ms |
173576 KB |
Output is correct |
23 |
Correct |
390 ms |
173064 KB |
Output is correct |
24 |
Correct |
824 ms |
177236 KB |
Output is correct |
25 |
Correct |
387 ms |
173400 KB |
Output is correct |
26 |
Correct |
540 ms |
175836 KB |
Output is correct |
27 |
Correct |
592 ms |
174440 KB |
Output is correct |
28 |
Correct |
467 ms |
175160 KB |
Output is correct |
29 |
Correct |
377 ms |
173128 KB |
Output is correct |
30 |
Correct |
384 ms |
173124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
199 ms |
172600 KB |
Output is correct |
2 |
Correct |
197 ms |
172624 KB |
Output is correct |
3 |
Correct |
192 ms |
172584 KB |
Output is correct |
4 |
Correct |
200 ms |
172484 KB |
Output is correct |
5 |
Correct |
191 ms |
172596 KB |
Output is correct |
6 |
Correct |
192 ms |
172580 KB |
Output is correct |
7 |
Correct |
195 ms |
172740 KB |
Output is correct |
8 |
Correct |
194 ms |
172620 KB |
Output is correct |
9 |
Correct |
190 ms |
172516 KB |
Output is correct |
10 |
Correct |
198 ms |
172504 KB |
Output is correct |
# |
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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
560 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
31 |
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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
560 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
345 ms |
173156 KB |
Output is correct |
31 |
Correct |
813 ms |
173248 KB |
Output is correct |
32 |
Correct |
410 ms |
173148 KB |
Output is correct |
33 |
Correct |
599 ms |
176604 KB |
Output is correct |
34 |
Correct |
440 ms |
173388 KB |
Output is correct |
35 |
Correct |
590 ms |
174808 KB |
Output is correct |
36 |
Correct |
505 ms |
173576 KB |
Output is correct |
37 |
Correct |
390 ms |
173064 KB |
Output is correct |
38 |
Correct |
824 ms |
177236 KB |
Output is correct |
39 |
Correct |
387 ms |
173400 KB |
Output is correct |
40 |
Correct |
540 ms |
175836 KB |
Output is correct |
41 |
Correct |
592 ms |
174440 KB |
Output is correct |
42 |
Correct |
467 ms |
175160 KB |
Output is correct |
43 |
Correct |
377 ms |
173128 KB |
Output is correct |
44 |
Correct |
384 ms |
173124 KB |
Output is correct |
45 |
Correct |
199 ms |
172600 KB |
Output is correct |
46 |
Correct |
197 ms |
172624 KB |
Output is correct |
47 |
Correct |
192 ms |
172584 KB |
Output is correct |
48 |
Correct |
200 ms |
172484 KB |
Output is correct |
49 |
Correct |
191 ms |
172596 KB |
Output is correct |
50 |
Correct |
192 ms |
172580 KB |
Output is correct |
51 |
Correct |
195 ms |
172740 KB |
Output is correct |
52 |
Correct |
194 ms |
172620 KB |
Output is correct |
53 |
Correct |
190 ms |
172516 KB |
Output is correct |
54 |
Correct |
198 ms |
172504 KB |
Output is correct |
55 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |