답안 #201215

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
201215 2020-02-09T19:44:39 Z maruii 유괴 2 (JOI17_abduction2) C++14
100 / 100
1616 ms 128632 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];
map<tuple<int, int, int>, long long> D;
 
const int SIZE = 1 << 16;
struct ST {
	int A[2 * SIZE];
	vector<int> a, b;
	void update(int x, int v) {
		for (x += SIZE; x; x >>= 1) A[x] = max(A[x], v);
	}
	int query(int o, int s, int e, int v) {
		a.clear(), b.clear();
		for (s += SIZE, e += SIZE; s <= e; s >>=1, e >>= 1) {
			if ( s & 1) a.push_back(s), ++s;
			if (~e & 1) b.push_back(e), --e;
		}
		a.insert(a.end(), b.rbegin(), b.rend());
		if (!o) reverse(a.begin(), a.end());
		for (auto i : a) if (A[i] > v) {
			while (i < SIZE) {
				i <<= 1;
				if (o == 1) { if (A[i] < v) i |= 1; }
				else {if (A[i | 1] > v) i |= 1;}
			}
			return i - SIZE;
		}
		return 0;
	}
} a, b;
 
long long f(int x, int y, int d) {
	if (!x || !y || x > H || y > W) return -1;
	long long &ret = D[{x, y, d}];
	if (ret) return ret;
	if (d == 0) {
		int p, q;
		if (sfa[x + 1] < B[y]) p = H + 1;
		else p = a.query(1, x + 1, H, B[y]);
		if (pfa[x - 1] < B[y]) q = 0;
		else q = a.query(0, 1, x - 1, B[y]);
		return ret = max(abs(x - p) + f(p, y, 1), abs(x - q) + f(q, y, 1));
	}
	else {
		int p, q;
		if (sfb[y + 1] < A[x]) p = W + 1;
		else p = b.query(1, y + 1, W, A[x]);
		if (pfb[y - 1] < A[x]) q = 0;
		else q = b.query(0, 1, y - 1, A[x]);
		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;
	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("%lld\n", max(f(x, y, 0), f(x, y, 1)));
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 6 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 6 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 6 ms 504 KB Output is correct
14 Correct 6 ms 504 KB Output is correct
15 Correct 6 ms 632 KB Output is correct
16 Correct 6 ms 504 KB Output is correct
17 Correct 6 ms 504 KB Output is correct
18 Correct 6 ms 632 KB Output is correct
19 Correct 7 ms 760 KB Output is correct
20 Correct 8 ms 888 KB Output is correct
21 Correct 7 ms 888 KB Output is correct
22 Correct 9 ms 1144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 6 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 6 ms 504 KB Output is correct
14 Correct 6 ms 504 KB Output is correct
15 Correct 6 ms 632 KB Output is correct
16 Correct 6 ms 504 KB Output is correct
17 Correct 6 ms 504 KB Output is correct
18 Correct 6 ms 632 KB Output is correct
19 Correct 7 ms 760 KB Output is correct
20 Correct 8 ms 888 KB Output is correct
21 Correct 7 ms 888 KB Output is correct
22 Correct 9 ms 1144 KB Output is correct
23 Correct 26 ms 3320 KB Output is correct
24 Correct 25 ms 3320 KB Output is correct
25 Correct 24 ms 3320 KB Output is correct
26 Correct 23 ms 3320 KB Output is correct
27 Correct 26 ms 3320 KB Output is correct
28 Correct 38 ms 9976 KB Output is correct
29 Correct 25 ms 4216 KB Output is correct
30 Correct 83 ms 10360 KB Output is correct
31 Correct 97 ms 12280 KB Output is correct
32 Correct 25 ms 3704 KB Output is correct
33 Correct 40 ms 5496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 760 KB Output is correct
2 Correct 7 ms 664 KB Output is correct
3 Correct 8 ms 760 KB Output is correct
4 Correct 8 ms 760 KB Output is correct
5 Correct 8 ms 760 KB Output is correct
6 Correct 7 ms 888 KB Output is correct
7 Correct 7 ms 764 KB Output is correct
8 Correct 11 ms 1016 KB Output is correct
9 Correct 10 ms 1144 KB Output is correct
10 Correct 11 ms 1016 KB Output is correct
11 Correct 10 ms 1144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 6 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 6 ms 504 KB Output is correct
14 Correct 6 ms 504 KB Output is correct
15 Correct 6 ms 632 KB Output is correct
16 Correct 6 ms 504 KB Output is correct
17 Correct 6 ms 504 KB Output is correct
18 Correct 6 ms 632 KB Output is correct
19 Correct 7 ms 760 KB Output is correct
20 Correct 8 ms 888 KB Output is correct
21 Correct 7 ms 888 KB Output is correct
22 Correct 9 ms 1144 KB Output is correct
23 Correct 26 ms 3320 KB Output is correct
24 Correct 25 ms 3320 KB Output is correct
25 Correct 24 ms 3320 KB Output is correct
26 Correct 23 ms 3320 KB Output is correct
27 Correct 26 ms 3320 KB Output is correct
28 Correct 38 ms 9976 KB Output is correct
29 Correct 25 ms 4216 KB Output is correct
30 Correct 83 ms 10360 KB Output is correct
31 Correct 97 ms 12280 KB Output is correct
32 Correct 25 ms 3704 KB Output is correct
33 Correct 40 ms 5496 KB Output is correct
34 Correct 8 ms 760 KB Output is correct
35 Correct 7 ms 664 KB Output is correct
36 Correct 8 ms 760 KB Output is correct
37 Correct 8 ms 760 KB Output is correct
38 Correct 8 ms 760 KB Output is correct
39 Correct 7 ms 888 KB Output is correct
40 Correct 7 ms 764 KB Output is correct
41 Correct 11 ms 1016 KB Output is correct
42 Correct 10 ms 1144 KB Output is correct
43 Correct 11 ms 1016 KB Output is correct
44 Correct 10 ms 1144 KB Output is correct
45 Correct 27 ms 3576 KB Output is correct
46 Correct 28 ms 3576 KB Output is correct
47 Correct 26 ms 3576 KB Output is correct
48 Correct 25 ms 3580 KB Output is correct
49 Correct 28 ms 3576 KB Output is correct
50 Correct 47 ms 8824 KB Output is correct
51 Correct 47 ms 9592 KB Output is correct
52 Correct 137 ms 14200 KB Output is correct
53 Correct 127 ms 13688 KB Output is correct
54 Correct 125 ms 13432 KB Output is correct
55 Correct 179 ms 18936 KB Output is correct
56 Correct 1616 ms 128632 KB Output is correct
57 Correct 360 ms 35832 KB Output is correct
58 Correct 382 ms 34424 KB Output is correct
59 Correct 372 ms 34040 KB Output is correct