【题解】 比太郎之旅
背景
出到了校内模拟赛中,写出目前没有的
题意
你有
思路
我们先考虑从每个点出发的答案,记作
首先
之后我们考虑
我们考虑如何计算往右能走到哪? 把这个东西记作
对于相邻的两个点 multiset。
现在我们已经解决了从
我们走的路线一定可以刻画成上面那种模式,考虑从
那么我们只需要能求出来
假如说我们现在确定从
注意到
-
x < i -
a_{r_i} - a_x < a_{x} - a_{x - 1}
第一个限制是好做的,现在我们考虑如何满足第二个限制。
我们变一下型,可以转化为:
那么上述的第两个条件就转化为:
-
r_i < k_x
这个我们再变一下型:
-
r_i < \max_{j = x}^{i} k_j
由于我们要求的是最小的
我们已经求出来
最后他让求的是从任意坐标开始的答案,我们找到离我们询问最近的点就行了。
分析一波复杂度,求
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define uint unsigned long long
#define double long double
#define Air
namespace io{inline int read(){int f=1,t=0;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}while(ch>='0'&&ch<='9'){t=t*10+ch-'0';ch=getchar();}return t*f;}inline void write(int x){if(x<0){putchar('-');x=-x;}if(x>=10){write(x/10);}putchar(x%10+'0');}}
using namespace io;
int n, q;
const int N = 2e5 + 10;
int a[N];
int rid[N];
int tid[N];
int ans[N];
vector<int>stt[N], edd[N];
int st[21][N];
int pre[N];
int st_ask(int x, int y){
int step = pre[y - x + 1];
return max(st[step][x], st[step][y - (1ll << (step)) + 1]);
}
signed main() {
#ifndef Air
freopen("bitaro.in","r",stdin);
freopen("bitaro.out","w",stdout);
#endif
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
n = read();
rid[1] = n;
pre[1] = 0;
for(int i = 2; i <= n; i++){
pre[i] = pre[i / 2] + 1;
}
for(int i = 1; i <= n; i++){
a[i] = read();
}
for(int i = 1; i <= n; i++){
if(i >= 2){
rid[i] = lower_bound(a + 1, a + 1 + n, a[i] + a[i] - a[i - 1]) - a - 1;
}
st[0][i] = rid[i];
}
for(int i = 1; i <= 20; i ++){
for(int j = 1; j + (1ll << (i)) - 1 <= n; j++){
st[i][j] = max(st[i - 1][j], st[i - 1][j + (1ll << (i - 1))]);
}
}
for(int i = 2; i < n; i++){
int dis = a[i + 1] - a[i];
int tmp = lower_bound(a + 1, a + i, a[i] - dis) - a;
if(tmp == i) continue;
stt[tmp + 1].push_back(i);
edd[i + 1].push_back(i);
}
ans[1] = a[n] - a[1];
multiset<int>mt;
mt.insert(n);
for(int i = 1; i <= n; i++){
for(auto y: stt[i]){
mt.insert(y);
}
for(auto y: edd[i]){
mt.erase(mt.find(y));
}
tid[i] = *mt.begin();
}
for(int i = 2; i <= n; i++){
if(tid[i] == n) {
ans[i] = a[n] - a[i] + a[n] - a[1];
continue;
}
int l = 1, r = i - 1;
while(l + 1 < r){
int mid = (l + r) >> 1;
if(st_ask(mid, i - 1) > tid[i]){
l = mid;
}
else{
r = mid;
}
}
int pos = 0;
for(int j = r; j >= l; j--){
if(st_ask(j, i - 1) > tid[i]){
pos = j;
break;
}
}
ans[i] = ans[pos] + a[tid[i]] - a[i] + a[tid[i]] - a[pos];
}
q = read();
while(q--){
int x = read();
if(x >= a[n]){
cout << ans[n] + (x - a[n])<< '\n';
continue;
}
if(x <= a[1]){
cout << ans[1] + (a[1] - x) << '\n';
continue;
}
int tmp = lower_bound(a + 1, a + 1 + n, x) - a;
if(a[tmp] - x < x - a[tmp - 1]){
cout << ans[tmp] + a[tmp] - x << '\n';
}
else{
cout << ans[tmp - 1] + (x - a[tmp - 1]) << '\n';
}
}
return 0;
}