#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
const long long INF=1e18;
const int mxn=1001;
struct T{
long long mnx=INF,mxx=-INF,mny=INF,mxy=-INF;
int l=0,r=0;
}st[mxn*24];
long long s[mxn],d[mxn];
int id,idx[mxn],root[mxn];
vector <long long> v;
T operator +(T a, T b){
return {min(a.mnx,b.mnx),max(a.mxx,b.mxx),min(a.mny,b.mny),max(a.mxy,b.mxy)};
}
int update(int node, int l, int r, int i){
id++;
if (l==r){
st[id]={s[l]+d[l],s[l]+d[l],s[l]-d[l],s[l]-d[l]};
return id;
}
int cur=id,mid=(l+r)>>1;
st[cur]=st[node];
if (mid<i)
st[cur].r=update(st[node].r,mid+1,r,i);
else
st[cur].l=update(st[node].l,l,mid,i);
st[cur].mnx=min(st[st[cur].l].mnx,st[st[cur].r].mnx);
st[cur].mxx=max(st[st[cur].l].mxx,st[st[cur].r].mxx);
st[cur].mny=min(st[st[cur].l].mny,st[st[cur].r].mny);
st[cur].mxy=max(st[st[cur].l].mxy,st[st[cur].r].mxy);
return cur;
}
T get(int node, int l, int r, int u, int v){
if (l>v||r<u||l>r||u>v)
return {INF,-INF,INF,-INF};
if (l>=u&&r<=v)
return st[node];
int mid=(l+r)>>1;
return get(st[node].l,l,mid,u,v)+get(st[node].r,mid+1,r,u,v);
}
long long find_shortcut(int n, vector <int> l, vector <int> D, int c){
long long lo=0,hi=INF,kq=-1;
for (int i=0;i<n;i++)
d[i]=D[i];
for (int i=1;i<n;i++){
s[i]=s[i-1]+l[i-1];
v.push_back(s[i]);
}
sort(v.begin(),v.end());
iota(idx,idx+n,0);
sort(idx,idx+n,[](int i, int j){return s[i]+d[i]>s[j]+d[j];});
for (int i=0;i<n;i++){
root[i]=update((i?root[i-1]:0),0,n-1,idx[i]);
cout << idx[i] << ' ';
}
cout << '\n';
while (lo<=hi){
long long mid=(lo+hi)>>1,ch=0,mnx=-INF,mxx=INF,mny=-INF,mxy=INF;
for (int i=0;i<n;i++){
int l=0,r=n-1,pos=-1;
while (l<=r){
int m=(l+r)>>1;
if (s[idx[m]]+d[idx[m]]>mid+s[i]-d[i]){
pos=m;
l=m+1;
}
else
r=m-1;
}
T t=get(root[pos],0,n-1,i+1,n-1);
mnx=max(mnx,s[i]+d[i]+c-mid+t.mxx);
mxx=min(mxx,s[i]-d[i]-c+mid+t.mny);
mny=max(mny,s[i]+d[i]+c-mid-t.mny);
mxy=min(mxy,s[i]-d[i]-c+mid-t.mxx);
}
for (int i=0;i<n;i++){
long long l=max(mnx-s[i],s[i]-mxy),r=min(mxx-s[i],s[i]-mny);
int x=lower_bound(v.begin(),v.end(),l)-v.begin(),y=upper_bound(v.begin(),v.end(),r)-v.begin()-1;
if (x<=y)
ch=1;
}
if (ch){
kq=mid;
hi=mid-1;
}
else
lo=mid+1;
}
return kq;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Secret is incorrect! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Secret is incorrect! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Secret is incorrect! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Secret is incorrect! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Secret is incorrect! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Secret is incorrect! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Secret is incorrect! |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Secret is incorrect! |
2 |
Halted |
0 ms |
0 KB |
- |