Submission #414746

# Submission time Handle Problem Language Result Execution time Memory
414746 2021-05-31T06:23:34 Z amoo_safar Abduction 2 (JOI17_abduction2) C++17
100 / 100
4930 ms 326696 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'
#define int ll

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;
}

int32_t 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;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 9 ms 11980 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 9 ms 11980 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 7 ms 11968 KB Output is correct
8 Correct 9 ms 11980 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 9 ms 11980 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 9 ms 11980 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 7 ms 11968 KB Output is correct
8 Correct 9 ms 11980 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
12 Correct 7 ms 12020 KB Output is correct
13 Correct 7 ms 12108 KB Output is correct
14 Correct 8 ms 12008 KB Output is correct
15 Correct 7 ms 12024 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 8 ms 12108 KB Output is correct
18 Correct 7 ms 12108 KB Output is correct
19 Correct 11 ms 12492 KB Output is correct
20 Correct 14 ms 12748 KB Output is correct
21 Correct 11 ms 12648 KB Output is correct
22 Correct 15 ms 13116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 9 ms 11980 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 9 ms 11980 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 7 ms 11968 KB Output is correct
8 Correct 9 ms 11980 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
12 Correct 7 ms 12020 KB Output is correct
13 Correct 7 ms 12108 KB Output is correct
14 Correct 8 ms 12008 KB Output is correct
15 Correct 7 ms 12024 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 8 ms 12108 KB Output is correct
18 Correct 7 ms 12108 KB Output is correct
19 Correct 11 ms 12492 KB Output is correct
20 Correct 14 ms 12748 KB Output is correct
21 Correct 11 ms 12648 KB Output is correct
22 Correct 15 ms 13116 KB Output is correct
23 Correct 20 ms 12760 KB Output is correct
24 Correct 20 ms 12748 KB Output is correct
25 Correct 20 ms 12748 KB Output is correct
26 Correct 25 ms 12804 KB Output is correct
27 Correct 25 ms 12796 KB Output is correct
28 Correct 55 ms 22516 KB Output is correct
29 Correct 24 ms 14156 KB Output is correct
30 Correct 193 ms 26960 KB Output is correct
31 Correct 235 ms 31020 KB Output is correct
32 Correct 27 ms 13508 KB Output is correct
33 Correct 59 ms 17212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12496 KB Output is correct
2 Correct 13 ms 12508 KB Output is correct
3 Correct 11 ms 12484 KB Output is correct
4 Correct 11 ms 12492 KB Output is correct
5 Correct 11 ms 12492 KB Output is correct
6 Correct 9 ms 12620 KB Output is correct
7 Correct 11 ms 12492 KB Output is correct
8 Correct 17 ms 13172 KB Output is correct
9 Correct 17 ms 13208 KB Output is correct
10 Correct 19 ms 13092 KB Output is correct
11 Correct 22 ms 13424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 9 ms 11980 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 9 ms 11980 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 7 ms 11968 KB Output is correct
8 Correct 9 ms 11980 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
12 Correct 7 ms 12020 KB Output is correct
13 Correct 7 ms 12108 KB Output is correct
14 Correct 8 ms 12008 KB Output is correct
15 Correct 7 ms 12024 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 8 ms 12108 KB Output is correct
18 Correct 7 ms 12108 KB Output is correct
19 Correct 11 ms 12492 KB Output is correct
20 Correct 14 ms 12748 KB Output is correct
21 Correct 11 ms 12648 KB Output is correct
22 Correct 15 ms 13116 KB Output is correct
23 Correct 20 ms 12760 KB Output is correct
24 Correct 20 ms 12748 KB Output is correct
25 Correct 20 ms 12748 KB Output is correct
26 Correct 25 ms 12804 KB Output is correct
27 Correct 25 ms 12796 KB Output is correct
28 Correct 55 ms 22516 KB Output is correct
29 Correct 24 ms 14156 KB Output is correct
30 Correct 193 ms 26960 KB Output is correct
31 Correct 235 ms 31020 KB Output is correct
32 Correct 27 ms 13508 KB Output is correct
33 Correct 59 ms 17212 KB Output is correct
34 Correct 11 ms 12496 KB Output is correct
35 Correct 13 ms 12508 KB Output is correct
36 Correct 11 ms 12484 KB Output is correct
37 Correct 11 ms 12492 KB Output is correct
38 Correct 11 ms 12492 KB Output is correct
39 Correct 9 ms 12620 KB Output is correct
40 Correct 11 ms 12492 KB Output is correct
41 Correct 17 ms 13172 KB Output is correct
42 Correct 17 ms 13208 KB Output is correct
43 Correct 19 ms 13092 KB Output is correct
44 Correct 22 ms 13424 KB Output is correct
45 Correct 32 ms 13424 KB Output is correct
46 Correct 28 ms 13420 KB Output is correct
47 Correct 31 ms 13520 KB Output is correct
48 Correct 32 ms 13536 KB Output is correct
49 Correct 29 ms 13452 KB Output is correct
50 Correct 76 ms 22464 KB Output is correct
51 Correct 80 ms 23532 KB Output is correct
52 Correct 353 ms 37440 KB Output is correct
53 Correct 363 ms 36628 KB Output is correct
54 Correct 326 ms 35140 KB Output is correct
55 Correct 472 ms 47132 KB Output is correct
56 Correct 4930 ms 326696 KB Output is correct
57 Correct 1134 ms 94688 KB Output is correct
58 Correct 1030 ms 90660 KB Output is correct
59 Correct 1040 ms 90092 KB Output is correct