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;
};
int n, d;
ll rt;
vector<node_t> st;
vector<pll> cor, bestdpl, bestdpr, bestdplp1, bestdprp1;
vector<int> pos;
void build(int id, int l, int r) {
if (l == r) {
st[id] = {cor[l].first, 0};
return;
}
int mid = (l + r) / 2;
build(lid, l, mid);
build(rid, mid + 1, r);
}
ll que(int id, int l, int r, int take) {
if (take == 0) return 0;
if (take >= st[id].oncnt) return st[id].tot;
int 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(int id, int l, int r, int tgt, ll tgtval, ll& state) {
if (l == r) {
if (st[id].oncnt) state = 0;
st[id].oncnt += state;
return;
}
int mid = (l + r) / 2;
if (tgt <= mid) turn(lid, l, mid, tgt, tgtval, state);
else turn(rid, mid + 1, r, tgt, tgtval, state);
st[id].oncnt += state;
st[id].tot += tgtval * state;
}
pll findpos(int d, int lo, int hi) {
int i, bestpos;
ll best = -1, res;
for (i = lo; i <= hi; i++) {
rt = 1;
turn(0, 0, n - 1, pos[i], cor[pos[i]].first, rt);
res = que(0, 0, n - 1, d - i);
if (res > best) {
best = res;
bestpos = i;
}
}
return mp(best, bestpos);
}
void dc(vector<pll>& res, int l, int r, int optl, int optr) {
if (l > r) {
//for (int j = optl; j <= optr; j++) turn(0, 0, n - 1, pos[j], cor[pos[j]].first, -1);
return;
}
int mid = (l + r) / 2, i;
res[mid] = findpos(mid, optl, optr);
rt = -1;
for (i = optl; i <= optr; i++) turn(0, 0, n - 1, pos[i], cor[pos[i]].first, rt);
dc(res, l, mid - 1, optl, res[mid].second);
rt = 1;
for (i = optl; i < res[mid].second; i++) turn(0, 0, n - 1, pos[i], cor[pos[i]].first, rt);
dc(res, mid + 1, r, res[mid].second, optr);
}
ll findMaxAttraction(int N, int start, int D, int att[]) {
int 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, d, 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, d, 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, d, 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, d, 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;
}
# | 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... |