This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In the name of God
#include <bits/stdc++.h>
#include"holiday.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll maxn = 2e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
#define lc (id << 1)
#define rc (id << 1 | 1)
int start, n, d;
ll a[maxn], at[maxn];
ll seg[maxn << 2], cnt[maxn << 2];
pll b[maxn];
void upd(ll id, ll s, ll e, ll i, ll x){
if(i < s || e <= i) return;
if(e - s == 1){
cnt[id] += x;
seg[id] += x * a[s];
return;
}
ll mid = (s + e) >> 1;
upd(lc, s, mid, i, x);
upd(rc, mid, e, i, x);
cnt[id] = cnt[lc] + cnt[rc];
seg[id] = seg[lc] + seg[rc];
return;
}
ll get(ll id, ll s, ll e, ll x){
if(x <= 0) return 0;
if(x >= cnt[id]) return seg[id];
ll mid = (s + e) >> 1;
return get(rc, mid, e, x) + get(lc, s, mid, x - cnt[rc]);
}
ll len(ll l, ll r){
return r - l + (start - l);
}
ll is[maxn];
ll cal(ll l, ll r){
return get(1, 0, n, d - len(l, r));
}
void add(ll i){
is[i]++;
upd(1, 0, n, at[i], 1);
}
void rem(ll i){
is[i]--;
upd(1, 0, n, at[i], -1);
}
ll solve(ll s, ll e, ll l, ll r){
if(s >= e) return 0;
// [s, l]
ll mid = (s + e) >> 1;
for(ll i = s; i < mid; i++){
rem(i);
}
// [mid, l]
ll ans = cal(mid, l);
ll opt = l;
for(ll i = l + 1; i < r; i++){
add(i);
ll cc = cal(mid, i);
if(cc > ans){
ans = cc;
opt = i;
}
}
// [mid, r - 1]
rem(mid);
// [mid + 1, r - 1]
for(ll i = opt + 1; i < r; i++){
rem(i);
}
// [mid + 1, opt]
ans = max(ans, solve(mid + 1, e, opt, r));
// [mid + 1, opt]
for(ll i = s; i <= mid; i++){
add(i);
}
// [s, opt]
for(ll i = l + 1; i <= opt; i++){
rem(i);
}
// [s, l]
ans = max(ans, solve(s, mid, l, opt + 1));
// [s, l]
return ans;
}
long long int findMaxAttraction(int N, int Start, int D, int A[]){
d = D;
n = N;
start = Start;
for(ll i = 0; i < n; i++){
b[i] = Mp(A[i], i);
}
sort(b, b + n);
for(ll i = 0; i < n; i++){
a[i] = b[i].F;
at[b[i].S] = i;
}
for(ll i = 0; i <= start; i++){
add(i);
}
ll ans = solve(0, start + 1, start, n);
for(ll i = 0; i <= start; i++){
rem(i);
}
reverse(at, at + n);
start = n - start - 1;
for(ll i = 0; i <= start; i++){
add(i);
}
ans = max(ans, solve(0, start + 1, start, n));
return ans;
}
/*int main(){
fast_io;
return 0;
}*/
# | 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... |