Submission #172328

# Submission time Handle Problem Language Result Execution time Memory
172328 2020-01-01T09:04:30 Z maruii Abduction 2 (JOI17_abduction2) C++14
27 / 100
27 ms 14456 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 ret = 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 ret = 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 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 508 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 380 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 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 508 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 380 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 632 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 632 KB Output is correct
16 Correct 3 ms 632 KB Output is correct
17 Correct 3 ms 632 KB Output is correct
18 Correct 4 ms 1144 KB Output is correct
19 Correct 11 ms 5240 KB Output is correct
20 Correct 15 ms 7928 KB Output is correct
21 Correct 12 ms 6136 KB Output is correct
22 Correct 21 ms 11128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 508 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 380 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 632 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 632 KB Output is correct
16 Correct 3 ms 632 KB Output is correct
17 Correct 3 ms 632 KB Output is correct
18 Correct 4 ms 1144 KB Output is correct
19 Correct 11 ms 5240 KB Output is correct
20 Correct 15 ms 7928 KB Output is correct
21 Correct 12 ms 6136 KB Output is correct
22 Correct 21 ms 11128 KB Output is correct
23 Runtime error 4 ms 632 KB Execution killed with signal 11 (could be triggered by violating memory limits)
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3064 KB Output is correct
2 Correct 8 ms 3192 KB Output is correct
3 Correct 8 ms 3064 KB Output is correct
4 Correct 9 ms 3192 KB Output is correct
5 Correct 8 ms 3064 KB Output is correct
6 Correct 9 ms 5112 KB Output is correct
7 Correct 9 ms 5240 KB Output is correct
8 Correct 23 ms 12024 KB Output is correct
9 Correct 22 ms 11640 KB Output is correct
10 Correct 22 ms 11384 KB Output is correct
11 Correct 27 ms 14456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 508 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 380 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 632 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 632 KB Output is correct
16 Correct 3 ms 632 KB Output is correct
17 Correct 3 ms 632 KB Output is correct
18 Correct 4 ms 1144 KB Output is correct
19 Correct 11 ms 5240 KB Output is correct
20 Correct 15 ms 7928 KB Output is correct
21 Correct 12 ms 6136 KB Output is correct
22 Correct 21 ms 11128 KB Output is correct
23 Runtime error 4 ms 632 KB Execution killed with signal 11 (could be triggered by violating memory limits)
24 Halted 0 ms 0 KB -