답안 #105721

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
105721 2019-04-14T04:25:38 Z Pro_ktmr 유괴 2 (JOI17_abduction2) C++14
100 / 100
4966 ms 129608 KB
#include"bits/stdc++.h"
using namespace std;
#define LL long long
#define PB push_back
#define MP make_pair

#define int long long

struct SegmentTree{
private:
	int N;
	vector<int> node;
public:
	void init(int n){
		N = 1;
		while(N < n) N *= 2;
		for(int i=0; i<2*N-1; i++) node.PB(0);
	}
	void update(int k, int x){
		k += N-1;
		node[k] = x;
		while(k > 0){
			k = (k-1)/2;
			node[k] = max(node[2*k+1], node[2*k+2]);
		}
	}
	int findL(int a, int b, int x, int k=0, int l=0, int r=-1){
		if(r == -1) r = N;
		if(r <= a || b <= l) return INT_MAX;
		if(r-l == 1){
			if(node[k] > x) return k-(N-1);
			else return INT_MAX;
		}
		if(a <= l && r <= b){
			if(node[2*k+1] > x) return findL(a, b, x, 2*k+1, l, (l+r)/2);
			if(node[2*k+2] > x) return findL(a, b, x, 2*k+2, (l+r)/2, r);
			return INT_MAX;
		}
		else{
			return min( findL(a, b, x, 2*k+1, l, (l+r)/2), findL(a, b, x, 2*k+2, (l+r)/2, r) );
		}
	}
	int findR(int a, int b, int x, int k=0, int l=0, int r=-1){
		if(r == -1) r = N;
		if(r <= a || b <= l) return INT_MIN;
		if(r-l == 1){
			if(node[k] > x) return k-(N-1);
			else return INT_MIN;
		}
		if(a <= l && r <= b){
			if(node[2*k+2] > x) return findR(a, b, x, 2*k+2, (l+r)/2, r);
			if(node[2*k+1] > x) return findR(a, b, x, 2*k+1, l, (l+r)/2);
			return INT_MIN;
		}
		else{
			return max( findR(a, b, x, 2*k+1, l, (l+r)/2), findR(a, b, x, 2*k+2, (l+r)/2, r) );
		}
	}
	void print(){
		for(int i=0; i<node.size(); i++) cout << node[i] << " ";
		cout << endl;
	}
};

int H,W,Q,A[50000],B[50000];
SegmentTree a,b;

map<pair<int,int>,int> dp;
/*
3
1 2
0
*/
int solve(int Y, int X, int moto){ //moto-10key
	if(moto == -1){
		int ans = 0;
		if(b.findR(0, X, A[Y]) != INT_MIN) ans = max(ans, solve(Y, b.findR(0, X, A[Y]), 2) +X-b.findR(0, X, A[Y]));
		else ans = max(ans, X);
		if(b.findL(X+1, W, A[Y]) != INT_MAX) ans = max(ans, solve(Y, b.findL(X+1, W, A[Y]), 1) +b.findL(X+1, W, A[Y])-X);
		else ans = max(ans, W-X-1);
		if(a.findR(0, Y, B[X]) != INT_MIN) ans = max(ans, solve(a.findR(0, Y, B[X]), X, 0) +Y-a.findR(0, Y, B[X]));
		else ans = max(ans, Y);
		if(a.findL(Y+1, H, B[X]) != INT_MAX) ans = max(ans, solve(a.findL(Y+1, H, B[X]), X, 3) +a.findL(Y+1, H, B[X])-Y);
		else ans = max(ans, H-Y-1);
		return ans;
	}
	if(dp.count({Y,X}) == 1) return dp[{Y,X}];
	int ans = 0;
	if(A[Y] > B[X]){
		if(b.findR(0, X, A[Y]) != INT_MIN) ans = max(ans, solve(Y, b.findR(0, X, A[Y]), 2) +X-b.findR(0, X, A[Y]));
		else ans = max(ans, X);
		if(b.findL(X+1, W, A[Y]) != INT_MAX) ans = max(ans, solve(Y, b.findL(X+1, W, A[Y]), 1) +b.findL(X+1, W, A[Y])-X);
		else ans = max(ans, W-X-1);
	}
	else{
		if(a.findR(0, Y, B[X]) != INT_MIN) ans = max(ans, solve(a.findR(0, Y, B[X]), X, 0) +Y-a.findR(0, Y, B[X]));
		else ans = max(ans, Y);
		if(a.findL(Y+1, H, B[X]) != INT_MAX) ans = max(ans, solve(a.findL(Y+1, H, B[X]), X, 3) +a.findL(Y+1, H, B[X])-Y);
		else ans = max(ans, H-Y-1);
	}
	//cout << Y << " " << X << " " << moto << " " << ans << endl;
	return dp[{Y,X}] = ans;
}

