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>
using namespace std;
#define ll long long
const int mx = 1e5 + 5;
int n, d, st, pos[mx], A[mx]; pair<int, int> S[mx]; ll ans, F[2][2][mx * 3], bit[mx][2];
void upd(int i, int type){
for (int j = pos[i]; j < mx; j += j & (-j))
bit[j][0] += type * A[i], bit[j][1] += type;
}
ll qry(int k){
ll ret = 0;
for (int pw = (1 << 17), pos = 0; pw; pw /= 2)
if (pos + pw < mx and bit[pos + pw][1] <= k){
pos += pw;
ret += bit[pos][0];
k -= bit[pos][1];
}
return ret;
}
void dnq(int l, int r, int distL, int distR, bool dir, bool rep){
if (l > r or distL > distR) return;
for (int i = distL; i <= distR; i++) upd(i, -1);
int midD = (l + r) / 2, stopMid = -1;
for (int stop = distL; stop <= distR; stop++){
int visit = midD - (stop - st) * (rep ? 2 : 1);
upd(stop, 1);
if (visit < 0) continue;
ll val = qry(visit);
if (val >= F[dir][rep][midD]){
F[dir][rep][midD] = val;
stopMid = stop;
}
}
for (int i = stopMid + 1; i <= distR; i++) upd(i, -1);
dnq(l, midD - 1, distL, stopMid, dir, rep);
for (int i = stopMid + 1; i <= distR; i++) upd(i, 1);
dnq(midD + 1, r, stopMid, distR, dir, rep);
}
void work(int start, int tmpA[], bool dir, bool rep){
for (int i = 0; i < n; i++){
A[i] = tmpA[i];
S[i] = {tmpA[i], i};
}
sort(S, S + n, greater<pair<int, int>>());
for (int i = 0; i < n; i++) pos[S[i].second] = i + 1; //BIT has to be 1 indexed
memset(bit, 0, sizeof(bit));
for (int i = start; i < n; i++) upd(i, 1);
st = start;
dnq(0, d, start, n - 1, dir, rep);
}
ll findMaxAttraction(int tmpN, int start, int tmpD, int tmpA[]){
n = tmpN; d = tmpD;
work(start, tmpA, 0, 0);
work(start + 1, tmpA, 0, 1);
reverse(tmpA, tmpA + n);
work(n - start - 1, tmpA, 1, 0);
work(n - start, tmpA, 1, 1);
for (int i = 0; i <= d - 2; i++)
ans = max({ans, F[0][1][i] + F[1][0][d - i - 2], F[1][1][i] + F[0][0][d - i - 2]});
return max({ans, F[0][0][d], F[1][0][d]});
}
# | 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... |