#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#pragma GCC optimize("O3,unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2,fma,mmx")
#include "peru.h"
using namespace std;
#define int long long
using namespace std;
int mod=1000000007;
int n, k, a[2500005], dp[2500005], ax[2500005];
int ll=1, rr=0;
int smh[2500005];
const int N = 2500005;
int t[2 * N];
void build()
{
for (int i = n - 1; i > 0; --i) t[i] = 100000000000000000ll;
}
void modify(int p, int value)
{
// cout<<"concac "<<p<<" "<<value<<endl;
for (t[p += n] = value; p > 1; p >>= 1) t[p>>1] = min(t[p],t[p^1]);
}
int query(int l, int r)
{
int res = 100000000000000000ll;
for (l += n, r += n; l < r; l >>= 1, r >>= 1)
{
if (l&1) res = min(res, t[l++]);
if (r&1) res = min(res, t[--r]);
}
return res;
}
void addto(int pos)
{
while(rr>=ll&&a[pos]>a[smh[rr]])
{
ax[smh[rr]]=100000000000000000ll;
modify(smh[rr], ax[smh[rr]]);
rr--;
}
if(rr>=ll)
{
ax[pos]=a[pos]+dp[smh[rr]];
modify(pos, ax[pos]);
// lol.push({-ax[pos], pos});
}
else
{
ax[pos]=a[pos]+dp[max(0ll, pos-k)];
modify(pos, ax[pos]);
// lol.push({-ax[pos], pos});
}
rr++; smh[rr]=pos;
if(smh[ll]==pos-k)
{
modify(smh[ll], 100000000000000000ll);
ll++;
}
else
{
ax[smh[ll]]=a[smh[ll]]+dp[max(0ll, pos-k)];
modify(smh[ll], ax[smh[ll]]);
// lol.push({-ax[smh[l]], smh[l]});
}
}
signed solve(signed nn, signed kk, signed* bucac)
{
n=nn, k=kk;
build();
for(int i=1; i<=n; i++)
{
a[i]=bucac[i-1];
}
for(int i=1; i<=n; i++)
{
addto(i);
dp[i]=query(max(1ll, i-k+1), i+1);
// cout<<i<<" cac"<<endl;
// for(int j=max(1ll, i-k); j<=i; j++)
// {
// cout<<ax[j]<<" ";
// }
// cout<<endl<<dp[i]<<endl;
}
int dhs=1, ans=0;
for(int i=n; i>=1; i--)
{
ans+=(dp[i]%mod*dhs)%mod;
ans%=mod;
dhs=(dhs*23)%mod;
}
return ans;
}
// vector<int> aa={3, 2, 9, 8, 7, 11, 3, 4};
// signed main()
// {
// // n=100;
// // build();
// // modify(2, 5);
// // modify(3, 7);
// // modify(4, 9);
// // modify(2, 7);
// // cout<<query(2, 5);
// // return 0;
// cout<<solve(8, 3, aa);
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 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 |
468 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 |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
15 |
Correct |
80 ms |
17732 KB |
Output is correct |
16 |
Correct |
71 ms |
17956 KB |
Output is correct |
17 |
Correct |
73 ms |
18560 KB |
Output is correct |
18 |
Correct |
74 ms |
17648 KB |
Output is correct |
19 |
Correct |
75 ms |
17588 KB |
Output is correct |
20 |
Correct |
75 ms |
17648 KB |
Output is correct |
21 |
Correct |
71 ms |
18224 KB |
Output is correct |
22 |
Correct |
69 ms |
18676 KB |
Output is correct |
23 |
Correct |
68 ms |
18724 KB |
Output is correct |
24 |
Correct |
65 ms |
19532 KB |
Output is correct |
25 |
Correct |
65 ms |
19564 KB |
Output is correct |
26 |
Correct |
77 ms |
18524 KB |
Output is correct |
27 |
Correct |
72 ms |
18196 KB |
Output is correct |
28 |
Correct |
68 ms |
18228 KB |
Output is correct |
29 |
Correct |
72 ms |
18392 KB |
Output is correct |
30 |
Correct |
66 ms |
18556 KB |
Output is correct |
31 |
Correct |
66 ms |
19236 KB |
Output is correct |
32 |
Correct |
68 ms |
18240 KB |
Output is correct |
33 |
Correct |
61 ms |
19528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
17732 KB |
Output is correct |
2 |
Correct |
71 ms |
17956 KB |
Output is correct |
3 |
Correct |
73 ms |
18560 KB |
Output is correct |
4 |
Correct |
74 ms |
17648 KB |
Output is correct |
5 |
Correct |
75 ms |
17588 KB |
Output is correct |
6 |
Correct |
75 ms |
17648 KB |
Output is correct |
7 |
Correct |
71 ms |
18224 KB |
Output is correct |
8 |
Correct |
69 ms |
18676 KB |
Output is correct |
9 |
Correct |
68 ms |
18724 KB |
Output is correct |
10 |
Correct |
65 ms |
19532 KB |
Output is correct |
11 |
Correct |
65 ms |
19564 KB |
Output is correct |
12 |
Correct |
77 ms |
18524 KB |
Output is correct |
13 |
Correct |
72 ms |
18196 KB |
Output is correct |
14 |
Correct |
68 ms |
18228 KB |
Output is correct |
15 |
Correct |
72 ms |
18392 KB |
Output is correct |
16 |
Correct |
66 ms |
18556 KB |
Output is correct |
17 |
Correct |
66 ms |
19236 KB |
Output is correct |
18 |
Correct |
68 ms |
18240 KB |
Output is correct |
19 |
Correct |
61 ms |
19528 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 |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
34 |
Correct |
452 ms |
112016 KB |
Output is correct |
35 |
Correct |
546 ms |
112780 KB |
Output is correct |
36 |
Correct |
429 ms |
112912 KB |
Output is correct |
37 |
Correct |
447 ms |
115352 KB |
Output is correct |
38 |
Correct |
460 ms |
113936 KB |
Output is correct |
39 |
Correct |
476 ms |
113824 KB |
Output is correct |
40 |
Correct |
467 ms |
117372 KB |
Output is correct |
41 |
Correct |
439 ms |
118308 KB |
Output is correct |
42 |
Correct |
454 ms |
115588 KB |
Output is correct |
43 |
Correct |
179 ms |
47280 KB |
Output is correct |
44 |
Correct |
303 ms |
68524 KB |
Output is correct |
45 |
Correct |
277 ms |
68592 KB |
Output is correct |
46 |
Correct |
295 ms |
68632 KB |
Output is correct |
47 |
Correct |
460 ms |
113764 KB |
Output is correct |
48 |
Correct |
442 ms |
114700 KB |
Output is correct |
49 |
Correct |
461 ms |
113576 KB |
Output is correct |
50 |
Correct |
459 ms |
112900 KB |
Output is correct |
51 |
Correct |
470 ms |
113328 KB |
Output is correct |
52 |
Correct |
497 ms |
113368 KB |
Output is correct |
53 |
Correct |
489 ms |
113032 KB |
Output is correct |
54 |
Correct |
471 ms |
119160 KB |
Output is correct |
55 |
Correct |
446 ms |
111628 KB |
Output is correct |
56 |
Correct |
436 ms |
111816 KB |
Output is correct |
57 |
Correct |
441 ms |
111652 KB |
Output is correct |
58 |
Correct |
446 ms |
112724 KB |
Output is correct |
59 |
Correct |
465 ms |
112332 KB |
Output is correct |
60 |
Correct |
472 ms |
112820 KB |
Output is correct |
61 |
Correct |
475 ms |
121364 KB |
Output is correct |
62 |
Correct |
454 ms |
116496 KB |
Output is correct |
63 |
Correct |
440 ms |
118964 KB |
Output is correct |