signed main(){
	cin >> H >> W >> Q;
	a.init(H);
	for(int i=0; i<H; i++){
		cin >> A[i];
		a.update(i, A[i]);
	}
	b.init(W);
	for(int i=0; i<W; i++){
		cin >> B[i];
		b.update(i, B[i]);
	}

	for(int i=0; i<Q; i++){
		int Y,X;
		cin >> Y >> X;
		Y--; X--;
		cout << solve(Y, X, -1) << endl;
	}

	return 0;
}

Compilation message

abduction2.cpp: In member function 'void SegmentTree::print()':
abduction2.cpp:60:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<node.size(); i++) cout << node[i] << " ";
                ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 3 ms 252 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 3 ms 252 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 6 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 4 ms 512 KB Output is correct
17 Correct 7 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 10 ms 640 KB Output is correct
20 Correct 10 ms 768 KB Output is correct
21 Correct 9 ms 768 KB Output is correct
22 Correct 11 ms 1024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 3 ms 252 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 6 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 4 ms 512 KB Output is correct
17 Correct 7 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 10 ms 640 KB Output is correct
20 Correct 10 ms 768 KB Output is correct
21 Correct 9 ms 768 KB Output is correct
22 Correct 11 ms 1024 KB Output is correct
23 Correct 56 ms 4224 KB Output is correct
24 Correct 77 ms 4204 KB Output is correct
25 Correct 62 ms 4324 KB Output is correct
26 Correct 76 ms 4200 KB Output is correct
27 Correct 70 ms 4196 KB Output is correct
28 Correct 163 ms 11368 KB Output is correct
29 Correct 81 ms 5248 KB Output is correct
30 Correct 248 ms 11368 KB Output is correct
31 Correct 362 ms 13524 KB Output is correct
32 Correct 64 ms 4588 KB Output is correct
33 Correct 113 ms 6512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 640 KB Output is correct
2 Correct 15 ms 632 KB Output is correct
3 Correct 14 ms 640 KB Output is correct
4 Correct 14 ms 640 KB Output is correct
5 Correct 14 ms 640 KB Output is correct
6 Correct 8 ms 748 KB Output is correct
7 Correct 7 ms 768 KB Output is correct
8 Correct 17 ms 1016 KB Output is correct
9 Correct 14 ms 1024 KB Output is correct
10 Correct 16 ms 896 KB Output is correct
11 Correct 20 ms 1016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 3 ms 252 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 6 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 4 ms 512 KB Output is correct
17 Correct 7 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 10 ms 640 KB Output is correct
20 Correct 10 ms 768 KB Output is correct
21 Correct 9 ms 768 KB Output is correct
22 Correct 11 ms 1024 KB Output is correct
23 Correct 56 ms 4224 KB Output is correct
24 Correct 77 ms 4204 KB Output is correct
25 Correct 62 ms 4324 KB Output is correct
26 Correct 76 ms 4200 KB Output is correct
27 Correct 70 ms 4196 KB Output is correct
28 Correct 163 ms 11368 KB Output is correct
29 Correct 81 ms 5248 KB Output is correct
30 Correct 248 ms 11368 KB Output is correct
31 Correct 362 ms 13524 KB Output is correct
32 Correct 64 ms 4588 KB Output is correct
33 Correct 113 ms 6512 KB Output is correct
34 Correct 11 ms 640 KB Output is correct
35 Correct 15 ms 632 KB Output is correct
36 Correct 14 ms 640 KB Output is correct
37 Correct 14 ms 640 KB Output is correct
38 Correct 14 ms 640 KB Output is correct
39 Correct 8 ms 748 KB Output is correct
40 Correct 7 ms 768 KB Output is correct
41 Correct 17 ms 1016 KB Output is correct
42 Correct 14 ms 1024 KB Output is correct
43 Correct 16 ms 896 KB Output is correct
44 Correct 20 ms 1016 KB Output is correct
45 Correct 74 ms 4460 KB Output is correct
46 Correct 90 ms 4456 KB Output is correct
47 Correct 82 ms 4452 KB Output is correct
48 Correct 105 ms 4456 KB Output is correct
49 Correct 77 ms 4424 KB Output is correct
50 Correct 143 ms 9972 KB Output is correct
51 Correct 156 ms 10784 KB Output is correct
52 Correct 407 ms 15292 KB Output is correct
53 Correct 361 ms 14716 KB Output is correct
54 Correct 474 ms 14444 KB Output is correct
55 Correct 496 ms 20112 KB Output is correct
56 Correct 4966 ms 129608 KB Output is correct
57 Correct 1296 ms 36676 KB Output is correct
58 Correct 1336 ms 35056 KB Output is correct
59 Correct 1297 ms 34924 KB Output is correct