이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
struct Line {
mutable ll m, p;
mutable int k, tr;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
const ll inf = LLONG_MAX;
struct LineContainer : vector<Line> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(int k, ll m, int tr) {
auto z = insert(end(), {m, 0, k, tr}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
int id=0;
pair<ll, int> query(ll x) {
if (empty()) return {-inf, 0};
while ((begin()+id)->p<x) ++id;
return {(begin()+id)->k * x + (begin()+id)->m, (begin()+id)->tr};
}
void merge(const LineContainer &tl, const LineContainer &tr){
for (auto &i:tl) add(i.k, i.m, i.tr);
for (auto &i:tr) add(i.k, i.m, i.tr);
}
};
struct FenwickTree{
int n;
vector<LineContainer> t;
void init(int _n){
n=_n;
t.assign(n+1, LineContainer());
}
void clear(){
for (int i=0; i<=n; ++i) t[i].clear(), t[i].shrink_to_fit(), t[i].id=0;
}
void update(int pos, int k, ll m, int tr){
for (int i=pos; i<=n; i+=i&(-i)) t[i].add(k, m, tr);
}
pair<ll, int> query(int pos, int x){
pair<ll, int> ans={-inf, 0};
for (int i=pos; i; i-=i&(-i)) ans=max(ans, t[i].query(x));
return ans;
}
} bit[2];
int n, k;
int a[100001], pf[100001];
int tr[100001][201];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k; ++k;
for (int i=1; i<=n; ++i) cin >> a[i];
partial_sum(a, a+n+1, pf);
bit[0].init(n);
bit[1].init(n);
bit[0].update(1, 0, 0, 0);
ll res=0;
for (int j=1; j<=k; ++j){
bit[j&1].clear();
for (int i=1; i<=n; ++i){
auto tmp=bit[(j&1)^1].query(i, pf[i]);
if (tmp.first==-inf) continue;
if (i==n && j==k) res=tmp.first;
tr[i][j]=tmp.second;
bit[j&1].update(i+1, pf[i], tmp.first-1ll*pf[i]*pf[i], i);
}
}
cout << res << '\n';
vector<int> ans;
while (n){
if (tr[n][k]) ans.push_back(tr[n][k]);
n=tr[n][k];
--k;
}
reverse(ans.begin(), ans.end());
for (int i:ans) cout << i << ' ';
return 0;
}
# | 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... |