This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC diagnostic warning "-std=c++11"
#include <bits/stdc++.h>
#include "holiday.h"
using namespace std;
const int NN=1e5+5;
int n,d,st;
long long arr[NN],f[NN*3],ind[NN];
struct segmentTree {
struct node {
long long val=0,cnt=0;
};
vector<node> t;
void build(int v, int tl, int tr) {
int NUM=(tr-tl+1)*4;
while (NUM--) {
node X; X.val=0; X.cnt=0;
t.push_back(X);
}
}
void update(int v, int tl, int tr, int ind, long long a, long long b) {
if (tl==tr) {
t[v].val+=a; t[v].cnt+=b; return;
}
int mid=(tl+tr)/2;
if (ind<=mid) update(v*2,tl,mid,ind,a,b);
else update(v*2+1,mid+1,tr,ind,a,b);
t[v].val=t[v*2].val+t[v*2+1].val; t[v].cnt=t[v*2].cnt+t[v*2+1].cnt;
}
long long query(int v, int tl, int tr, int k) {
if (k<=0) return 0;
if (v>=t.size()) return 0;
if (t[v].cnt<=k) return t[v].val;
int mid=(tl+tr)/2;
long long res=query(v*2,tl,mid,k);
if (t[v*2].cnt<k) res+=query(v*2+1,mid+1,tr,k-t[v*2].cnt);
return res;
}
} segtree;
void solve(int tl, int tr, int l, int r, int x) {
if (tl>tr) return;
int mid=(tl+tr)/2;
long long mx_num=-1; int mx_ind=0;
for (int i=l; i<=r; i++) {
segtree.update(1,st,n-1,ind[i],arr[i],1);
long long num=segtree.query(1,st,n-1,mid-(i-st)*x);
if (num>mx_num) {
mx_num=num; mx_ind=i;
}
}
f[mid]=mx_num;
for (int i=l; i<=r; i++) segtree.update(1,st,n-1,ind[i],-arr[i],-1);
if (tl==tr) return;
for (int i=l; i<mx_ind; i++) segtree.update(1,st,n-1,ind[i],arr[i],1);
solve(mid+1,tr,mx_ind,r,x);
for (int i=l; i<mx_ind; i++) segtree.update(1,st,n-1,ind[i],-arr[i],-1);
solve(tl,mid-1,l,mx_ind,x);
}
long long cal() {
if (st==0) return 0;
pair<int,int> arr2[n];
for (int i=0; i<n; i++) arr2[i]={arr[i],i};
sort(arr2+st,arr2+n); reverse(arr2+st,arr2+n);
for (int i=0; i<n; i++) ind[arr2[i].second]=i;
segtree.t.clear(); segtree.build(1,st,n-1);
solve(0,d,st,n-1,2);
long long f1[d+1];
for (int i=0; i<=d; i++) f1[i]=f[i];
reverse(arr,arr+n); int in_st=st;
st=n-1-st; st++;
for (int i=0; i<n; i++) arr2[i]={arr[i],i};
sort(arr2+st,arr2+n); reverse(arr2+st,arr2+n);
for (int i=0; i<n; i++) ind[arr2[i].second]=i;
segtree.t.clear(); segtree.build(1,st,n-1);
solve(0,d,st,n-1,1);
st=in_st; reverse(arr,arr+n);
long long f2[d+1];
for (int i=0; i<=d; i++) f2[i]=f[i];
long long ans=0;
for (int i=0; i<=d-1; i++) {
int j=d-i-1;
ans=max(ans,f1[i]+f2[j]);
}
// for (int i=0; i<=d; i++) cout<<"I1 "<<i<<" "<<f1[i]<<endl;
// for (int i=0; i<=d; i++) cout<<"I2 "<<i<<" "<<f2[i]<<endl;
return ans;
}
long long findMaxAttraction(int N, int START, int D, int attraction[]) {
n=N; st=START; d=D;
for (int i=0; i<n; i++) arr[i]=attraction[i];
long long ans1=cal(); reverse(arr,arr+n); st=n-1-st; long long ans2=cal();
return max(ans1,ans2);
}
Compilation message (stderr)
holiday.cpp:2:33: warning: '-std=c++11' is not an option that controls warnings [-Wpragmas]
2 | #pragma GCC diagnostic warning "-std=c++11"
| ^~~~~~~~~~~~
holiday.cpp: In member function 'long long int segmentTree::query(int, int, int, int)':
holiday.cpp:38:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<segmentTree::node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if (v>=t.size()) 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... |