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 "overtaking.h"
#include <bits/stdc++.h>
using namespace std;
struct A{
long long l,r,t,i,j,mx;
bool operator <(const A& a)const{
return l<a.l;
}
};
long long t[1005][1005];
long long dp[1005][1005];
vector<A> a;
long long x;
long long l;
vector<long long> b;
int nn;
int g(long long x){
return lower_bound(b.begin(),b.end(),x) - b.begin();
}
long long seg[4000005];
void upd(int lef,int rig,int x,int y,long long k,int lev){
if(x>rig || lef>y || x>y)return;
if(x<=lef && rig<=y){
seg[lev]=max(seg[lev],k);
return;
}
int mid=lef+rig>>1;
upd(lef,mid,x,y,k,lev*2);
upd(mid+1,rig,x,y,k,lev*2+1);
}
void init(int L, int N, std::vector<long long> T, std::vector<int> W, int X, int M, std::vector<int> S)
{
x=X;
l = L;
for(int i=0 ; i<N ; i++){
t[0][i] = T[i];
}
for(int i=1 ; i<M ; i++){
vector<pair<long long,int>> v;
for(int j=0 ; j<N ; j++){
v.push_back({t[i-1][j],j});
}
sort(v.begin(),v.end(),[&](auto x,auto y){
if(x.first == y.first)return W[x.second]<W[y.second];
return x.first<y.first;
});
long long mx = 0;
long long prv = -1;
for(auto x: v){
int j = x.second;
t[i][j] = max(mx,t[i-1][j] + (S[i]-S[i-1])*1ll*W[j]);
prv = x.first;
mx=max(mx,t[i][j]);
a.push_back({t[i-1][j] - X*1ll*S[i-1],t[i][j]+X*1ll*(L-S[i]),t[i][j]+1ll*X*(L-S[i]),i,j});
b.push_back(t[i][j]-1ll*S[i]*X);
}
}
sort(b.begin(),b.end());
b.erase(unique(b.begin(),b.end()),b.end());
for(nn=1 ; nn<b.size() ;nn *=2);
for(int i=0 ;i<N ;i++)
dp[M-1][i] = t[M-1][i];
for(int i=M-2 ; i>= 0 ; i--){
vector<pair<long long,int>> v;
for(int j=0 ; j<N ; j++){
int l = g(t[i][j]-1ll*S[i]*X)+1;
int r = g(t[i+1][j]-1ll*S[i+1]*X)-1;
upd(0,nn-1,l,r,dp[i+1][j],1);
}
for(int j=0 ; j<N ; j++){
dp[i][j]= t[i][j]+1ll*X*(L-S[i]);
for(int l = nn+g(t[i][j]-1ll*S[i]*X) ; l>=1 ; l>>=1)
dp[i][j]=max(dp[i][j],seg[l]);
}
}
sort(a.begin(),a.end());
if(!a.empty())
a[0].mx = dp[a[0].i][a[0].j];
for(int i=1 ; i<a.size() ; i++){
a[i].mx = max(a[i-1].mx,dp[a[i].i][a[i].j]);
}
return;
}
long long arrival_time(long long Y)
{
auto k = lower_bound(a.begin(),a.end(),(A){Y});
if(k == a.begin()){
return Y+l*x;
}
k--;
return max(Y+l*x,k->mx);
}
Compilation message (stderr)
overtaking.cpp: In function 'void upd(int, int, int, int, long long int, int)':
overtaking.cpp:31:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | int mid=lef+rig>>1;
| ~~~^~~~
overtaking.cpp: In function 'void init(int, int, std::vector<long long int>, std::vector<int>, int, int, std::vector<int>)':
overtaking.cpp:57:19: warning: variable 'prv' set but not used [-Wunused-but-set-variable]
57 | long long prv = -1;
| ^~~
overtaking.cpp:73:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(nn=1 ; nn<b.size() ;nn *=2);
| ~~^~~~~~~~~
overtaking.cpp:102:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<A>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i=1 ; i<a.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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |