This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/// hhoangcpascal
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#define file_name "Holiday"
#define llong long long
using namespace std;
struct TNode {
int Left, Right;
int cnt;
llong sum;
} Seg[1800006];
int Time = 0, sz, Start, n, d, root[100006], a[100006], sorted[100006];
void Compress() {
sort(sorted+1, sorted+n+1);
sz = unique(sorted+1, sorted+n+1) - sorted - 1;
for(int i=1; i<=n; ++i) a[i] = lower_bound(sorted+1, sorted+sz+1, a[i]) - sorted;
}
void Update(int old, int ver, int l, int r, int x, int val) {
if (l > x || r < x || l > r) return;
if (l == r) {
Seg[ver].sum = Seg[old].sum + 1LL * val * sorted[l];
Seg[ver].cnt = Seg[old].cnt + val;
return;
}
int mid = (l + r) >> 1;
if (x <= mid) {
Seg[ver].Right = Seg[old].Right;
Seg[ver].Left = ++Time;
Update(Seg[old].Left, Seg[ver].Left, l, mid, x, val);
} else {
Seg[ver].Left = Seg[old].Left;
Seg[ver].Right = ++Time;;
Update(Seg[old].Right, Seg[ver].Right, mid+1, r, x, val);
}
Seg[ver].cnt = Seg[Seg[ver].Left].cnt + Seg[Seg[ver].Right].cnt;
Seg[ver].sum = Seg[Seg[ver].Left].sum + Seg[Seg[ver].Right].sum;
}
llong Get(int old, int ver, int l, int r, int cnt) {
if (cnt <= 0) return 0LL;
if (l == r) return 1LL * cnt * sorted[l];
int mid = (l + r) >> 1;
int tmp = Seg[Seg[ver].Right].cnt - Seg[Seg[old].Right].cnt;
llong ans;
if (tmp > cnt) ans = Get(Seg[old].Right, Seg[ver].Right, mid+1, r, cnt);
else {
ans = Seg[Seg[ver].Right].sum - Seg[Seg[old].Right].sum;
ans += Get(Seg[old].Left, Seg[ver].Left, l, mid, cnt - tmp);
}
return ans;
}
llong res = 0;
void DAC(int l, int r, int u, int v) {
if (l > r) return;
int mid = (l + r) >> 1, p = u;
llong tmp = -1;
for(int i=u; i<=v; ++i) {
int len_l = Start - mid, len_r = i - Start;
int cnt = d - len_l - len_r - min(len_l, len_r);
cnt = min(cnt, i - mid + 1);
if (cnt < 0) continue;
llong now = Get(root[mid-1], root[i], 1, sz, cnt);
if (tmp < now) {
tmp = now;
p = i;
}
}
res = max(res, tmp);
DAC(l, mid - 1, u, p);
DAC(mid + 1, r, p, v);
}
llong findMaxAttraction(int N, int St, int D, int arr[]) {
n = N, Start = St + 1, d = D;
for(int i=n; i>0; --i) a[i] = sorted[i] = arr[i-1];
Compress();
for(int i=1; i<=n; ++i) {
root[i] = ++Time;
Update(root[i-1], root[i], 1, sz, a[i], 1);
}
DAC(1, Start, Start, n);
return res;
}
# | 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... |