답안 #414744

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
414744 2021-05-31T06:20:54 Z amoo_safar 유괴 2 (JOI17_abduction2) C++17
44 / 100
449 ms 35120 KB
// vaziat meshki-ghermeze !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 5e4 + 10;
const ll Inf = 2242545357980376863LL;
const int Log = 16;

typedef pair<int, int> pii;
typedef pair<int, pii> T; // 0 -> R, 1 -> L, 2 -> D, 3 -> U;

int h, w;
int H[Log][N], W[Log][N];

vector<T> V;
bool Valid(T mv){
	pii pos = mv.S;
	int dir = mv.F;
	if(pos.F == h && dir == 2) return false;
	if(pos.F == 0 && dir == 3) return false;
	if(pos.S == w && dir == 0) return false;
	if(pos.S == 0 && dir == 1) return false;
	return true;
}
int Go(T mv){
	pii pos = mv.S;
	int dir = mv.F;
	if(dir == 0)
		return w - pos.S;
	if(dir == 1)
		return pos.S - 1;
	if(dir == 2)
		return h - pos.F;
	return pos.F - 1;
}


void Move(T mv){
	pii pos = mv.S;
	int dir = mv.F;
	
	int nw, v;
	if(dir == 0){
		nw = pos.S + 1;
		v = H[0][pos.F];
		for(int l = Log - 1; l >= 0; l--){
			if(nw + (1 << l) - 1 > w) continue;
			if(W[l][nw] < v){
				nw += (1 << l);
			}
		}
		if(nw == w + 1) return ;
		pos = {pos.F, nw};
		assert(W[0][nw] > v);
		V.pb({2, pos}); V.pb({3, pos});
	}
	if(dir == 1){
		nw = pos.S - 1;
		v = H[0][pos.F];
		for(int l = Log - 1; l >= 0; l--){
			if(nw - (1 << l) < 0) continue;
			if(W[l][nw - (1 << l) + 1] < v){
				nw -= (1 << l);
			}
		}
		if(nw == 0) return ;
		assert(W[0][nw] > v);
		pos = {pos.F, nw};
		V.pb({2, pos}); V.pb({3, pos});
	}
	if(dir == 2){
		nw = pos.F + 1;
		v = W[0][pos.S];
		for(int l = Log - 1; l >= 0; l--){
			if(nw + (1 << l) - 1 > h) continue;
			if(H[l][nw] < v){
				nw += (1 << l);
			}
		}
		if(nw == h + 1) return ;
		assert(H[0][nw] > v);
		pos = {nw, pos.S};
		V.pb({0, pos}); V.pb({1, pos});
	}
	if(dir == 3){
		nw = pos.F - 1;
		v = W[0][pos.S];
		for(int l = Log - 1; l >= 0; l--){
			if(nw - (1 << l) < 0) continue;
			if(H[l][nw - (1 << l) + 1] < v){
				nw -= (1 << l);
			}
		}
		if(nw == 0) return ;
		pos = {nw, pos.S};
		assert(H[0][nw] > v);

		V.pb({0, pos}); V.pb({1, pos});
	}
}

map<T, int> mp;
int dis(pii A, pii B){ return abs(A.F - B.F) + abs(A.S - B.S); }

