#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int BASE = 1 << 18;
constexpr ll oo = 2e9;
pair<ll, ll> TREE[BASE<<1][2]; //0 to z lewej/ 1 z prawej
set<ll> S;
vector<pair<ll, ll>> queries;
ll ans[BASE];
ll nxt[BASE][2]; //0 to z lewej/ 1 z prawej
ll a[BASE];
int n, q;
void update(int v, pair<int, int> val, bool c){
v += BASE;
TREE[v][c] = val;
v/=2;
while(v > 0){
if(!c) TREE[v][c] = min(TREE[2*v][c], TREE[2*v+1][c]);
else TREE[v][c] = max(TREE[2*v][c], TREE[2*v+1][c]);
v/=2;
}
}
void compute(){
//z lewej
for(int i = 1; i < n; i++){
ll roznica = a[i+1] - a[i];
auto it = S.lower_bound((a[i]-roznica));
nxt[i][0] = (*it);
}
nxt[n][0] = a[1];
//z prawej
for(int i = n; i > 1; i--){
ll roznica = a[i] - a[i-1];
auto it = S.lower_bound((a[i]+roznica-1));
if((*it) >= a[i]+roznica || it == S.end()) --it;
nxt[i][1] = (*it);
}
nxt[1][1] = a[n];
nxt[0][0] = oo;
for(int i = n+1; i < BASE; i++)
nxt[i][0] = oo;
for(int i = 0; i < BASE; i++)
update(i, {nxt[i][0], a[i]}, 0);
}
int wsk = 1;
void przesun(ll x){
while(x > a[wsk]){
if(wsk < n) update(wsk, {oo, 0}, 0);
update(wsk, {nxt[wsk][1], a[wsk]}, 1);
wsk++;
}
if(x == a[wsk] || x < a[1])
update(wsk, {nxt[wsk][1], a[wsk]}, 1);
}
pair<ll, ll> leftquery(int v, int l, int r, ll x){ //chcemy sc jak najbardziej w lewo
if(l == r) return TREE[v][0];
int mid = (l+r)/2;
if(TREE[2*v][0].first < x || TREE[2*v][0].first == a[1])
return leftquery(2*v, l, mid, x);
return leftquery(2*v+1, mid+1, r, x);
}
pair<ll, ll> rightquery(int v, int l, int r, ll x){ // chcemy isc jak najbardziej w prawo
if(l == r) return TREE[v][1];
int mid = (l+r)/2;
if(TREE[2*v+1][1].first > x || TREE[2*v+1][1].first == a[n])
return rightquery(2*v+1, mid+1, r, x);
return rightquery(2*v, l, mid, x);
}
ll check(ll x){
ll l = max(x, a[1]), r = min(x, a[n]), act = x;
ll res = 0;
bool strona = 0;
if(x-a[wsk-1] <= a[wsk]-x){
l = a[wsk-1];
act = l;
res += x - a[wsk-1];
strona ^=1;
}
else{
r = a[wsk];
act = r;
res += a[wsk]-x;
}
while(l > a[1] || r < a[n]){
pair<ll, ll> tmp;
if(!strona){
tmp = leftquery(1, 0, BASE-1, l);
if(l != tmp.first){
//cout << tmp.first << " " << tmp.second << "\n";
res += abs(tmp.second-act);
res += abs(tmp.second - tmp.first);
act = tmp.first;
r = max({r, tmp.first, tmp.second});
l = min({l, tmp.second, tmp.first});
}
}
else{
tmp = rightquery(1, 0, BASE-1, r);
if(r != tmp.first){
res += abs(tmp.second-act);
res += abs(tmp.second - tmp.first);
act = tmp.first;
r = max({r, tmp.first, tmp.second});
l = min({l, tmp.second, tmp.first});
}
}
//cout << "l : " << l << " r: " << r << " RES : " << res << "\n";
strona ^= 1;
}
//cout << "\n";
return res;
}
void solve(){
for(auto [u, idx] : queries){
przesun(u);
ans[idx] = check(u);
}
}
int main(){
//ios_base::sync_with_stdio(0);
//cin.tie(0);
cin >> n;
a[0] = -oo;
for(int i = 1; i <= n; i++){
cin >> a[i];
S.insert(a[i]);
}
a[n+1] = oo;
compute();
cin >> q;
for(int i = 1; i <= q; i++){
int x;
cin >> x;
queries.push_back({x, i});
}
sort(queries.begin(), queries.end());
/*
for(int i = 1; i <= n; i++)
cout << nxt[i][0] << " ";
cout << "\n";
for(int i = 1; i <= n; i++)
cout << nxt[i][1] << " ";
cout << "\n";
*/
solve();
for(int i = 1; i <= q; i++)
cout << ans[i] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
24664 KB |
Output is correct |
2 |
Correct |
31 ms |
24916 KB |
Output is correct |
3 |
Correct |
31 ms |
24896 KB |
Output is correct |
4 |
Correct |
30 ms |
24924 KB |
Output is correct |
5 |
Correct |
31 ms |
24924 KB |
Output is correct |
6 |
Correct |
32 ms |
25176 KB |
Output is correct |
7 |
Correct |
31 ms |
24916 KB |
Output is correct |
8 |
Correct |
30 ms |
24668 KB |
Output is correct |
9 |
Correct |
30 ms |
24796 KB |
Output is correct |
10 |
Correct |
29 ms |
24796 KB |
Output is correct |
11 |
Correct |
30 ms |
24660 KB |
Output is correct |
12 |
Correct |
30 ms |
24656 KB |
Output is correct |
13 |
Correct |
31 ms |
24656 KB |
Output is correct |
14 |
Correct |
32 ms |
24932 KB |
Output is correct |
15 |
Correct |
32 ms |
24912 KB |
Output is correct |
16 |
Correct |
30 ms |
24920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
24664 KB |
Output is correct |
2 |
Correct |
31 ms |
24916 KB |
Output is correct |
3 |
Correct |
31 ms |
24896 KB |
Output is correct |
4 |
Correct |
30 ms |
24924 KB |
Output is correct |
5 |
Correct |
31 ms |
24924 KB |
Output is correct |
6 |
Correct |
32 ms |
25176 KB |
Output is correct |
7 |
Correct |
31 ms |
24916 KB |
Output is correct |
8 |
Correct |
30 ms |
24668 KB |
Output is correct |
9 |
Correct |
30 ms |
24796 KB |
Output is correct |
10 |
Correct |
29 ms |
24796 KB |
Output is correct |
11 |
Correct |
30 ms |
24660 KB |
Output is correct |
12 |
Correct |
30 ms |
24656 KB |
Output is correct |
13 |
Correct |
31 ms |
24656 KB |
Output is correct |
14 |
Correct |
32 ms |
24932 KB |
Output is correct |
15 |
Correct |
32 ms |
24912 KB |
Output is correct |
16 |
Correct |
30 ms |
24920 KB |
Output is correct |
17 |
Correct |
187 ms |
36176 KB |
Output is correct |
18 |
Correct |
162 ms |
36240 KB |
Output is correct |
19 |
Correct |
177 ms |
36272 KB |
Output is correct |
20 |
Correct |
178 ms |
36180 KB |
Output is correct |
21 |
Correct |
194 ms |
36184 KB |
Output is correct |
22 |
Correct |
155 ms |
36156 KB |
Output is correct |
23 |
Correct |
160 ms |
36180 KB |
Output is correct |
24 |
Correct |
180 ms |
36176 KB |
Output is correct |
25 |
Correct |
170 ms |
36536 KB |
Output is correct |
26 |
Correct |
193 ms |
36180 KB |
Output is correct |
27 |
Correct |
169 ms |
36148 KB |
Output is correct |
28 |
Correct |
172 ms |
36512 KB |
Output is correct |
29 |
Correct |
165 ms |
36148 KB |
Output is correct |
30 |
Correct |
174 ms |
36204 KB |
Output is correct |
31 |
Correct |
166 ms |
36180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
24792 KB |
Output is correct |
2 |
Correct |
34 ms |
24656 KB |
Output is correct |
3 |
Correct |
33 ms |
24804 KB |
Output is correct |
4 |
Correct |
30 ms |
24664 KB |
Output is correct |
5 |
Correct |
30 ms |
24804 KB |
Output is correct |
6 |
Correct |
30 ms |
24724 KB |
Output is correct |
7 |
Correct |
285 ms |
41424 KB |
Output is correct |
8 |
Correct |
291 ms |
42020 KB |
Output is correct |
9 |
Correct |
277 ms |
42116 KB |
Output is correct |
10 |
Correct |
272 ms |
41720 KB |
Output is correct |
11 |
Correct |
274 ms |
43032 KB |
Output is correct |
12 |
Correct |
272 ms |
42788 KB |
Output is correct |
13 |
Correct |
121 ms |
32712 KB |
Output is correct |
14 |
Correct |
92 ms |
29588 KB |
Output is correct |
15 |
Correct |
269 ms |
42620 KB |
Output is correct |
16 |
Correct |
292 ms |
42700 KB |
Output is correct |
17 |
Correct |
273 ms |
42696 KB |
Output is correct |
18 |
Correct |
119 ms |
32184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
24664 KB |
Output is correct |
2 |
Correct |
31 ms |
24916 KB |
Output is correct |
3 |
Correct |
31 ms |
24896 KB |
Output is correct |
4 |
Correct |
30 ms |
24924 KB |
Output is correct |
5 |
Correct |
31 ms |
24924 KB |
Output is correct |
6 |
Correct |
32 ms |
25176 KB |
Output is correct |
7 |
Correct |
31 ms |
24916 KB |
Output is correct |
8 |
Correct |
30 ms |
24668 KB |
Output is correct |
9 |
Correct |
30 ms |
24796 KB |
Output is correct |
10 |
Correct |
29 ms |
24796 KB |
Output is correct |
11 |
Correct |
30 ms |
24660 KB |
Output is correct |
12 |
Correct |
30 ms |
24656 KB |
Output is correct |
13 |
Correct |
31 ms |
24656 KB |
Output is correct |
14 |
Correct |
32 ms |
24932 KB |
Output is correct |
15 |
Correct |
32 ms |
24912 KB |
Output is correct |
16 |
Correct |
30 ms |
24920 KB |
Output is correct |
17 |
Correct |
187 ms |
36176 KB |
Output is correct |
18 |
Correct |
162 ms |
36240 KB |
Output is correct |
19 |
Correct |
177 ms |
36272 KB |
Output is correct |
20 |
Correct |
178 ms |
36180 KB |
Output is correct |
21 |
Correct |
194 ms |
36184 KB |
Output is correct |
22 |
Correct |
155 ms |
36156 KB |
Output is correct |
23 |
Correct |
160 ms |
36180 KB |
Output is correct |
24 |
Correct |
180 ms |
36176 KB |
Output is correct |
25 |
Correct |
170 ms |
36536 KB |
Output is correct |
26 |
Correct |
193 ms |
36180 KB |
Output is correct |
27 |
Correct |
169 ms |
36148 KB |
Output is correct |
28 |
Correct |
172 ms |
36512 KB |
Output is correct |
29 |
Correct |
165 ms |
36148 KB |
Output is correct |
30 |
Correct |
174 ms |
36204 KB |
Output is correct |
31 |
Correct |
166 ms |
36180 KB |
Output is correct |
32 |
Correct |
30 ms |
24792 KB |
Output is correct |
33 |
Correct |
34 ms |
24656 KB |
Output is correct |
34 |
Correct |
33 ms |
24804 KB |
Output is correct |
35 |
Correct |
30 ms |
24664 KB |
Output is correct |
36 |
Correct |
30 ms |
24804 KB |
Output is correct |
37 |
Correct |
30 ms |
24724 KB |
Output is correct |
38 |
Correct |
285 ms |
41424 KB |
Output is correct |
39 |
Correct |
291 ms |
42020 KB |
Output is correct |
40 |
Correct |
277 ms |
42116 KB |
Output is correct |
41 |
Correct |
272 ms |
41720 KB |
Output is correct |
42 |
Correct |
274 ms |
43032 KB |
Output is correct |
43 |
Correct |
272 ms |
42788 KB |
Output is correct |
44 |
Correct |
121 ms |
32712 KB |
Output is correct |
45 |
Correct |
92 ms |
29588 KB |
Output is correct |
46 |
Correct |
269 ms |
42620 KB |
Output is correct |
47 |
Correct |
292 ms |
42700 KB |
Output is correct |
48 |
Correct |
273 ms |
42696 KB |
Output is correct |
49 |
Correct |
119 ms |
32184 KB |
Output is correct |
50 |
Correct |
295 ms |
44476 KB |
Output is correct |
51 |
Correct |
293 ms |
43960 KB |
Output is correct |
52 |
Correct |
295 ms |
43632 KB |
Output is correct |
53 |
Correct |
288 ms |
44212 KB |
Output is correct |
54 |
Correct |
281 ms |
43980 KB |
Output is correct |
55 |
Correct |
282 ms |
43628 KB |
Output is correct |
56 |
Correct |
297 ms |
43568 KB |
Output is correct |
57 |
Correct |
271 ms |
43448 KB |
Output is correct |
58 |
Correct |
275 ms |
43960 KB |
Output is correct |