#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("VNOICUP.INP");
ofstream fout("VNOICUP.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
using u128 = __uint128_t;
//const int x[4] = {1, -1, 0, 0};
//const int y[4] = {0, 0, 1, -1};
const ll mod = 998244353;
const int mxn = 2e5 + 5, mxq = 2e5 + 5, sq = 400, mxv = 2e7 + 5;
//const int base = (1 << 18);
const ll inf = 2e9 + 5, neg = -69420;
int n;
ll x[mxn + 1], mx[mxn + 1][18], mn[mxn + 1][18];
ll getmn(int l, int r){
int lg = __lg(r - l + 1);
return(min(mn[l][lg], mn[r - (1 << lg) + 1][lg]));
}
ll getmx(int l, int r){
int lg = __lg(r - l + 1);
return(max(mx[l][lg], mx[r - (1 << lg) + 1][lg]));
}
void solve(ll s){
int id = lower_bound(x, x + n + 1, s) - x;
ll res, l, r;
if(abs(x[id - 1] - s) <= abs(x[id] - s)){
res = abs(x[id - 1] - s); l = r = id - 1;
}else{
l = r = id; res = x[id] - s;
}
bool turn = 1;
while(l > 1 || r < n){
if(turn){
// go left
int le = 1, ri = l - 1, ans = l;
while(le <= ri){
int mid = (le + ri) >> 1;
if(getmx(mid, l - 1) <= x[r + 1]){
ans = mid; ri = mid - 1;
}else{
le = mid + 1;
}
}
res += x[l] - x[ans];
if(r < n)res += x[++r] - x[ans];
l = ans;
}else{
// go right
int le = r + 1, ri = n, ans = r;
while(le <= ri){
int mid = (le + ri) >> 1;
if(getmn(r, mid - 1) > x[l - 1]){
ans = mid; le = mid + 1;
}else{
ri = mid - 1;
}
}
res += x[ans] - x[r];
if(l > 1)res += x[ans] - x[--l];
r = ans;
}
turn = !turn;
}
cout << res << "\n";
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> x[i];
}
x[0] = -inf; x[n + 1] = inf;
for(int i = 0; i < n; i++){
mx[i][0] = 2 * x[i + 1] - x[i]; mn[i][0] = -x[i + 1] + 2 * x[i];
}
for(int i = 1; i < 18; i++){
for(int j = 0; j + (1 << i) - 1 < n; j++){
mx[j][i] = max(mx[j][i - 1], mx[j + (1 << (i - 1))][i - 1]);
mn[j][i] = min(mn[j][i - 1], mn[j + (1 << (i - 1))][i - 1]);
}
}
int q; cin >> q;
while(q--){
int s; cin >> s;
solve(s);
}
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
864 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
864 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
868 KB |
Output is correct |
17 |
Correct |
77 ms |
60184 KB |
Output is correct |
18 |
Correct |
94 ms |
60180 KB |
Output is correct |
19 |
Correct |
79 ms |
60136 KB |
Output is correct |
20 |
Correct |
98 ms |
60176 KB |
Output is correct |
21 |
Correct |
80 ms |
60088 KB |
Output is correct |
22 |
Correct |
76 ms |
60160 KB |
Output is correct |
23 |
Correct |
78 ms |
60184 KB |
Output is correct |
24 |
Correct |
77 ms |
60076 KB |
Output is correct |
25 |
Correct |
103 ms |
60200 KB |
Output is correct |
26 |
Correct |
76 ms |
60108 KB |
Output is correct |
27 |
Correct |
75 ms |
60176 KB |
Output is correct |
28 |
Correct |
76 ms |
60176 KB |
Output is correct |
29 |
Correct |
101 ms |
60124 KB |
Output is correct |
30 |
Correct |
77 ms |
60256 KB |
Output is correct |
31 |
Correct |
89 ms |
60124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
218 ms |
59592 KB |
Output is correct |
8 |
Correct |
234 ms |
62128 KB |
Output is correct |
9 |
Correct |
226 ms |
62148 KB |
Output is correct |
10 |
Correct |
234 ms |
62088 KB |
Output is correct |
11 |
Correct |
229 ms |
62072 KB |
Output is correct |
12 |
Correct |
244 ms |
62164 KB |
Output is correct |
13 |
Correct |
32 ms |
4124 KB |
Output is correct |
14 |
Correct |
35 ms |
1536 KB |
Output is correct |
15 |
Correct |
237 ms |
63164 KB |
Output is correct |
16 |
Correct |
238 ms |
63212 KB |
Output is correct |
17 |
Correct |
233 ms |
63172 KB |
Output is correct |
18 |
Correct |
45 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
864 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
868 KB |
Output is correct |
17 |
Correct |
77 ms |
60184 KB |
Output is correct |
18 |
Correct |
94 ms |
60180 KB |
Output is correct |
19 |
Correct |
79 ms |
60136 KB |
Output is correct |
20 |
Correct |
98 ms |
60176 KB |
Output is correct |
21 |
Correct |
80 ms |
60088 KB |
Output is correct |
22 |
Correct |
76 ms |
60160 KB |
Output is correct |
23 |
Correct |
78 ms |
60184 KB |
Output is correct |
24 |
Correct |
77 ms |
60076 KB |
Output is correct |
25 |
Correct |
103 ms |
60200 KB |
Output is correct |
26 |
Correct |
76 ms |
60108 KB |
Output is correct |
27 |
Correct |
75 ms |
60176 KB |
Output is correct |
28 |
Correct |
76 ms |
60176 KB |
Output is correct |
29 |
Correct |
101 ms |
60124 KB |
Output is correct |
30 |
Correct |
77 ms |
60256 KB |
Output is correct |
31 |
Correct |
89 ms |
60124 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
218 ms |
59592 KB |
Output is correct |
39 |
Correct |
234 ms |
62128 KB |
Output is correct |
40 |
Correct |
226 ms |
62148 KB |
Output is correct |
41 |
Correct |
234 ms |
62088 KB |
Output is correct |
42 |
Correct |
229 ms |
62072 KB |
Output is correct |
43 |
Correct |
244 ms |
62164 KB |
Output is correct |
44 |
Correct |
32 ms |
4124 KB |
Output is correct |
45 |
Correct |
35 ms |
1536 KB |
Output is correct |
46 |
Correct |
237 ms |
63164 KB |
Output is correct |
47 |
Correct |
238 ms |
63212 KB |
Output is correct |
48 |
Correct |
233 ms |
63172 KB |
Output is correct |
49 |
Correct |
45 ms |
4172 KB |
Output is correct |
50 |
Correct |
523 ms |
64308 KB |
Output is correct |
51 |
Correct |
453 ms |
64320 KB |
Output is correct |
52 |
Correct |
463 ms |
64244 KB |
Output is correct |
53 |
Correct |
352 ms |
64244 KB |
Output is correct |
54 |
Correct |
419 ms |
64244 KB |
Output is correct |
55 |
Correct |
358 ms |
64300 KB |
Output is correct |
56 |
Correct |
164 ms |
64176 KB |
Output is correct |
57 |
Correct |
163 ms |
64140 KB |
Output is correct |
58 |
Correct |
184 ms |
64168 KB |
Output is correct |