#include "peru.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#define mp make_pair
#define ll long long
#define ii pair<int, int>
#define pl pair<ll, ll>
#define f1 first
#define s2 second
const int MAX = 25e5 + 7;
const int mod = 1e9 + 7;
inline int add(ll x, ll y) { return (int)((x + y) % mod); }
inline int mul(ll x, ll y) { return (int)((x * y) % mod); }
struct Item {int mx, l, r; };
ll dp[MAX];
static int dq_l = 1, dq_r = 0;
Item dq[MAX];
int solve(int n, int k, int* v){
priority_queue<ll, vector<ll>, greater<ll>> pq, pending;
dp[0] = v[0];
dq[++dq_r] = {v[0], 0, 0};
pq.push(v[0]);
for (int i = 1; i < n; ++i)
{
//remove the front
if (dq_l <= dq_r && dq[dq_l].l <= i-k)
{
//int mx = dq[dq_l].mx, l = dq[dq_l].mx, r = dq[dq_l].r;
auto [mx, l, r] = dq[dq_l];
dq_l++;
pending.push(mx + (l ? dp[l-1] : 0));
if (r > i-k)
{
dq[--dq_l] = {mx, i-k+1, r};
pq.push(mx + (i-k+1 ? dp[i-k] : 0));
}
}
int cur_l = i, cur_r = i;
while (dq_l <= dq_r && dq[dq_r].mx <= v[i])
{
//int mx = dq[dq_r].mx, l = dq[dq_r].l, r = dq[dq_r].r;
auto [mx, l, r] = dq[dq_r];
dq_r--;
pending.push(mx + (l ? dp[l-1] : 0));
cur_l = l;
}
dq[++dq_r] = {v[i], cur_l, cur_r};
pq.push(v[i] + (cur_l ? dp[cur_l-1] : 0));
while (!pending.empty())
{
if (pq.top() > pending.top())
pending.pop();
else if (pq.top() < pending.top())
break;
else pq.pop(), pending.pop();
}
dp[i] = pq.top();
}
int pwr = 1, ans = 0;
for (int i = n-1; i >= 0; --i)
ans = add(ans, mul(dp[i]%mod, pwr)), pwr = mul(pwr, 23);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
113 ms |
6996 KB |
Output is correct |
16 |
Correct |
111 ms |
7620 KB |
Output is correct |
17 |
Correct |
93 ms |
8336 KB |
Output is correct |
18 |
Correct |
137 ms |
6944 KB |
Output is correct |
19 |
Correct |
151 ms |
6864 KB |
Output is correct |
20 |
Correct |
166 ms |
7056 KB |
Output is correct |
21 |
Correct |
100 ms |
7500 KB |
Output is correct |
22 |
Correct |
102 ms |
8128 KB |
Output is correct |
23 |
Correct |
104 ms |
8148 KB |
Output is correct |
24 |
Correct |
95 ms |
8744 KB |
Output is correct |
25 |
Correct |
109 ms |
8876 KB |
Output is correct |
26 |
Correct |
101 ms |
7296 KB |
Output is correct |
27 |
Correct |
123 ms |
6964 KB |
Output is correct |
28 |
Correct |
113 ms |
6900 KB |
Output is correct |
29 |
Correct |
117 ms |
6668 KB |
Output is correct |
30 |
Correct |
112 ms |
6792 KB |
Output is correct |
31 |
Correct |
103 ms |
7476 KB |
Output is correct |
32 |
Correct |
122 ms |
5948 KB |
Output is correct |
33 |
Correct |
84 ms |
7588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
6996 KB |
Output is correct |
2 |
Correct |
111 ms |
7620 KB |
Output is correct |
3 |
Correct |
93 ms |
8336 KB |
Output is correct |
4 |
Correct |
137 ms |
6944 KB |
Output is correct |
5 |
Correct |
151 ms |
6864 KB |
Output is correct |
6 |
Correct |
166 ms |
7056 KB |
Output is correct |
7 |
Correct |
100 ms |
7500 KB |
Output is correct |
8 |
Correct |
102 ms |
8128 KB |
Output is correct |
9 |
Correct |
104 ms |
8148 KB |
Output is correct |
10 |
Correct |
95 ms |
8744 KB |
Output is correct |
11 |
Correct |
109 ms |
8876 KB |
Output is correct |
12 |
Correct |
101 ms |
7296 KB |
Output is correct |
13 |
Correct |
123 ms |
6964 KB |
Output is correct |
14 |
Correct |
113 ms |
6900 KB |
Output is correct |
15 |
Correct |
117 ms |
6668 KB |
Output is correct |
16 |
Correct |
112 ms |
6792 KB |
Output is correct |
17 |
Correct |
103 ms |
7476 KB |
Output is correct |
18 |
Correct |
122 ms |
5948 KB |
Output is correct |
19 |
Correct |
84 ms |
7588 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Execution timed out |
665 ms |
35580 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |