Submission #172327

# Submission time Handle Problem Language Result Execution time Memory
172327 2020-01-01T09:03:32 Z maruii Abduction 2 (JOI17_abduction2) C++14
13 / 100
5000 ms 824 KB
#include <bits/stdc++.h>
using namespace std;

int H, W, Q;
int A[50005], B[50005];
int pfa[50005], sfa[50005], pfb[50005], sfb[50005];
int D[2002][2002][2];

const int SIZE = 1 << 16;
struct ST {
	int A[2 * SIZE];
	void update(int x, int v) {
		for (x += SIZE; x; x >>= 1) A[x] = max(A[x], v);
	}
	int query(int s, int e) {
		int ret = 0;
		for (s += SIZE, e += SIZE; s <= e; s >>= 1, e >>= 1) {
			if ( s & 1) ret = max(ret, A[s++]);
			if (~e & 1) ret = max(ret, A[e--]);
		}
		return ret;
	}
} a, b;

int f(int x, int y, int d) {
	if (!x || !y) return -2e9;
	int &ret = D[x][y][d];
	if (ret) return ret;
	if (d == 0) {
		if (max(pfa[x - 1], sfa[x + 1]) < B[y]) return ret = max(x - 1, H - x);
		int p, q;
		{
			int l = x + 1, r = H;
			if (sfa[x + 1] < B[y]) p = 0;
			else {
				while (l < r) {
					int m = l + r >> 1;
					if (a.query(x + 1, m) > B[y]) r = m;
					else l = m + 1;
				}
				p = l;
			}
		}
		{
			int l = 1, r = x - 1;
			if (pfa[x - 1] < B[y]) q = 0;
			else {
				while (l < r) {
					int m = l + r + 1 >> 1;
					if (a.query(m, x - 1) > B[y]) l = m;
					else r = m - 1;
				}
				q = r;
			}
		}
		return max(abs(x - p) + f(p, y, 1), abs(x - q) + f(q, y, 1));
	}
	else {
		if (max(pfb[y - 1], sfb[y + 1]) < A[x]) return ret = max(y - 1, W - y);
		int p, q;
		{
			int l = y + 1, r = W;
			if (sfb[y + 1] < A[x]) p = 0;
			else {
				while (l < r) {
					int m = l + r >> 1;
					if (b.query(y + 1, m) > A[x]) r = m;
					else l = m + 1;
				}
				p = l;
			}
		}
		{
			int l = 1, r = y - 1;
			if (pfb[y - 1] < A[x]) q = 0;
			else {
				while (l < r) {
					int m = l + r + 1 >> 1;
					if (b.query(m, y - 1) > A[x]) l = m;
					else r = m - 1;
				}
				q = r;
			}
		}
		return max(abs(y - p) + f(x, p, 0), abs(y - q) + f(x, q, 0));
	}
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> H >> W >> Q;
	if (H > 2000 || W > 2000) assert(0);
	for (int i = 1; i <= H; ++i) cin >> A[i], a.update(i, A[i]);
	for (int i = 1; i <= W; ++i) cin >> B[i], b.update(i, B[i]);

	for (int i = 1; i <= H; ++i) pfa[i] = max(pfa[i - 1], A[i]);
	for (int i = 1; i <= W; ++i) pfb[i] = max(pfb[i - 1], B[i]);
	for (int i = H; i; --i) sfa[i] = max(sfa[i + 1], A[i]);
	for (int i = W; i; --i) sfb[i] = max(sfb[i + 1], B[i]);

	for (int i = 0; i < Q; ++i) {
		int x, y; cin >> x >> y;
		printf("%d\n", max(f(x, y, 0), f(x, y, 1)));
	}
	return 0;
}

Compilation message

abduction2.cpp: In function 'int f(int, int, int)':
abduction2.cpp:37:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
      int m = l + r >> 1;
              ~~^~~
abduction2.cpp:49:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
      int m = l + r + 1 >> 1;
              ~~~~~~^~~
abduction2.cpp:66:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
      int m = l + r >> 1;
              ~~^~~
abduction2.cpp:78:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
      int m = l + r + 1 >> 1;
              ~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 496 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 476 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 496 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 476 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 3 ms 504 KB Output is correct
13 Correct 3 ms 504 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 504 KB Output is correct
16 Correct 3 ms 504 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 504 KB Output is correct
19 Execution timed out 5004 ms 504 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 496 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 476 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 3 ms 504 KB Output is correct
13 Correct 3 ms 504 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 504 KB Output is correct
16 Correct 3 ms 504 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 504 KB Output is correct
19 Execution timed out 5004 ms 504 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 71 ms 656 KB Output is correct
2 Correct 42 ms 632 KB Output is correct
3 Correct 54 ms 760 KB Output is correct
4 Correct 59 ms 824 KB Output is correct
5 Correct 66 ms 760 KB Output is correct
6 Correct 74 ms 632 KB Output is correct
7 Correct 68 ms 712 KB Output is correct
8 Execution timed out 5089 ms 632 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 496 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 476 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 3 ms 504 KB Output is correct
13 Correct 3 ms 504 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 504 KB Output is correct
16 Correct 3 ms 504 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 504 KB Output is correct
19 Execution timed out 5004 ms 504 KB Time limit exceeded
20 Halted 0 ms 0 KB -