int Solve(T nw){
	// cerr << "!! " << nw.F << ' ' << nw.S.F << ' ' << nw.S.S << '\n';
	if(!Valid(nw)) return -(3 * N);
	if(mp.count(nw))
		return mp[nw];

	V.clear();
	Move(nw);
	vector<T> V2 = V;
	int res = 0;

	// cerr << "----\n";
	// cerr << "$ (" << nw.F << ", (" << nw.S.F << ", " << nw.S.S << ")) :" << "\n";
	// for(auto x : V2)
	// 	cerr << "!! (" << x.F << ", (" << x.S.F << ", " << x.S.S << "))" << "\n";
	// cerr << "----\n";
	if(V2.empty())
		res = Go(nw);

	for(auto x : V2){
		// if(dis(nw.S, x.S) == 0){
		// 	cout << "##\n";
		// 	cout << nw.F << ' ' << nw.S.F << ' ' << nw.S.S << '\n';
		// 	cout << x.F << ' ' << x.S.F << ' ' << x.S.S << '\n';
		// }
		res = max(res, Solve(x) + dis(nw.S, x.S));
	}
	return mp[nw] = res;
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int q;
	cin >> h >> w >> q;
	for(int i = 1; i <= h; i++) cin >> H[0][i];
	for(int i = 1; i <= w; i++) cin >> W[0][i];
	for(int l = 0; l + 1 < Log; l++){
		int st = (1 << l);
		memcpy(H[l + 1], H[l], sizeof H[l]);
		for(int i = 1; i + st <= h; i++)
			H[l + 1][i] = max(H[l + 1][i], H[l][i + st]);

		memcpy(W[l + 1], W[l], sizeof W[l]);
		for(int i = 1; i + st <= w; i++)
			W[l + 1][i] = max(W[l + 1][i], W[l][i + st]);
	}
	int x, y;

	// Solve({0, {2, 2}});
	// for(auto [nw, rs] : mp)
	// 	cerr << "!! (" << nw.F << ", (" << nw.S.F << ", " << nw.S.S << ")) :" << rs << "\n";
	// exit(0);
	for(int i = 0; i < q; i++){
		int res = 0;
		cin >> x >> y;
		for(int d = 0; d < 4; d++)
			res = max(res, Solve({d, {x, y}}));
		cout << res << '\n';
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6092 KB Output is correct
2 Correct 4 ms 6092 KB Output is correct
3 Correct 4 ms 6092 KB Output is correct
4 Correct 4 ms 6084 KB Output is correct
5 Correct 4 ms 6076 KB Output is correct
6 Correct 4 ms 6116 KB Output is correct
7 Correct 4 ms 6092 KB Output is correct
8 Correct 4 ms 6092 KB Output is correct
9 Correct 4 ms 6092 KB Output is correct
10 Correct 4 ms 6092 KB Output is correct
11 Correct 4 ms 6092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6092 KB Output is correct
2 Correct 4 ms 6092 KB Output is correct
3 Correct 4 ms 6092 KB Output is correct
4 Correct 4 ms 6084 KB Output is correct
5 Correct 4 ms 6076 KB Output is correct
6 Correct 4 ms 6116 KB Output is correct
7 Correct 4 ms 6092 KB Output is correct
8 Correct 4 ms 6092 KB Output is correct
9 Correct 4 ms 6092 KB Output is correct
10 Correct 4 ms 6092 KB Output is correct
11 Correct 4 ms 6092 KB Output is correct
12 Correct 4 ms 6220 KB Output is correct
13 Correct 6 ms 6220 KB Output is correct
14 Correct 5 ms 6136 KB Output is correct
15 Correct 5 ms 6140 KB Output is correct
16 Correct 5 ms 6188 KB Output is correct
17 Correct 5 ms 6220 KB Output is correct
18 Correct 4 ms 6220 KB Output is correct
19 Correct 8 ms 6544 KB Output is correct
20 Correct 10 ms 6732 KB Output is correct
21 Correct 9 ms 6604 KB Output is correct
22 Correct 13 ms 7120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6092 KB Output is correct
2 Correct 4 ms 6092 KB Output is correct
3 Correct 4 ms 6092 KB Output is correct
4 Correct 4 ms 6084 KB Output is correct
5 Correct 4 ms 6076 KB Output is correct
6 Correct 4 ms 6116 KB Output is correct
7 Correct 4 ms 6092 KB Output is correct
8 Correct 4 ms 6092 KB Output is correct
9 Correct 4 ms 6092 KB Output is correct
10 Correct 4 ms 6092 KB Output is correct
11 Correct 4 ms 6092 KB Output is correct
12 Correct 4 ms 6220 KB Output is correct
13 Correct 6 ms 6220 KB Output is correct
14 Correct 5 ms 6136 KB Output is correct
15 Correct 5 ms 6140 KB Output is correct
16 Correct 5 ms 6188 KB Output is correct
17 Correct 5 ms 6220 KB Output is correct
18 Correct 4 ms 6220 KB Output is correct
19 Correct 8 ms 6544 KB Output is correct
20 Correct 10 ms 6732 KB Output is correct
21 Correct 9 ms 6604 KB Output is correct
22 Correct 13 ms 7120 KB Output is correct
23 Correct 23 ms 7476 KB Output is correct
24 Correct 18 ms 7492 KB Output is correct
25 Correct 18 ms 7500 KB Output is correct
26 Correct 18 ms 7420 KB Output is correct
27 Correct 23 ms 7500 KB Output is correct
28 Correct 49 ms 15720 KB Output is correct
29 Correct 22 ms 8640 KB Output is correct
30 Correct 167 ms 19044 KB Output is correct
31 Correct 253 ms 22348 KB Output is correct
32 Correct 31 ms 8132 KB Output is correct
33 Correct 71 ms 11112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 6516 KB Output is correct
2 Correct 9 ms 6604 KB Output is correct
3 Correct 9 ms 6644 KB Output is correct
4 Correct 9 ms 6568 KB Output is correct
5 Correct 8 ms 6476 KB Output is correct
6 Correct 7 ms 6604 KB Output is correct
7 Correct 6 ms 6604 KB Output is correct
8 Correct 14 ms 7116 KB Output is correct
9 Correct 14 ms 7116 KB Output is correct
10 Correct 13 ms 6948 KB Output is correct
11 Correct 21 ms 7204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6092 KB Output is correct
2 Correct 4 ms 6092 KB Output is correct
3 Correct 4 ms 6092 KB Output is correct
4 Correct 4 ms 6084 KB Output is correct
5 Correct 4 ms 6076 KB Output is correct
6 Correct 4 ms 6116 KB Output is correct
7 Correct 4 ms 6092 KB Output is correct
8 Correct 4 ms 6092 KB Output is correct
9 Correct 4 ms 6092 KB Output is correct
10 Correct 4 ms 6092 KB Output is correct
11 Correct 4 ms 6092 KB Output is correct
12 Correct 4 ms 6220 KB Output is correct
13 Correct 6 ms 6220 KB Output is correct
14 Correct 5 ms 6136 KB Output is correct
15 Correct 5 ms 6140 KB Output is correct
16 Correct 5 ms 6188 KB Output is correct
17 Correct 5 ms 6220 KB Output is correct
18 Correct 4 ms 6220 KB Output is correct
19 Correct 8 ms 6544 KB Output is correct
20 Correct 10 ms 6732 KB Output is correct
21 Correct 9 ms 6604 KB Output is correct
22 Correct 13 ms 7120 KB Output is correct
23 Correct 23 ms 7476 KB Output is correct
24 Correct 18 ms 7492 KB Output is correct
25 Correct 18 ms 7500 KB Output is correct
26 Correct 18 ms 7420 KB Output is correct
27 Correct 23 ms 7500 KB Output is correct
28 Correct 49 ms 15720 KB Output is correct
29 Correct 22 ms 8640 KB Output is correct
30 Correct 167 ms 19044 KB Output is correct
31 Correct 253 ms 22348 KB Output is correct
32 Correct 31 ms 8132 KB Output is correct
33 Correct 71 ms 11112 KB Output is correct
34 Correct 9 ms 6516 KB Output is correct
35 Correct 9 ms 6604 KB Output is correct
36 Correct 9 ms 6644 KB Output is correct
37 Correct 9 ms 6568 KB Output is correct
38 Correct 8 ms 6476 KB Output is correct
39 Correct 7 ms 6604 KB Output is correct
40 Correct 6 ms 6604 KB Output is correct
41 Correct 14 ms 7116 KB Output is correct
42 Correct 14 ms 7116 KB Output is correct
43 Correct 13 ms 6948 KB Output is correct
44 Correct 21 ms 7204 KB Output is correct
45 Correct 27 ms 8036 KB Output is correct
46 Correct 25 ms 8004 KB Output is correct
47 Correct 26 ms 8008 KB Output is correct
48 Correct 25 ms 7988 KB Output is correct
49 Correct 25 ms 8056 KB Output is correct
50 Correct 73 ms 15420 KB Output is correct
51 Correct 75 ms 16452 KB Output is correct
52 Correct 391 ms 27324 KB Output is correct
53 Correct 333 ms 26692 KB Output is correct
54 Correct 331 ms 25604 KB Output is correct
55 Incorrect 449 ms 35120 KB Output isn't correct
56 Halted 0 ms 0 KB -