Submission #305615

#TimeUsernameProblemLanguageResultExecution timeMemory
305615youngyojunAbduction 2 (JOI17_abduction2)C++17
100 / 100
2185 ms174096 KiB
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define revv(V) reverse(allv(V))
#define sz(V) ((int)(V).size())
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
 
const int MAXH = 50005;
const int MAXW = 50005;
 
unordered_map<ll, ll> MP;
 
int AU[17][MAXH], AD[17][MAXH];
int BL[17][MAXW], BR[17][MAXW];
 
int A[MAXH], B[MAXW];
 
int H, W, Q;
 
ll f(int y, int x, int dr) {
	ll key = (((ll(y) << 17) | x) << 3) | dr;
	auto it = MP.find(key);
	if(MP.end() != it) return it->second;
 
	if(!dr) {
		int Y = y;
		for(int i = 17; i--;) if(AU[i][Y] < B[x])
			Y = min(H, Y + (1<<i));
		Y++;
		if(A[Y] < B[x]) return MP[key] = H-y;
		ll ret = max(f(Y, x, 1), f(Y, x, 3)) + (Y-y);
		return MP[key] = ret;
	} else if(2 == dr) {
		int Y = y;
		for(int i = 17; i--;) if(AD[i][Y] < B[x])
			Y = max(1, Y - (1<<i));
		Y--;
		if(A[Y] < B[x]) return MP[key] = y-1;
		ll ret = max(f(Y, x, 1), f(Y, x, 3)) + (y-Y);
		return MP[key] = ret;
	} else if(1 == dr) {
		int X = x;
		for(int i = 17; i--;) if(BR[i][X] < A[y])
			X = min(W, X + (1<<i));
		X++;
		if(B[X] < A[y]) return MP[key] = W-x;
		ll ret = max(f(y, X, 0), f(y, X, 2)) + (X-x);
		return MP[key] = ret;
	} else {
		int X = x;
		for(int i = 17; i--;) if(BL[i][X] < A[y])
			X = max(1, X - (1<<i));
		X--;
		if(B[X] < A[y]) return MP[key] = x-1;
		ll ret = max(f(y, X, 0), f(y, X, 2)) + (x-X);
		return MP[key] = ret;
	}
}
 
int main() {
	ios::sync_with_stdio(false);
 
	cin >> H >> W >> Q;
	for(int i = 1; i <= H; i++) cin >> A[i];
	for(int i = 1; i <= W; i++) cin >> B[i];
 
	for(int i = 1; i <= H; i++) {
		AU[0][i] = A[i+1];
		AD[0][i] = A[i-1];
	}
	for(int i = 1; i <= W; i++) {
		BL[0][i] = B[i-1];
		BR[0][i] = B[i+1];
	}
 
	for(int j = 0; j < 16; j++) {
		for(int i = 1; i <= H; i++) {
			AU[j+1][i] = AU[j][i];
			if(i + (1<<j) <= H) upmax(AU[j+1][i], AU[j][i + (1<<j)]);
			AD[j+1][i] = AD[j][i];
			if(0 < i - (1<<j)) upmax(AD[j+1][i], AD[j][i - (1<<j)]);
		}
		for(int i = 1; i <= W; i++) {
			BL[j+1][i] = BL[j][i];
			if(0 < i - (1<<j)) upmax(BL[j+1][i], BL[j][i - (1<<j)]);
			BR[j+1][i] = BR[j][i];
			if(i + (1<<j) <= W) upmax(BR[j+1][i], BR[j][i + (1<<j)]);
		}
	}
 
	for(int y, x; Q--;) {
		cin >> y >> x;
		ll ret = 0;
		for(int dr = 0; dr < 4; dr++)
			upmax(ret, f(y, x, dr));
		printf("%lld\n", ret);
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...