This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define ii pair <long long, int>
#define fi first
#define se second
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int l, int r) { return l + rnd() % (r - l + 1); }
const int MAXN = 1e5 + 5;
const int MAXK = 205;
const long long INF = 1e18 + 7;
int N, K, a[MAXN], trace[MAXN][MAXK];
vector <int> nen;
long long dp[MAXN];
struct Lichao_Tree{
struct node{
long long a, b;
int trace;
bool used = 0;
long long get(int x) {
if (used == 0) return -INF;
return nen[x - 1] * a + b;
}
} it[4 * MAXN];
void update(int id, int l, int r, int u, int v, node val) {
if (l > v || r < u) return;
if (l >= u && r <= v) {
if (it[id].get(l) >= val.get(l) && it[id].get(r) >= val.get(r))
return;
if (it[id].get(l) <= val.get(l) && it[id].get(r) <= val.get(r))
return void(it[id] = val);
}
int mid = l + r >> 1;
update(id * 2, l, mid, u, v, val);
update(id * 2 + 1, mid + 1, r, u, v, val);
}
ii get(int id, int l, int r, int x) {
ii ans = {it[id].get(x), it[id].trace};
if (l == r) return ans;
int mid = l + r >> 1;
ii tmp;
if (mid >= x) tmp = get(id * 2, l, mid, x);
else tmp = get(id * 2 + 1, mid + 1, r, x);
if (ans.fi < tmp.fi) ans = tmp;
return ans;
}
void update(int l, int r, int prv, long long a, long long b) {
node tmp = {a, b, prv, 1};
update(1, 1, N, l, r, tmp);
}
} T;
void solve(void) {
cin >> N >> K; K++;
FOR(i, 1, N) {
cin >> a[i];
a[i] += a[i - 1];
nen.emplace_back(a[i]);
}
sort(ALL(nen));
FOR(i, 1, N) a[i] = lower_bound(ALL(nen), a[i]) - nen.begin() + 1;
T.update(1, N, 0, 0, 0);
FOR(k, 1, K) {
FOR(i, k, N) {
ii tmp = T.get(1, 1, N, a[i]);
dp[i] = tmp.fi;
trace[i][k] = tmp.se;
}
FOR(i, 1, 4 * N) {
T.it[i].used = 0;
}
FOR(i, k, N) {
T.update(1, N, i, nen[a[i] - 1], dp[i] - 1ll * nen[a[i] - 1] * nen[a[i] - 1]);
}
}
cout << dp[N] << '\n';
for (int x = N, cur = K; cur > 0; x = trace[x][cur], cur--)
if (x != N) cout << x << ' ';
}
signed main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
file("TASK");
//int T; cin >> T; while (T--)
solve();
cerr << "Time elapsed: " << TIME << " s.\n";
return (0 ^ 0);
}
Compilation message (stderr)
sequence.cpp: In member function 'void Lichao_Tree::update(int, int, int, int, int, Lichao_Tree::node)':
sequence.cpp:44:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int mid = l + r >> 1;
| ~~^~~
sequence.cpp: In member function 'std::pair<long long int, int> Lichao_Tree::get(int, int, int, int)':
sequence.cpp:52:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int mid = l + r >> 1;
| ~~^~~
sequence.cpp: In function 'int main()':
sequence.cpp:9:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:100:5: note: in expansion of macro 'file'
100 | file("TASK");
| ^~~~
sequence.cpp:9:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:100:5: note: in expansion of macro 'file'
100 | file("TASK");
| ^~~~
# | 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... |