QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#137895 | #6343. Bitaro's travel | Rafi22# | 0 | 208ms | 62500kb | C++14 | 3.3kb | 2023-08-10 18:43:58 | 2024-07-04 01:32:05 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define ld long double
//#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
int inf=1000000000000000007;
int mod=1000000007;
int mod1=998244353;
const int N=200007,L=18;
int mn[N][L];
int mx[N][L];
int lg[N];
int get_mn(int l,int r)
{
int k=lg[r-l+1];
return min(mn[l][k],mn[r-(1<<k)+1][k]);
}
int get_mx(int l,int r)
{
int k=lg[r-l+1];
return max(mx[l][k],mx[r-(1<<k)+1][k]);
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
vector<int>a(n+2);
a[0]=-inf;
for(int i=1;i<=n;i++) cin>>a[i];
a[n+1]=inf;
for(int i=1;i<=n+1;i++)
{
mn[i][0]=2*a[i-1]-a[i];
mx[i][0]=2*a[i]-a[i-1];
}
for(int j=1;j<L;j++)
{
for(int i=1;i+(1<<j)-1<=n+1;i++)
{
mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
mx[i][j]=max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
}
}
for(int i=2;i<=n+1;i++) lg[i]=lg[i/2]+1;
int q;
cin>>q;
while(q--)
{
int x,ans=0;
cin>>x;
int l=lower_bound(all(a),x)-a.begin();
ans+=min(a[l]-x,x-a[l-1]);
if(a[l]-x>=x-a[l-1]) l--;
int r=l;
bool side=0;
while(l>1||r<n)
{
if(l==1)
{
if(side) ans+=a[n]-a[r];
else ans+=a[n]-a[l];
break;
}
if(r==n)
{
if(side) ans+=a[r]-a[1];
else ans+=a[l]-a[1];
break;
}
if(side)
{
int L=r+1,R=n+1,sr,i;
while(L<=R)
{
sr=(L+R)/2;
if(get_mn(r+1,sr)<=a[l-1])
{
i=sr;
R=sr-1;
}
else L=sr+1;
}
/* for(int i=r+1;i<=n+1;i++)
{
if(2*a[i-1]-a[i]<=a[l-1])
{
break;
}
}*/
ans+=a[i-1]-a[r];
ans+=a[i-1]-a[l-1];
r=i-1;
l--;
}
else
{
int L=0,R=l-1,sr,i;
while(L<=R)
{
sr=(L+R)/2;
if(get_mx(L+1,l)>a[r+1])
{
i=sr;
L=sr+1;
}
else R=sr-1;
}
/* for(int i=l-1;i>=0;i--)
{
if(2*a[i+1]-a[i]>a[r+1])
{
break;
}
}*/
ans+=a[l]-a[i+1];
ans+=a[r+1]-a[i+1];
l=i+1;
r++;
}
side^=1;
}
cout<<ans<<endl;
}
return 0;
}
/*
5
0 5 6 7 9
1
7
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 7680kb
input:
2000 154914587 154914588 154914591 154914592 154914594 154914596 154914598 154914599 154914601 154914603 154914608 154914610 154914612 154914615 154914618 154914619 154914621 154914622 154914626 154914627 154914631 154914633 154914636 154914638 154914640 154914641 154914642 154914644 154914645 15491...
output:
1619812619
result:
wrong answer 1st numbers differ - expected: '809906250', found: '1619812619'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #31:
score: 0
Wrong Answer
time: 208ms
memory: 62500kb
input:
200000 9 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181...
output:
200107 999999991 202154 400098 379455 400098 400098 381369 400098 265794 363817 400097 342292 260613 302587 400097 400098 281789 345769 400097 218270 400098 400098 400097 400097 332977 400097 224651 400098 259560 206246 231269 400097 371811 400098 382486 202443 400098 359464 400098 354417 400098 400...
result:
wrong answer 4th numbers differ - expected: '346046', found: '400098'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%