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;
int n,m;
vector<int> s;
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)
{
vector<long long> tt;
vector<int> w;
for(int i=0 ;i<N ; i++){
if(W[i]>X){
tt.push_back(T[i]);
w.push_back(W[i]);
}
}
N = tt.size();
T = tt;
n=N;
m=M;
s=S;
W =w;
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++)
v.push_back({t[i][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;
});
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]-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)
{
long long ans = Y+l*x;
for(int i=0 ; i<m-1 ; i++){
for(int j=0 ; j<n ; j++)
if(t[i][j]<Y+x*s[i] && Y+x*s[i+1]<t[i+1][j]){
ans=max(ans,dp[i+1][j]);
}
}
return ans;
}
Compilation message (stderr)
overtaking.cpp: In function 'void upd(int, int, int, int, long long int, int)':
overtaking.cpp:32:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | 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:70:19: warning: variable 'prv' set but not used [-Wunused-but-set-variable]
70 | long long prv = -1;
| ^~~
overtaking.cpp:86:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(nn=1 ; nn<b.size() ;nn *=2);
| ~~^~~~~~~~~
overtaking.cpp:121:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<A>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | 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... |