This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Power Of Ninja Go
#include <bits/stdc++.h>
#ifdef atom
#include "grader.cpp"
#else
#include "holiday.h"
#endif
using namespace std;
typedef long long ll; typedef pair<int, int> ii;
#define X first
#define Y second
#define vi vector<int>
#define vii vector< ii >
#define pb push_back
int *arr, N, st, D;
struct node
{
ll v;
node *left, *right;
node(ll a, node *b, node *c)
{
v = a;
left = b; right = c;
}
node* update(int L, int R, int x, int dx)
{
//printf("%d %d %d\n", L, R, x);
if(L> x || R< x) return this;
if(L == x && x == R)
{
return new node(this->v+dx, NULL, NULL);
}
int M = (L+R)/2;
return new node(this->v+dx, this->left->update(L, M, x, dx), this->right->update(M+1, R, x, dx));
}
};
node *zero;
const int maxn = 2e5+5;
node *cnt[maxn], *val[maxn];
ll pong(node *va, node *vb, node *a, node *b, int k)
{
//printf("ei (%lld-%lld) with (%lld-%lld)\n", a->v, b->v, (va->v), (vb->v));
if(a->v - b->v <= k) return va->v - vb->v;
int x = a->right->v - b->right->v;
//printf("yoyo %d\n", x);
if(k<= x) return pong(va->right, vb->right, a->right, b->right, k);
return (va->right->v - vb->right->v) + pong(va->left, vb->left, a->left, b->left, k-x);
}
ll sumK(int k, int a, int b)
{
return pong(val[b], a?val[a-1]:zero, cnt[b], a?cnt[a-1]:zero, k);
}
ll solve(int L = 0, int R = st, int i = st, int j = N-1)
{
if(L> R) return 0;
int M = (L+R)/2;
int opt = i;
ll res = -4e18;
for(int p = i; p<= j; p++)
{
int a = st-M, b = p-st;
int del = min(a, b)*2+max(a, b);
if(del>= D) break;
ll here = sumK(D-del, M, p);
//printf("sumK(%d %d %d) = %lld\n", D-del, M, p, here);
if(here> res)
{
res = here;
opt = p;
}
}
return max(max(solve(L, M-1, i, opt), solve(M+1, R, opt, j)), res);
}
int rev[maxn];
long long findMaxAttraction(int n, int start, int d, int attraction[])
{
arr = attraction; N = n; st = start; D = d;
zero = new node(0, NULL, NULL);
zero->left = zero->right = zero;
vii foo;
for(int i = 0; i< n; i++) foo.pb(ii(arr[i], i));
sort(foo.begin(), foo.end());
for(int i = 0; i< n; i++) rev[foo[i].Y] = i;
for(int i = 0; i< n; i++)
{
//printf("(%d)\n", rev[i]);
cnt[i] = (i?cnt[i-1]:zero)->update(0, n-1, rev[i], 1);
val[i] = (i?val[i-1]:zero)->update(0, n-1, rev[i], arr[i]);
}
//printf("kuy %lld\n", val[n-1]->v);
return solve();
}
Compilation message (stderr)
grader.cpp: In function 'int main()':
grader.cpp:7:12: warning: variable 'n_s' set but not used [-Wunused-but-set-variable]
int i, n_s;
^
# | 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... |