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>
#include"holiday.h"
using namespace std;
#define ll long long
#define pill pair<int,ll>
#define pi pair<int,int>
#define f first
#define s second
/***********************/
struct SegTree{
vector<pill> st;
vector<int> minmax;
int sz;
SegTree(int n){
sz = n;
st.assign(n*4, {0,0ll});
minmax.assign(n*4, 0);
}
void updactive(int n, int l, int r, int ind, int val){
if(l == r){
st[n].f = val;
return;
}
int mid = (l + r)/2;
if(mid >= ind)
updactive(2*n+1, l, mid, ind, val);
else
updactive(2*n + 2, mid + 1, r, ind, val);
st[n].f = st[2*n+1].f + st[2*n+2].f;
}
void updval(int n, int l, int r, int ind, int val){
if(l == r){
st[n].s = val;
return;
}
int mid = (l + r)/2;
if(mid >= ind)
updval(2*n+1, l, mid, ind, val);
else
updval(2*n + 2, mid + 1, r, ind, val);
st[n].s = st[2*n+1].s + st[2*n+2].s;
}
void updmin(int n, int l, int r, int ind, int val){
if(l == r){
minmax[n] = val;
return;
}
int mid = (l + r)/2;
if(mid >= ind)
updmin(2*n + 1, l, mid, ind , val);
else
updmin(2*n+2, mid + 1, r, ind , val);
minmax[n] = min(minmax[2* n +1], minmax[2*n + 2]);
}
void updmax(int n, int l, int r, int ind, int val){
if(l == r){
minmax[n] = val;
return;
}
int mid = (l + r)/2;
if(mid >= ind)
updmax(2*n + 1, l, mid, ind , val);
else
updmax(2*n+2, mid + 1, r, ind , val);
minmax[n] = max(minmax[2* n +1], minmax[2*n + 2]);
}
void activate(int ind,int city, int val, bool right){
updactive(0,0,sz, ind, 1);
updval(0,0,sz, ind, val);
if(right)
updmax(0,0,sz,ind,city);
else
updmin(0,0,sz,ind, city);
}
int getfirst(int n, int l, int r, int val){
if(l == r) return l;
int mid = (l + r)/2;
if(st[2*n+1].f >= val )
return getfirst(2*n+1, l, mid, val);
return getfirst(2*n+ 2, mid + 1, r, val - st[2*n+1].f);
}
int getfirst(int val){
int target = st[0].f - val +1;
if(target <=0)
return 0;
return getfirst(0, 0, sz, target);
}
ll qrysum(int n, int l, int r, int s, int e){
if(s<= l and e >= r) return st[n].s;
ll ans = 0;
int mid = (l + r)/2;
if( mid >= s)
ans += qrysum(2*n+1, l, mid, s, e);
if(mid < e)
ans+=qrysum(2*n+2, mid + 1, r, s,e);
return ans;
}
int getmin(int n, int l, int r, int s, int e){
if(s<= l and e >= r) return minmax[n];
int ans = 1e9;
int mid = (l + r)/2;
if( mid >= s)
ans = min(getmin(2*n+1, l, mid, s, e), ans);
if(mid < e)
ans = min(getmin(2*n+2, mid + 1, r, s,e), ans);
return ans;
}
int getmax(int n, int l, int r, int s, int e){
if(s<= l and e >= r) return minmax[n];
int ans = -1;
int mid = (l + r)/2;
if( mid >= s)
ans = max(getmax(2*n+1, l, mid, s, e), ans);
if(mid < e)
ans = max(getmax(2*n+2, mid + 1, r, s,e),ans);
return ans;
}
pair<ll, int> getlargest(int rng, bool right){
int lower = getfirst(rng);
//cout << lower<<endl;
ll res = qrysum(0, 0,sz, lower, sz );
if(right)
return {res, getmax(0, 0,sz, lower,sz)};
return {res, getmin(0, 0,sz, lower,sz)};
}
};
vector<int> inds;
int n;
int start;
vector<ll> dpright;
vector<int> minright;
vector<int> attraction;
vector<SegTree> strights;
void dcright(int s, int e, int l, int r, int lvl){
if(e < s || l > r) return;
int days = (s + e)/2;
for(int right = l; right <=r; right++){
strights[lvl].activate(inds[right],right, attraction[right], true);
int dist = right - start;
/*if(days - dist <= 0){
break;
}*/
pair<ll, int> res = strights[lvl].getlargest(days - dist, true);
//if(days == 2) cout << res.f<<", "<<res.s<<", "<<dist<<endl;
if(res.f > dpright[days]){
dpright[days] = res.f;
minright[days] = res.s;
}
else if(res.f == dpright[days]){
minright[days] = min(minright[days], res.s);
}
}
dcright(s, days -1, l, minright[days], lvl+1);
dcright(days +1, e, minright[days], r,lvl+1);
}
vector<ll> dpleft;
vector<int> maxleft;
vector<SegTree> stlefts;
void dcleft(int s, int e, int l, int r, int lvl){
if(e < s || l > r) return;
int days = (s + e)/2;
for(int left = r; left >=l; left--){
stlefts[lvl].activate(inds[left],left, attraction[left], false);
int dist = start - left;
pair<ll, int> res = stlefts[lvl].getlargest(days - dist,false);
if(res.f > dpleft[days]){
dpleft[days] = res.f;
maxleft[days] = res.s;
}
else if(res.f == dpright[days]){
maxleft[days] = max(maxleft[days], res.s);
}
}
dcleft(s, days -1, maxleft[days],r, lvl+1);
dcleft(days +1, e,l, maxleft[days],lvl+1);
}
long long int findMaxAttraction(int n_, int start_, int d, int attractions[]) {
n = n_;
start = start_;
attraction.assign(n,0);
for(int i =0; i<n; i++) attraction[i] = attractions[i];
vector<pi> cities(n);
for(int i =0; i<n; i++){
cities[i] = {attraction[i], i};
}
sort(cities.begin(), cities.end());
inds.assign(n, 0);
for(int i= 0; i<n;i++)
inds[cities[i].s] = i;
dpright.assign(d+1, -1);
minright.assign(d + 1, 1e9);
strights.assign(30, SegTree(n));
dcright(1, d, start, n -1, 0);
strights.clear();
dpleft.assign(d+1, 0);
maxleft.assign(d + 1, 0);
stlefts.assign(30, SegTree(n));
dcleft(1, d,0, start-1, 0);
stlefts.clear();
ll ans = 0;
for(int daysright = 0; daysright <= d; daysright++){
int distright = daysright == 0 ? 0: minright[daysright] - start;
int daysleft = max(0,d - daysright - distright);
//cout<< daysleft<<", "<<daysright<<", "<<distright<<": "<<dpleft[daysleft]<<", "<<dpright[daysright]<<endl;
ans = max(ans,dpleft[daysleft] + dpright[daysright] );
}
//cout <<endl;
for(int daysleft = 0; daysleft <= d; daysleft++){
int distleft = daysleft == 0 ? 0: start - maxleft[daysleft] ;
int daysright = max(0,d - daysleft - distleft);
//cout<< daysleft<<", "<<daysright<<", "<<distleft<<": "<<dpleft[daysleft]<<", "<<dpright[daysright]<<endl;
ans = max(ans,dpleft[daysleft] + dpright[daysright] );
}
return ans;
}
/***********************/
# | 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... |