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 type(x) __typeof((x).begin())
#define foreach(it, x) for(type(x) it = (x).begin(); it != (x).end(); it++)
typedef long long ll;
typedef pair < int, int > ii;
const int inf = 1e9 + 333;
const ll linf = 1e18 + inf;
#include"holiday.h"
const int N = 1e5 + 5;
ll ans = 0;
int n, start, d;
int a[N];
typedef pair < ll, int > pl;
pl t[N << 2];
vector < int > vs;
int id[N];
void add(int x, int l, int r, int x1) {
if(l == r) {
t[x].first += vs[x1 - 1];
t[x].second += 1;
return;
}
int m = l + r >> 1;
if(x1 <= m)
add(x + x, l, m, x1);
else
add(x + x + 1, m + 1, r, x1);
t[x] = make_pair(t[x + x].first + t[x + x + 1].first, t[x + x].second + t[x + x + 1].second);
}
void del(int x, int l, int r, int x1) {
if(l == r) {
t[x].first -= vs[x1 - 1];
t[x].second -= 1;
return;
}
int m = l + r >> 1;
if(x1 <= m)
del(x + x, l, m, x1);
else
del(x + x + 1, m + 1, r, x1);
t[x] = make_pair(t[x + x].first + t[x + x + 1].first, t[x + x].second + t[x + x + 1].second);
}
ll get(int x, int l, int r, int k) {
if(l == r) {
return (!!k) * t[x].first;
}
int m = l + r >> 1;
if(t[x + x + 1].second >= k)
return get(x + x + 1, m + 1, r, k);
return t[x + x + 1].first + get(x + x, l, m, k - t[x + x + 1].second);
}
int L, R;
void fix(int l, int r) {
while(R < r) add(1, 1, n, id[++R]);
while(R > r) del(1, 1, n, id[R--]);
while(L > l) add(1, 1, n, id[--L]);
while(L < l) del(1, 1, n, id[L++]);
}
void solve(int l, int r, int optL, int optR) {
//printf("l = %d r = %d optL = %d optR = %d\n", l, r, optL, optR);
if(l > r)
return;
int m = l + r >> 1;
ll res = 0;
int opt = -1;
for(int i = optL; i <= optR; i++) {
fix(i, m);
ll ret = get(1, 1, n, d - (m - i) - (m - start));
//printf("i = %d m = %d ret = %d\n", i, m, ret);
if(opt == -1 or ret > res) {
ans = max(ans, ret);
res = ret;
opt = i;
}
}
solve(l, m - 1, optL, opt);
solve(m + 1, r, opt, optR);
}
void solveStart() {
L = 1;
R = 0;
memset(t, 0, sizeof(t));
solve(start, n, 1, start);
}
long long int findMaxAttraction(int N, int Start, int D, int Attraction[]) {
n = N;
start = Start + 1;
d = D;
for(int i = 0; i < n; i++)
a[i + 1] = Attraction[i];
for(int i = 1; i <= n; i++) {
vs.push_back(a[i]);
}
sort(vs.begin(), vs.end());
vs.resize(unique(vs.begin(), vs.end()) - vs.begin());
for(int i = 1; i <= n; i++)
id[i] = lower_bound(vs.begin(), vs.end(), a[i]) - vs.begin() + 1;
solveStart();
reverse(a + 1, a + n + 1);
reverse(id + 1, id + n + 1);
start = n - start + 1;
solveStart();
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... |