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>
#define ll long long
#define lid id * 2 + 1
#define rid id * 2 + 2
#define pll pair<ll, ll>
#define mp make_pair
#define pb push_back
using namespace std;
struct node_t {
ll tot, oncnt;
};
ll n, d;
ll state;
vector<node_t> st;
vector<pll> cor, bestdpl, bestdpr, bestdplp1, bestdprp1;
vector<ll> pos;
void build(ll id, ll l, ll r) {
if (l == r) {
st[id] = {cor[l].first, 0};
return;
}
ll mid = (l + r) / 2;
build(lid, l, mid);
build(rid, mid + 1, r);
}
ll que(ll id, ll l, ll r, ll take) {
if (take <= 0) return 0;
if (take >= st[id].oncnt) return st[id].tot;
ll mid = (l + r) / 2;
if (take >= st[lid].oncnt) return st[lid].tot + que(rid, mid + 1, r, take - st[lid].oncnt);
return que(lid, l, mid, take);
}
void turn(ll id, ll l, ll r, ll tgt, ll tgtval) {
if (l == r) {
if (st[id].oncnt == state) state = 0;
st[id].oncnt += state;
return;
}
ll mid = (l + r) / 2;
if (tgt <= mid) turn(lid, l, mid, tgt, tgtval);
else turn(rid, mid + 1, r, tgt, tgtval);
st[id].oncnt += state;
st[id].tot += tgtval * state;
}
pll findpos(ll d, ll lo, ll hi) {
ll i, bestpos;
ll best = -1, res;
for (i = lo; i <= hi; i++) {
state = 1;
turn(0, 0, n - 1, pos[i], cor[pos[i]].first);
res = que(0, 0, n - 1, d - i);
if (res > best) {
best = res;
bestpos = i;
}
}
return mp(best, bestpos);
}
void dc(vector<pll>& res, ll l, ll r, ll optl, ll optr) {
if (l > r) {
//for (ll j = optl; j <= optr; j++) turn(0, 0, n - 1, pos[j], cor[pos[j]].first, -1);
return;
}
ll mid = (l + r) / 2, i;
res[mid] = findpos(mid, optl, optr);
for (i = optl; i <= optr; i++) {
state = -1;
turn(0, 0, n - 1, pos[i], cor[pos[i]].first);
}
dc(res, l, mid - 1, optl, res[mid].second);
for (i = optl; i < res[mid].second; i++) {
state = 1;
turn(0, 0, n - 1, pos[i], cor[pos[i]].first);
}
dc(res, mid + 1, r, res[mid].second, optr);
}
ll findMaxAttraction(int N, int start, int D, int att[]) {
ll i;
d = D;
bestdpl.resize(d + 1);
bestdpr.resize(d + 1);
bestdplp1.resize(d + 1);
bestdprp1.resize(d + 1);
n = N;
st.resize(n * 4);
pos.resize(n);
for (i = start; i < n; i++) cor.pb(mp(att[i], i - start));
sort(cor.rbegin(), cor.rend());
n = cor.size();
for (i = 0; i < n; i++) pos[cor[i].second] = i;
build(0, 0, n - 1);
dc(bestdpr, 0, min(d, n * 2 - 1), 0, min(d, n - 1));
if (start < n - 1) {
n = N;
st.clear();
st.resize(n * 4);
pos.clear();
pos.resize(n);
cor.clear();
for (i = start + 1; i < n; i++) cor.pb(mp(att[i], i - start - 1));
sort(cor.rbegin(), cor.rend());
n = cor.size();
for (i = 0; i < n; i++) pos[cor[i].second] = i;
build(0, 0, n - 1);
dc(bestdprp1, 0, min(d, n * 2 - 1), 0, min(d, n - 1));
}
n = N;
st.clear();
st.resize(n * 4);
pos.clear();
pos.resize(n);
cor.clear();
for (i = start; i >= 0; i--) cor.pb(mp(att[i], start - i));
sort(cor.rbegin(), cor.rend());
n = cor.size();
for (i = 0; i < n; i++) pos[cor[i].second] = i;
build(0, 0, n - 1);
dc(bestdpl, 0, min(d, n * 2 - 1), 0, min(d, n - 1));
if (start > 0) {
n = N;
st.clear();
st.resize(n * 4);
pos.clear();
pos.resize(n);
cor.clear();
for (i = start - 1; i >= 0; i--) cor.pb(mp(att[i], start - i - 1));
sort(cor.rbegin(), cor.rend());
n = cor.size();
for (i = 0; i < n; i++) pos[cor[i].second] = i;
build(0, 0, n - 1);
dc(bestdplp1, 0, min(d, n * 2 - 1), 0, min(d, n - 1));
}
n = N;
ll ans = max(start > 0 ? bestdpl[d].first : 0, start < n - 1 ? bestdpr[d].first : 0);
// rL
if (start < n - 1) {
for (i = 0; i < d; i++) ans = max(ans, bestdpr[i].first + bestdplp1[max(d - bestdpr[i].second - i - 1, 0ll)].first);
}
// lR
if (start > 0) {
for (i = 0; i < d; i++) ans = max(ans, bestdpl[i].first + bestdprp1[max(d - bestdpl[i].second - i - 1, 0ll)].first);
}
return ans;
}
Compilation message (stderr)
holiday.cpp: In function 'std::pair<long long int, long long int> findpos(long long int, long long int, long long int)':
holiday.cpp:52:11: warning: 'bestpos' may be used uninitialized in this function [-Wmaybe-uninitialized]
52 | ll i, bestpos;
| ^~~~~~~
# | 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... |