Submission #1872274


Source Code Expand

#include <cstdio>
#include <algorithm>
int N, Q, A, last;
long long tag[524288], as[524288], ds[524288];
inline void down(int p)
{
	if (tag[p])
	{
		as[p << 1] += tag[p];
		ds[p << 1] += tag[p];
		tag[p << 1] += tag[p];
		as[p << 1 | 1] += tag[p];
		ds[p << 1 | 1] += tag[p];
		tag[p << 1 | 1] += tag[p];
		tag[p] = 0;
	}
}
inline void up(int p)
{
	as[p] = std::min(as[p << 1], as[p << 1 | 1]);
	ds[p] = std::min(ds[p << 1], ds[p << 1 | 1]);
}
void build(int p, int l, int r)
{
	if (l == r)
	{
		long long w = l == A ? 0 : 1LL << 60;
		as[p] = w + l;
		ds[p] = w - l;
		return;
	}
	int m = l + r >> 1;
	build(p << 1, l, m);
	build(p << 1 | 1, m + 1, r);
	as[p] = std::min(as[p << 1], as[p << 1 | 1]);
	ds[p] = std::min(ds[p << 1], ds[p << 1 | 1]);
}
inline int abs(int x)
{
	return x < 0 ? -x : x;
}
long long G(int p, int l, int r, int L, int R, long long *s)
{
	if (L <= l && r <= R)
		return s[p];
	int m = l + r >> 1;
	down(p);
	if (R <= m)
		return G(p << 1, l, m, L, R, s);
	if (L > m)
		return G(p << 1 | 1, m + 1, r, L, R, s);
	return std::min(G(p << 1, l, m, L, R, s), G(p << 1 | 1, m + 1, r, L, R, s));
}
void DFS(int p, int l, int r, long long &O)
{
	if (l == r)
	{
		O = std::min(O, ds[p] + l);
		return;
	}
	int m = l + r >> 1;
	down(p);
	DFS(p << 1, l, m, O);
	DFS(p << 1 | 1, m + 1, r, O);
}
int main()
{
	scanf("%d%d%d%d", &N, &Q, &A, &last);
	build(1, 1, N);
	for (int i = 1, x; i <= Q; i++)
	{
		scanf("%d", &x);
		long long ILoveYou = std::min(G(1, 1, N, 1, x, ds) + x, G(1, 1, N, x, N, as) - x);
		tag[1] += abs(last - x);
		int p = 1, l = 1, r = N;
		while (l < r)
		{
			int m = l + r >> 1;
			down(p);
			if (last <= m)
				p <<= 1, r = m;
			else
				p = p << 1 | 1, l = m + 1;
		}
		as[p] = ILoveYou + last;
		ds[p] = ILoveYou - last;
		while (p >>= 1)
			up(p);
		last = x;
	}
	long long O = 1LL << 60;
	DFS(1, 1, N, O);
	printf("%lld\n", O);
	return 0;
}

Submission Info

Submission Time
Task F - Many Moves
User NiroBC
Language C++14 (GCC 5.4.1)
Score 900
Code Size 1977 Byte
Status AC
Exec Time 194 ms
Memory 12416 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:68:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d", &N, &Q, &A, &last);
                                      ^
./Main.cpp:72:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &x);
                  ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 900 / 900
Status
AC × 4
AC × 34
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 1 ms 4224 KB
example1 AC 1 ms 4224 KB
example2 AC 1 ms 4224 KB
example3 AC 1 ms 4224 KB
flower0 AC 179 ms 12416 KB
flower1 AC 180 ms 12416 KB
flower2 AC 179 ms 12416 KB
flower3 AC 180 ms 12416 KB
flower4 AC 179 ms 12416 KB
flower5 AC 179 ms 12416 KB
flower6 AC 180 ms 12416 KB
flower7 AC 180 ms 12416 KB
flower8 AC 181 ms 12416 KB
flower9 AC 183 ms 12416 KB
maxrand0 AC 187 ms 12416 KB
maxrand1 AC 188 ms 12416 KB
maxrand2 AC 189 ms 12416 KB
maxrand3 AC 188 ms 12416 KB
maxrand4 AC 188 ms 12416 KB
maxrand5 AC 187 ms 12416 KB
maxrand6 AC 190 ms 12416 KB
maxrand7 AC 190 ms 12416 KB
maxrand8 AC 194 ms 12416 KB
maxrand9 AC 191 ms 12416 KB
smallrand0 AC 1 ms 4224 KB
smallrand1 AC 1 ms 4224 KB
smallrand2 AC 1 ms 4224 KB
smallrand3 AC 1 ms 4224 KB
smallrand4 AC 1 ms 4224 KB
smallrand5 AC 1 ms 4224 KB
smallrand6 AC 1 ms 4224 KB
smallrand7 AC 1 ms 4224 KB
smallrand8 AC 1 ms 4224 KB
smallrand9 AC 1 ms 4224 KB