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>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 3e5 + 69;
const ll mod = 1e9 + 7;
typedef pair<ll,ll> LL;
ll n,start,d;
ll val[2][N],ID[2][N];
struct Node{
ll val,on;
};
Node st[4*N];
Node operator + (Node a,Node b){
Node c;
c.val = a.val + b.val; c.on = a.on + b.on;
return c;
}
void Renew(){
for (ll i = 1;i < 4*N;i++) st[i].val = st[i].on = 0;
}
void upd(ll id,ll l,ll r,ll u,ll val){
if (u < l||r < u) return;
if (l == r){
st[id].val = val; st[id].on = 1;
return;
}
ll mid = (l + r)/2;
upd(id*2,l,mid,u,val); upd(id*2 + 1,mid + 1,r,u,val);
st[id] = st[id*2] + st[id*2 + 1];
}
ll Get(ll id,ll l,ll r,ll cnt){
if (st[id].on <= cnt) return st[id].val;
ll mid = (l + r)/2;
if (st[id*2].on > cnt) return Get(id*2,l,mid,cnt);
else return st[id*2].val + Get(id*2 + 1,mid + 1,r,cnt - st[id*2].on);
}
struct elem{
ll L,R,fday,lday;
};
void update(ll &mx,ll &ans,ll cur,ll day,ll sz){
ll rm = day - cur + 1;
if (rm <= 0) return;
ll p = Get(1,1,sz,rm);
if (p > mx){
mx = p; ans = cur;
}
}
void f(ll day,ll a[],ll sz,ll cond){
ll id[N]; LL A[N]; Renew();
for (ll i = 1;i <= sz;i++) A[i].fs = a[i],A[i].sc = i;
sort(A + 1,A + sz + 1,greater<LL>());
for (ll i = 1;i <= sz;i++) id[A[i].sc] = i;
queue<elem> q;
q.push({1,sz,1,day});
ll cur = 1;
while(!q.empty()){
elem t = q.front(); q.pop();
ll mx = -69,ans,now = (t.fday + t.lday)/2; //cout<<t.L<<" "<<t.R<<" x "<<t.fday<<" "<<t.lday<<"\n";
update(mx,ans,cur - 1,now,sz);
if (t.fday > t.lday) continue;
//if (t.L == 1) Renew(),cur = 1;
if (t.fday > t.lday) continue;
while(cur <= t.R){
upd(1,1,sz,id[cur],a[cur]); update(mx,ans,cur,now,sz);
cur++;
}
val[cond][now] = mx; ID[cond][now] = ans;
//cout<<t.L<<" "<<t.R<<" "<<now<<" x "<<mx<<" "<<ans<<"\n";
q.push({t.L,ans,t.fday,now - 1});
q.push({ans,t.R,now + 1,t.lday});
if (t.lday == d) Renew(),cur = 1;
}
}
ll final_ans = 0;
void Got(){
for (ll i = 0;i <= d;i++){
ll p = val[0][i],rm = d - i - ID[0][i],inc = 0;
if (rm > 0) inc += val[1][rm];
final_ans = max(final_ans,p + inc);
}
}
ll a[N],A[N],B[N];
ll findMaxAttraction(int _n,int s,int _d,int arr[]){
start = s + 1; d = _d; n = _n; ///anyone if 0 point pay attention to this fucking line, it kill my day
for (ll i = 0;i < n;i++) a[i + 1] = arr[i];
//for (ll i = 1;i <= n;i++) a[i] = arr[i];
for (ll i = start;i <= n;i++) A[i - start + 1] = a[i]; f(d,A,n - start + 1,0); ID[0][0] = 1;
for (ll i = start - 1;i > 0;i--) B[start - i] = a[i]; f(d,B,start - 1,1); Got();
//cout<<final_ans; exit(0);
for (ll i = 0;i <= d;i++) val[0][i] = val[1][i] = 0;
for (ll i = start + 1;i <= n;i++) B[i - start] = a[i]; f(d,B,n - start,1);
for (ll i = start;i > 0;i--) A[start - i + 1] = a[i]; f(d,A,start,0); Got();
return final_ans;
}
/*
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".INP","r")){
freopen(task".INP","r",stdin);
//freopen(task".OUT","w",stdout);
}
ll a[N];
cin>>n>>start>>d;
for (int i = 0;i < n;i++) cin>>a[i];
cout<<findMaxAttraction(n,start,d,a);
}
*/
Compilation message (stderr)
holiday.cpp: In function 'long long int findMaxAttraction(int, int, int, int*)':
holiday.cpp:98:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
98 | for (ll i = start;i <= n;i++) A[i - start + 1] = a[i]; f(d,A,n - start + 1,0); ID[0][0] = 1;
| ^~~
holiday.cpp:98:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
98 | for (ll i = start;i <= n;i++) A[i - start + 1] = a[i]; f(d,A,n - start + 1,0); ID[0][0] = 1;
| ^
holiday.cpp:99:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
99 | for (ll i = start - 1;i > 0;i--) B[start - i] = a[i]; f(d,B,start - 1,1); Got();
| ^~~
holiday.cpp:99:59: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
99 | for (ll i = start - 1;i > 0;i--) B[start - i] = a[i]; f(d,B,start - 1,1); Got();
| ^
holiday.cpp:103:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
103 | for (ll i = start + 1;i <= n;i++) B[i - start] = a[i]; f(d,B,n - start,1);
| ^~~
holiday.cpp:103:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
103 | for (ll i = start + 1;i <= n;i++) B[i - start] = a[i]; f(d,B,n - start,1);
| ^
holiday.cpp:104:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
104 | for (ll i = start;i > 0;i--) A[start - i + 1] = a[i]; f(d,A,start,0); Got();
| ^~~
holiday.cpp:104:59: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
104 | for (ll i = start;i > 0;i--) A[start - i + 1] = a[i]; f(d,A,start,0); Got();
| ^
# | 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... |