/// 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4480 KB |
Output is correct |
2 |
Correct |
15 ms |
4480 KB |
Output is correct |
3 |
Correct |
18 ms |
6784 KB |
Output is correct |
4 |
Correct |
17 ms |
6784 KB |
Output is correct |
5 |
Correct |
40 ms |
18680 KB |
Output is correct |
6 |
Correct |
13 ms |
6016 KB |
Output is correct |
7 |
Correct |
22 ms |
10496 KB |
Output is correct |
8 |
Correct |
29 ms |
12800 KB |
Output is correct |
9 |
Correct |
9 ms |
4352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1280 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
1280 KB |
Output is correct |
4 |
Correct |
4 ms |
1280 KB |
Output is correct |
5 |
Correct |
3 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
5128 KB |
Output is correct |
2 |
Correct |
76 ms |
44332 KB |
Output is correct |
3 |
Correct |
62 ms |
18936 KB |
Output is correct |
4 |
Correct |
3 ms |
1280 KB |
Output is correct |
5 |
Correct |
2 ms |
768 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
250 ms |
43856 KB |
Output is correct |
9 |
Correct |
246 ms |
43896 KB |
Output is correct |