#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int H,W;
int A[50000],B[50000];
map<pii,LLI> dp;
LLI findAns(int x,int y) {
if ((x < 0) || (x >= W) || (y < 0) || (y >= H)) return -1;
else if (dp.count(mp(x,y))) return dp[mp(x,y)];
int i;
LLI ans;
if (A[y] < B[x]) {
for (i = y; i >= 0; i--) {
if (A[i] > B[x]) break;
}
ans = findAns(x,i)+abs(i-y);
for (i = y; i < H; i++) {
if (A[i] > B[x]) break;
}
ans = max(ans,findAns(x,i)+abs(i-y));
}
else {
for (i = x; i >= 0; i--) {
if (B[i] > A[y]) break;
}
ans = findAns(i,y)+abs(i-x);
for (i = x; i < W; i++) {
if (B[i] > A[y]) break;
}
ans = max(ans,findAns(i,y)+abs(i-x));
}
dp[mp(x,y)] = ans;
return ans;
}
int main() {
int i,j;
int Q,S,T;
scanf("%d %d %d",&H,&W,&Q);
for (i = 0; i < H; i++) scanf("%d",&A[i]);
for (i = 0; i < W; i++) scanf("%d",&B[i]);
for (i = 0; i < Q; i++) {
scanf("%d %d",&S,&T);
S--,T--;
LLI ans = 0;
for (j = S-1; j >= 0; j--) {
if (A[j] > B[T]) break;
}
ans = max(ans,findAns(T,j)+abs(j-S));
for (j = S+1; j < H; j++) {
if (A[j] > B[T]) break;
}
ans = max(ans,findAns(T,j)+abs(j-S));
for (j = T-1; j >= 0; j--) {
if (B[j] > A[S]) break;
}
ans = max(ans,findAns(j,S)+abs(j-T));
for (j = T+1; j < W; j++) {
if (B[j] > A[S]) break;
}
ans = max(ans,findAns(j,S)+abs(j-T));
printf("%lld\n",ans);
}
return 0;
}
Compilation message
abduction2.cpp: In function 'int main()':
abduction2.cpp:93:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | scanf("%d %d %d",&H,&W,&Q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:94:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | for (i = 0; i < H; i++) scanf("%d",&A[i]);
| ~~~~~^~~~~~~~~~~~
abduction2.cpp:95:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | for (i = 0; i < W; i++) scanf("%d",&B[i]);
| ~~~~~^~~~~~~~~~~~
abduction2.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | scanf("%d %d",&S,&T);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
2 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
5 ms |
460 KB |
Output is correct |
20 |
Correct |
6 ms |
632 KB |
Output is correct |
21 |
Correct |
5 ms |
460 KB |
Output is correct |
22 |
Correct |
8 ms |
840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
2 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
5 ms |
460 KB |
Output is correct |
20 |
Correct |
6 ms |
632 KB |
Output is correct |
21 |
Correct |
5 ms |
460 KB |
Output is correct |
22 |
Correct |
8 ms |
840 KB |
Output is correct |
23 |
Correct |
17 ms |
588 KB |
Output is correct |
24 |
Correct |
14 ms |
588 KB |
Output is correct |
25 |
Correct |
14 ms |
660 KB |
Output is correct |
26 |
Correct |
15 ms |
576 KB |
Output is correct |
27 |
Correct |
14 ms |
668 KB |
Output is correct |
28 |
Correct |
539 ms |
6196 KB |
Output is correct |
29 |
Correct |
126 ms |
1436 KB |
Output is correct |
30 |
Correct |
1473 ms |
7184 KB |
Output is correct |
31 |
Correct |
1721 ms |
9120 KB |
Output is correct |
32 |
Correct |
136 ms |
1016 KB |
Output is correct |
33 |
Correct |
573 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
460 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
588 KB |
Output is correct |
7 |
Correct |
3 ms |
460 KB |
Output is correct |
8 |
Correct |
8 ms |
716 KB |
Output is correct |
9 |
Correct |
10 ms |
696 KB |
Output is correct |
10 |
Correct |
7 ms |
716 KB |
Output is correct |
11 |
Correct |
11 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
2 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
5 ms |
460 KB |
Output is correct |
20 |
Correct |
6 ms |
632 KB |
Output is correct |
21 |
Correct |
5 ms |
460 KB |
Output is correct |
22 |
Correct |
8 ms |
840 KB |
Output is correct |
23 |
Correct |
17 ms |
588 KB |
Output is correct |
24 |
Correct |
14 ms |
588 KB |
Output is correct |
25 |
Correct |
14 ms |
660 KB |
Output is correct |
26 |
Correct |
15 ms |
576 KB |
Output is correct |
27 |
Correct |
14 ms |
668 KB |
Output is correct |
28 |
Correct |
539 ms |
6196 KB |
Output is correct |
29 |
Correct |
126 ms |
1436 KB |
Output is correct |
30 |
Correct |
1473 ms |
7184 KB |
Output is correct |
31 |
Correct |
1721 ms |
9120 KB |
Output is correct |
32 |
Correct |
136 ms |
1016 KB |
Output is correct |
33 |
Correct |
573 ms |
2680 KB |
Output is correct |
34 |
Correct |
3 ms |
460 KB |
Output is correct |
35 |
Correct |
3 ms |
460 KB |
Output is correct |
36 |
Correct |
3 ms |
460 KB |
Output is correct |
37 |
Correct |
3 ms |
460 KB |
Output is correct |
38 |
Correct |
3 ms |
460 KB |
Output is correct |
39 |
Correct |
3 ms |
588 KB |
Output is correct |
40 |
Correct |
3 ms |
460 KB |
Output is correct |
41 |
Correct |
8 ms |
716 KB |
Output is correct |
42 |
Correct |
10 ms |
696 KB |
Output is correct |
43 |
Correct |
7 ms |
716 KB |
Output is correct |
44 |
Correct |
11 ms |
844 KB |
Output is correct |
45 |
Correct |
23 ms |
952 KB |
Output is correct |
46 |
Correct |
23 ms |
844 KB |
Output is correct |
47 |
Correct |
22 ms |
864 KB |
Output is correct |
48 |
Correct |
22 ms |
896 KB |
Output is correct |
49 |
Correct |
23 ms |
936 KB |
Output is correct |
50 |
Correct |
635 ms |
5444 KB |
Output is correct |
51 |
Correct |
631 ms |
6212 KB |
Output is correct |
52 |
Correct |
2009 ms |
11120 KB |
Output is correct |
53 |
Correct |
1914 ms |
10772 KB |
Output is correct |
54 |
Correct |
1933 ms |
10296 KB |
Output is correct |
55 |
Correct |
2797 ms |
15576 KB |
Output is correct |
56 |
Correct |
4162 ms |
126820 KB |
Output is correct |
57 |
Correct |
1333 ms |
33900 KB |
Output is correct |
58 |
Correct |
1291 ms |
32276 KB |
Output is correct |
59 |
Correct |
1262 ms |
32176 KB |
Output is correct |