This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define lg long long
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const lg N = 5e5+5;
lg n, d, tim;
lg dp[2][N], p[N];
lg t[N], lst[N], seg[N << 2][3];
vector<lg> o[N];
void doLazy(lg node, lg lr, lg hr)
{
if(!seg[node][0]) return;
seg[node][1] += seg[node][0];
seg[node][2] += seg[node][0];
if(lr != hr)
{
for(int ch = node*2; ch <= node*2+1; ch++)
{
seg[ch][0] += seg[node][0];
}
}
seg[node][0] = 0;
return;
}
void update(lg node, lg lr, lg hr, lg lq, lg hq, lg val)
{
doLazy(node, lr, hr);
if(lq > hr || lr > hq) return;
if(lq <= lr && hr <= hq)
{
seg[node][0] += val;
// cout << "NODE " << node << ' ' << lq << ' ' << seg[9][0] << ' ' << seg[9][1] << '\n';
doLazy(node, lr, hr);
return;
}
lg mid = (lr+hr)/2;
update(node << 1, lr, mid, lq, hq, val);
update(node << 1 | 1, mid+1, hr, lq, hq, val);
seg[node][1] = min(seg[node << 1][1], seg[node << 1 | 1][1]);
seg[node][2] = max(seg[node << 1][2], seg[node << 1 | 1][2]);
return;
}
lg get(lg node, lg lr, lg hr, lg lq, lg hq)
{
doLazy(node, lr, hr);
if(lq > hr || lr > hq) return 0;
if(seg[node][2] < 0) return 0;
if(lq <= lr && hr <= hq && seg[node][1] >= 0) return hr-lr+1;
if(lr == hr)
{
return seg[node][2] >= 0;
}
lg mid = (lr+hr)/2;
lg ans = get(node << 1, lr, mid, lq, hq);
ans += get(node << 1 | 1, mid+1, hr, lq, hq);
return ans;
}
void traverse(lg node, lg lr, lg hr)
{
doLazy(node, lr, hr);
if(lr == hr)
{
cout << "A: " << node << ' ' << lr << ' ' << hr << '\n';
cout << seg[node][0] << ' ' << seg[node][1] << ' ' << seg[node][2] << '\n';
return;
}
lg mid = (lr+hr)/2;
traverse(node << 1, lr, mid);
traverse(node << 1 | 1, mid+1, hr);
return;
}
lg C(lg i, lg j)
{
// lg ans = o[j].size()-(lower_bound(o[j].begin(), o[j].end(), i)-o[j].begin());
// return ans;
update(1, 1, n, 1, n, -i);
lg ans = get(1, 1, n, i, j);
update(1, 1, n, 1, n, i);
return ans;
}
void compute(lg lev, lg l, lg r, lg optl, lg optr)
{
if(l > r) return;
lg mid = (l+r)/2;
array<lg, 2> best = {1000000000000, optl};
for(int i = optl; i <= min(optr, mid-1); i++)
{
best = min(best, {dp[(lev+1)&1][i]+C(i+1, mid), i});
}
dp[lev&1][mid] = best[0];
compute(lev, l, mid-1, optl, best[1]);
compute(lev, mid+1, r, best[1], optr);
return;
}
int main()
{
fastio;
cin >> n >> d >> tim;
for(int i = 1; i <= n; i++)
{
cin >> t[i];
}
for(int i = 1; i <= n; i++)
{
for(int j = i; j >= 1; j--)
{
if(t[j]+i-j <= tim)
{
lst[i] = j;
break;
}
}
update(1, 1, n, i, i, lst[i]);
// o[i].push_back(lst[i]);
// for(auto it : o[i-1]) o[i].push_back(it);
// sort(o[i].begin(), o[i].end());
}
// for(int i = 1; i <= n; i++)
// {
// // cout << get(1, 1, n, i, i) << ' ';
// for(int j = i; j <= n; j++)
// {
// update(1, 1, n, 1, n, -i);
// if(get(1, 1, n, i, j) != C(i, j))
// {
// cout << "A\n";
// cout << i << ' ' << j << '\n';
// cout << get(1, 1, n, i, j) << ' ' << C(i, j) << '\n';
// }
// update(1, 1, n, 1, n, i);
// }
// }
d = min(n, d);
t[0] = 1e18;
memset(dp, 0x3f, sizeof(dp));
dp[d&1][0] = 0;
for(int i = 1; i <= n; i++) dp[d&1][i] = C(1, i);
for(int i = d-1; i >= 0; i--)
{
for(int j = 1; j <= n; j++) dp[i&1][j] = 1e15;
compute(i, 1, n, 1, n);
}
cout << dp[0][n] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |