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"holiday.h"
#include <bits/stdc++.h>
#define fr first
#define sc second
#define mk make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
using namespace std;
const int N = 1e5 + 3;
long long d1[2][(int)3e5], d2[2][(int)3e5];
struct tree{
int cnt, l, r;
long long sum;
}
t[N * 15];
int n, a[N], sz, st, root[N];
vector <int> vec;
void update (int ov, int nv, int pos, int val, int tl = 0, int tr = n) {
if (tl == tr) {
t[nv].cnt = t[ov].cnt + 1;
t[nv].sum = t[ov].sum + val;
} else {
int tm = (tl + tr) >> 1;
if (pos <= tm) {
t[nv].r = t[ov].r;
t[nv].l = ++sz;
update( t[ov].l, t[nv].l, pos, val, tl, tm );
} else {
t[nv].l = t[ov].l;
t[nv].r = ++sz;
update( t[ov].r, t[nv].r, pos, val, tm + 1, tr );
}
t[nv].cnt = t[ t[nv].l ].cnt + t[ t[nv].r ].cnt;
t[nv].sum = t[ t[nv].l ].sum + t[ t[nv].r ].sum;
}
}
long long get (int ov, int nv, int k, int tl = 0, int tr = n) {
if (tl == tr) {
return min((t[nv].sum - t[ov].sum), k * 1ll * vec[tl]);
}
int tm = (tl + tr) >> 1;
if (t[t[nv].r].cnt - t[t[ov].r].cnt >= k) {
return get(t[ov].r, t[nv].r, k, tm + 1, tr);
}
return get(t[ov].l, t[nv].l, k - (t[t[nv].r].cnt - t[t[ov].r].cnt), tl, tm ) + t[t[nv].r].sum - t[t[ov].r].sum;
}
void solve1 (int opl, int opr, int dl, int dr) {
if (dl > dr) return;
int dm = (dl + dr) >> 1, opt;
long long res = 0, val;
for (int i = opl; i <= opr; i++) {
if ( dm - (i - st) <= 0 ) break;
val = get(root[st - 1], root[i], dm - (i - st));
if ( val > res ) {
res = val;
opt = i;
}
}
d1[0][dm] = res;
d1[1][dm + (opt - st)] = res;
solve1(opl, opt, dl, dm - 1);
solve1(opt, opr, dm + 1, dr);
}
void solve2 (int opl, int opr, int dl, int dr) {
if (dl > dr || opl > opr) return;
int dm = (dl + dr) >> 1, opt;
long long res = 0, val;
for (int i = opr; i >= opl; i--) {
if ( dm - (st - i) <= 0 ) break;
val = get(root[i - 1], root[st - 1], dm - (st - i));
if ( val > res ) {
res = val;
opt = i;
}
}
d2[0][dm] = res;
d2[1][dm + (st - opt)] = res;
solve2(opl, opt, dm + 1, dr);
solve2(opt, opr, dl, dm - 1);
}
long long findMaxAttraction(int A, int start, int D, int at[]) { start++;
n = A;
st = start;
if (start != 1 || D > 3e5) {
while (1);
}
for (int i = 0; i < n; i++) {
vec.pb(at[i]);
}
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
for (int i = 0; i < n; i++) {
a[i + 1] = lower_bound(all(vec), at[i]) - vec.begin();
root[i + 1] = ++sz;
update( root[i], root[i + 1], a[i + 1], vec[ a[i + 1] ] );
}
solve1(start, n, 1, min(D, 2 * (n - start) + 1) );
long long ans = 0;
for (int i = 0; i <= D; i++) {
ans = max(ans, d1[0][i]);
}
return ans;
}
Compilation message (stderr)
holiday.cpp: In function 'void solve1(int, int, int, int)':
holiday.cpp:71:18: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
d1[1][dm + (opt - st)] = res;
~~~~~^~~~~
holiday.cpp: In function 'void solve2(int, int, int, int)':
holiday.cpp:89:17: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
d2[1][dm + (st - opt)] = res;
~~~~^~~~~~
# | 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... |