#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
struct maxseg
{
vector<int> tree;
maxseg(int N)
{
int sz = 1 << ((int)ceil(log2(N+1))+1);
tree.resize(sz);
}
int init(int s, int e, int node, vector<int> &a)
{
if (s == e) return tree[node] = a[s];
return tree[node] = max(init(s, (s+e)/2, 2*node, a), init((s+e)/2+1, e, 2*node+1, a));
}
int lft(int s, int e, int node, int id, int x)
{
if (tree[node] < x) return -1;
if (id < s) return -1;
if (s == e) return s;
int tmp = lft((s+e)/2+1, e, 2*node+1, id, x);
if (tmp == -1) return lft(s, (s+e)/2, 2*node, id, x);
else return tmp;
}
};
struct minseg
{
vector<int> tree;
minseg(int N)
{
int sz = 1 << ((int)ceil(log2(N+1))+1);
tree.resize(sz);
}
int init(int s, int e, int node, vector<int> &a)
{
if (s == e) return tree[node] = a[s];
return tree[node] = min(init(s, (s+e)/2, 2*node, a), init((s+e)/2+1, e, 2*node+1, a));
}
int rht(int s, int e, int node, int id, int x)
{
if (tree[node] > x) return -1;
if (e < id) return -1;
if (s == e) return s;
int tmp = rht(s, (s+e)/2, 2*node, id, x);
if (tmp == -1) return rht((s+e)/2+1, e, 2*node+1, id, x);
else return tmp;
}
};
const long long inf = 1e10;
long long X[200005];
long long dis[200005];
int N, Q;
int go[200005][2];
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N;
X[0] = -inf, X[N+1] = inf;
for (int i = 1; i <= N; i++) cin >> X[i];
for (int i = 1; i <= N; i++) {
go[i][0] = lower_bound(X, X+N+2, 2*X[i]-X[i-1]) - X - 1;
go[i][1] = lower_bound(X, X+N+2, 2*X[i]-X[i+1]) - X;
}
vector<int> mv[2];
mv[0].resize(N+1); mv[1].resize(N+1);
for (int i = 1; i <= N; i++) mv[0][i] = go[i][0];
for (int i = 1; i <= N; i++) mv[1][i] = go[i][1];
minseg rseg(N);
maxseg lseg(N);
rseg.init(1, N, 1, mv[1]);
lseg.init(1, N, 1, mv[0]);
for (int i = 1; i <= N; i++) {
int curl = i, curr = i;
bool dir = false;
while (1) {
if (curl <= 1 && curr >= N) break;
if (!dir) {
int stop = lseg.lft(1, N, 1, curl, curr+1);
if (stop == -1) stop = 1;
dis[i] += X[curl] - X[stop];
if (curr == N) break;
dis[i] += X[curr+1] - X[stop];
curl = stop; curr++; dir = true;
}
else {
int stop = rseg.rht(1, N, 1, curr, curl-1);
if (stop == -1) stop = N;
dis[i] += X[stop] - X[curr];
if (curl == 1) break;
dis[i] += X[stop] - X[curl-1];
curr = stop; curl--; dir = false;
}
}
}
cin >> Q;
while (Q--) {
long long s; cin >> s;
int id = lower_bound(X+1, X+N+1, s) - X;
int nxt = (X[id] - s < s - X[id-1]) ? id : (id-1);
cout << abs(s - X[nxt]) + dis[nxt] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
428 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
428 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
100 ms |
10680 KB |
Output is correct |
18 |
Correct |
99 ms |
10592 KB |
Output is correct |
19 |
Correct |
97 ms |
10604 KB |
Output is correct |
20 |
Correct |
96 ms |
10588 KB |
Output is correct |
21 |
Correct |
96 ms |
10596 KB |
Output is correct |
22 |
Correct |
102 ms |
10572 KB |
Output is correct |
23 |
Correct |
94 ms |
10652 KB |
Output is correct |
24 |
Correct |
86 ms |
10684 KB |
Output is correct |
25 |
Correct |
89 ms |
10568 KB |
Output is correct |
26 |
Correct |
87 ms |
10692 KB |
Output is correct |
27 |
Correct |
87 ms |
10580 KB |
Output is correct |
28 |
Correct |
97 ms |
10732 KB |
Output is correct |
29 |
Correct |
124 ms |
10640 KB |
Output is correct |
30 |
Correct |
129 ms |
10692 KB |
Output is correct |
31 |
Correct |
129 ms |
10692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
110 ms |
12020 KB |
Output is correct |
8 |
Correct |
112 ms |
11972 KB |
Output is correct |
9 |
Correct |
110 ms |
12020 KB |
Output is correct |
10 |
Correct |
109 ms |
12004 KB |
Output is correct |
11 |
Correct |
111 ms |
12092 KB |
Output is correct |
12 |
Correct |
110 ms |
12060 KB |
Output is correct |
13 |
Correct |
33 ms |
2260 KB |
Output is correct |
14 |
Correct |
24 ms |
844 KB |
Output is correct |
15 |
Correct |
126 ms |
12504 KB |
Output is correct |
16 |
Correct |
127 ms |
12416 KB |
Output is correct |
17 |
Correct |
127 ms |
12400 KB |
Output is correct |
18 |
Correct |
33 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
428 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
100 ms |
10680 KB |
Output is correct |
18 |
Correct |
99 ms |
10592 KB |
Output is correct |
19 |
Correct |
97 ms |
10604 KB |
Output is correct |
20 |
Correct |
96 ms |
10588 KB |
Output is correct |
21 |
Correct |
96 ms |
10596 KB |
Output is correct |
22 |
Correct |
102 ms |
10572 KB |
Output is correct |
23 |
Correct |
94 ms |
10652 KB |
Output is correct |
24 |
Correct |
86 ms |
10684 KB |
Output is correct |
25 |
Correct |
89 ms |
10568 KB |
Output is correct |
26 |
Correct |
87 ms |
10692 KB |
Output is correct |
27 |
Correct |
87 ms |
10580 KB |
Output is correct |
28 |
Correct |
97 ms |
10732 KB |
Output is correct |
29 |
Correct |
124 ms |
10640 KB |
Output is correct |
30 |
Correct |
129 ms |
10692 KB |
Output is correct |
31 |
Correct |
129 ms |
10692 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 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 |
110 ms |
12020 KB |
Output is correct |
39 |
Correct |
112 ms |
11972 KB |
Output is correct |
40 |
Correct |
110 ms |
12020 KB |
Output is correct |
41 |
Correct |
109 ms |
12004 KB |
Output is correct |
42 |
Correct |
111 ms |
12092 KB |
Output is correct |
43 |
Correct |
110 ms |
12060 KB |
Output is correct |
44 |
Correct |
33 ms |
2260 KB |
Output is correct |
45 |
Correct |
24 ms |
844 KB |
Output is correct |
46 |
Correct |
126 ms |
12504 KB |
Output is correct |
47 |
Correct |
127 ms |
12416 KB |
Output is correct |
48 |
Correct |
127 ms |
12400 KB |
Output is correct |
49 |
Correct |
33 ms |
2260 KB |
Output is correct |
50 |
Correct |
160 ms |
12980 KB |
Output is correct |
51 |
Correct |
153 ms |
12888 KB |
Output is correct |
52 |
Correct |
151 ms |
12856 KB |
Output is correct |
53 |
Correct |
152 ms |
13004 KB |
Output is correct |
54 |
Correct |
145 ms |
12840 KB |
Output is correct |
55 |
Correct |
145 ms |
12876 KB |
Output is correct |
56 |
Correct |
166 ms |
12768 KB |
Output is correct |
57 |
Correct |
159 ms |
12764 KB |
Output is correct |
58 |
Correct |
159 ms |
12756 KB |
Output is correct |