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>
#pragma GCC diagnostic warning "-std=c++11"
#define ll long long
#define f first
#define s second
#define pb push_back
#define N 300005
using namespace std;
ll n,d,s,a[N],cur,ans,lst[N],co;
vector<ll>v[N],b[N],s1[N],s2[N],g[N];
void build(ll x){
if((int)v[x].size() == 1){
lst[x] = 1;
return;
}
for(int i=1; i<v[x].size(); i++)
g[x].pb(v[x][i]);
sort(g[x].begin() , g[x].end());
int mid = g[x][((int)g[x].size() - 1) / 2];
ll raod = 0,pp = 0;
v[2*x].pb(0);b[2*x].pb(0);s1[2*x].pb(0);s2[2*x].pb(0);
v[2*x+1].pb(0);b[2*x+1].pb(0);s1[2*x+1].pb(0);s2[2*x+1].pb(0);
ll kk = v[x][1] , sum1 = 0 , sum2 = 0;
for(int i=1; i<v[x].size(); i++){
ll t = v[x][i];
if(t != kk)pp = 1;
if(t <= mid){
v[2 * x].pb(t);
sum1 += t;
raod++;
}
else{
v[2 * x + 1].pb(t);
sum2 += t;
}
b[x].pb(raod);
s1[x].pb(sum1);
s2[x].pb(sum2);
}
if(!pp){lst[x] = 1;return;}
build(2 * x);
build(2 * x + 1);
}
void count(ll x,ll L,ll R,ll raod){
if(lst[x] == 1){
if((int)v[x].size() <= 1)return;
co += min(raod , (ll)((int)v[x].size() - 1)) * v[x][1];
return;
}
if(R - L + 1 - b[x][R] + b[x][L - 1] >= raod)
count(2 * x + 1 , L - b[x][L - 1] , R - b[x][R] , raod);
if(R - L + 1 - b[x][R] + b[x][L - 1] < raod){
co += s2[x][R] - s2[x][L - 1];
count(2 * x, b[x][L - 1] + 1 , b[x][R] , raod - (R - L + 1 - b[x][R] + b[x][L - 1]));
}
}
void buildwavelet(){
v[1].pb(0);
b[1].pb(0);
s1[1].pb(0);
s2[1].pb(0);
for(int i=1; i<=n; i++)
v[1].pb(a[i]);
build(1);
}
ll get(ll x,ll y,ll k){
co = 0;
count(1 , x , y , k);
return co;
}
void solve(ll x,ll y,ll p,ll q){
if(y < x)return;
ll mid = (x + y) / 2,ind = -1,cur = -1;
for(int i=p; i<=q; i++){
ll z = get(i , mid , d - 2 * (mid - s) - (s - i));
if(z > cur){
cur = z;
if(z > ans)ans = z;
ind = i;
}
}
solve(x , mid - 1 , p , ind);
solve(mid + 1 , y , ind , q);
}
void solverev(ll x,ll y,ll p,ll q){
if(y > x)return;
ll mid = (x + y) / 2 , ind = -1, cur = -1;
for(int i=p; i<=q; i++){
ll z = get(i , mid , d - 2 * (s - mid) - (i - s));
if(z > cur){
cur = z;
ind = i;
if(z > ans)ans = z;
}
}
solverev(x , mid - 1 , p , ind);
solverev(mid + 1 , y , ind , q);
}
long long int findMaxAttraction(int nnn,int sss,int ddd, int aaa[]){
n = nnn;
s = sss + 1;
d = ddd;
for(int i=1; i<=n; i++)a[i] = aaa[i - 1];
ans = 0;
buildwavelet();
solve(s , n , 1 , s - 1);
for(int i=s; i<=n; i++){
ll z = get(s , i , d - (i - s));
if(z > ans)
ans = z;
}
for(int i=s; i>=1; i--){
ll z = get(i , s , d - (s - i));
if(z > ans)
ans = z;
}
solverev(1 , s , s + 1 , n);
return ans;
}
/*
int main(){
int nn,aa[200005],ss,dd;
cin >> nn >> ss >> dd;
for(int i=0; i<nn; i++){
cin >> aa[i];
}
cout << findMaxAttraction(nn , ss , dd , aa) << endl;
return 0;
}
*/
Compilation message (stderr)
holiday.cpp:2:32: warning: '-std=c++11' is not an option that controls warnings [-Wpragmas]
#pragma GCC diagnostic warning "-std=c++11"
^~~~~~~~~~~~
holiday.cpp: In function 'void build(long long int)':
holiday.cpp:16:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1; i<v[x].size(); i++)
~^~~~~~~~~~~~
holiday.cpp:24:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1; i<v[x].size(); i++){
~^~~~~~~~~~~~
# | 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... |