Submission #1476079
Source Code Expand
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lc (o << 1)
#define rc (o << 1 | 1)
#define mid ((l+r) >> 1)
#define int long long
using namespace std;
inline int read(){
char ch = getchar(); int x = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9') { x = x*10 + ch-'0'; ch = getchar(); }
return x;
}
const int maxn = 200009;
int L[maxn<<2], R[maxn<<2], lazyL[maxn<<2], lazyR[maxn<<2], x[maxn];
int n, Q, A, B, cost, ans = 1e18;
void build(int T[], int op, int o, int l, int r){
if (l == r) { T[o] = abs(l-B) + op*l; return; }
build(T, op, lc, l, mid); build(T, op, rc, mid+1, r);
T[o] = min(T[lc], T[rc]);
}
void pushdown(int T[], int lazy[], int o){
T[lc] += lazy[o]; lazy[lc] += lazy[o];
T[rc] += lazy[o]; lazy[rc] += lazy[o];
lazy[o] = 0;
}
void update(int T[], int lazy[], int o, int l, int r, int x, int y){
if (l == r) { T[o] = min(T[o], y); return; }
pushdown(T, lazy, o);
if (x <= mid) update(T, lazy, lc, l, mid, x, y);
else update(T, lazy, rc, mid+1, r, x, y);
T[o] = min(T[lc], T[rc]);
}
int query(int T[], int lazy[], int o, int l, int r, int x, int y){
if (l == x && y == r) return T[o];
pushdown(T, lazy, o); int res = 1e18;
if (x <= mid) res = min(res, query(T, lazy, lc, l, mid, x, min(mid, y)));
if (mid+1 <= y) res = min(res, query(T, lazy, rc, mid+1, r, max(mid+1, x), y));
return res;
}
signed main(){
n = read(); Q = read();
x[0] = A = read(); B = read();
for (int i=1; i<=Q; i++) x[i] = read();
build(L, -1, 1, 1, n); build(R, 1, 1, 1, n);
for (int i=1; i<=Q; i++){
cost = min(query(L, lazyL, 1, 1, n, 1, x[i]) + x[i],
query(R, lazyR, 1, 1, n, x[i], n) - x[i]);
L[1] += abs(x[i]-x[i-1]); lazyL[1] += abs(x[i]-x[i-1]);
R[1] += abs(x[i]-x[i-1]); lazyR[1] += abs(x[i]-x[i-1]);
update(L, lazyL, 1, 1, n, x[i-1], cost-x[i-1]);
update(R, lazyR, 1, 1, n, x[i-1], cost+x[i-1]);
}
for (int i=1; i<=n; i++)
ans = min(ans, query(L, lazyL, 1, 1, n, i, i) + i);
printf("%lld", ans);
return 0;
}
Submission Info
Submission Time |
|
Task |
F - Many Moves |
User |
Cyanic |
Language |
C++14 (GCC 5.4.1) |
Score |
900 |
Code Size |
2066 Byte |
Status |
AC |
Exec Time |
230 ms |
Memory |
24704 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
900 / 900 |
Status |
|
|
Set Name |
Test Cases |
Sample |
example0, example1, example2, example3 |
All |
example0, example1, example2, example3, flower0, flower1, flower2, flower3, flower4, flower5, flower6, flower7, flower8, flower9, maxrand0, maxrand1, maxrand2, maxrand3, maxrand4, maxrand5, maxrand6, maxrand7, maxrand8, maxrand9, smallrand0, smallrand1, smallrand2, smallrand3, smallrand4, smallrand5, smallrand6, smallrand7, smallrand8, smallrand9 |
Case Name |
Status |
Exec Time |
Memory |
example0 |
AC |
2 ms |
6272 KB |
example1 |
AC |
1 ms |
6272 KB |
example2 |
AC |
1 ms |
6272 KB |
example3 |
AC |
2 ms |
6272 KB |
flower0 |
AC |
220 ms |
24704 KB |
flower1 |
AC |
220 ms |
24704 KB |
flower2 |
AC |
223 ms |
24704 KB |
flower3 |
AC |
220 ms |
24704 KB |
flower4 |
AC |
220 ms |
24704 KB |
flower5 |
AC |
222 ms |
24704 KB |
flower6 |
AC |
220 ms |
24704 KB |
flower7 |
AC |
220 ms |
24704 KB |
flower8 |
AC |
221 ms |
24704 KB |
flower9 |
AC |
221 ms |
24704 KB |
maxrand0 |
AC |
230 ms |
24704 KB |
maxrand1 |
AC |
227 ms |
24704 KB |
maxrand2 |
AC |
227 ms |
24704 KB |
maxrand3 |
AC |
228 ms |
24704 KB |
maxrand4 |
AC |
227 ms |
24704 KB |
maxrand5 |
AC |
227 ms |
24704 KB |
maxrand6 |
AC |
226 ms |
24704 KB |
maxrand7 |
AC |
226 ms |
24704 KB |
maxrand8 |
AC |
227 ms |
24704 KB |
maxrand9 |
AC |
226 ms |
24704 KB |
smallrand0 |
AC |
1 ms |
6272 KB |
smallrand1 |
AC |
1 ms |
6272 KB |
smallrand2 |
AC |
1 ms |
6272 KB |
smallrand3 |
AC |
1 ms |
6272 KB |
smallrand4 |
AC |
2 ms |
6272 KB |
smallrand5 |
AC |
1 ms |
6272 KB |
smallrand6 |
AC |
2 ms |
6272 KB |
smallrand7 |
AC |
2 ms |
6272 KB |
smallrand8 |
AC |
1 ms |
6272 KB |
smallrand9 |
AC |
2 ms |
6272 KB |