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<algorithm>
#include<iostream>
#include<vector>
#include<set>
using namespace std;
using ll = long long;
const int N = 1e5 + 123;
int n;
pair<int,int>pp[N];
int cnt[N << 2];
ll sum[N << 2];
void upd(int pos, int type, int l = 0, int r = n, int v = 1) {
if (r - l == 1) {
cnt[v] += type, sum[v] += type * pp[l].first;
return;
}
int m = (l + r) >> 1;
if (pos < m) upd(pos, type, l, m, v << 1);
else upd(pos, type, m, r, v<<1|1);
cnt[v] = cnt[v << 1] + cnt[v<<1|1];
sum[v] = sum[v << 1] + sum[v<<1|1];
}
ll get(int y, int l = 0, int r = n, int v = 1) {
if (r - l == 1) return y ? sum[v] : 0;
int m = (l + r) >> 1;
if (cnt[v<<1|1] >= y) return get(y, m, r, v<<1|1);
else return get(y - cnt[v<<1|1], l, m, v << 1) + sum[v<<1|1];
}
int pos[N];
ll ans;
int start, d;
int L = 1, R = 0;
void moveL(int s) {
while (L < s) {
upd(pos[L++], -1);
}
while (L > s) {
upd(pos[--L], +1);
}
}
void moveR(int s) {
while (R < s) {
upd(pos[++R], +1);
}
while (R > s) {
upd(pos[R--], -1);
}
}
void solve(int l, int r, int opl, int opr) {
if (l > r) return;
int m = (l + r) >> 1;
int opm = -1;
moveL(m);
int x = d - (start - m);
ll cur = 0;
opm = opl;
for (int i = opl ; i <= opr ; ++ i) {
moveR(i);
int y = x - (i - m);
if (y > 0) {
ll temp = get(y);
if (temp > cur) {
opm = i;
cur = temp;
}
}
}
ans = max(ans, cur);
solve(l, m - 1, opl, opm);
solve(m + 1, r, opm, opr);
}
ll findMaxAttraction(int n_, int start_, int d_, int attraction[]) {
n = n_, start = start_, d = d_;
for (int i = 0 ; i < n ; ++ i) {
pp[i] = {attraction[i], i};
}
sort(pp, pp + n);
for (int i = 0 ; i < n ; ++ i) {
pos[pp[i].second] = i;
}
for (int _ = 0 ; _ < 2 ; ++ _) {
solve(0, start, start, n - 1);
moveL(1);
moveR(0);
for (int i = 0 ; i < n ; ++ i) {
pp[i].second = n - pp[i].second - 1;
pos[pp[i].second] = i;
}
reverse(attraction, attraction + n);
start = n - start - 1;
}
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... |