#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF ((int) 1e9)
#define MOD (1000 * 1000 * 1000 + 7)
#define maxn 50111
#define logn 17
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
ll segd[maxn][logn], segu[maxn][logn], segl[maxn][logn], segr[maxn][logn];
ll a[maxn], b[maxn];
ll h, w, q;
map<ll, ll> dp;
ll f(int x, int y, int dir){
ll id = x * 2 * maxn + 2 * y + dir;
if(dp[id])
return dp[id];
// cout << x << " " << y << " " << dir << endl;
ll cur = 0;
if(dir == 0){ // left and right
int l = y - 1;
int r = y + 1;
for(int i = logn - 1; i >= 0; i--){
if(l - (1 << i) + 1 >= 0 && segl[l][i] < a[x])
l -= (1 << i);
if(r + (1 << i) - 1 < w && segr[r][i] < a[x])
r += (1 << i);
}
ll left = (l < 0) ? (y) : (abs(l - y) + f(x, l, 1));
ll right = (r >= w) ? (w - 1 - y) : (abs(r - y) + f(x, r, 1));
cur = max(left, right);
}
if(dir == 1){ // up and down
int u = x - 1;
int d = x + 1;
for(int i = logn - 1; i >= 0; i--){
if(u - (1 << i) + 1 >= 0 && segu[u][i] < b[y])
u -= (1 << i);
if(d + (1 << i) - 1 < h && segd[d][i] < b[y])
d += (1 << i);
}
ll up = (u < 0) ? (x) : (abs(u - x) + f(u, y, 0));
ll down = (d >= h) ? (h - 1 - x) : (abs(d - x) + f(d, y, 0));
cur = max(up, down);
}
return dp[id] = cur;
}
void precalc(){
for(int i = 0; i < h; i++)
segu[i][0] = segd[i][0] = a[i];
for(int j = 1; j < logn; j++){
for(int i = 0; i < h; i++){
if(i + (1 << j) - 1 < h)
segd[i][j] = max(segd[i][j - 1], segd[i + (1 << (j - 1))][j - 1]);
if(i - (1 << j) + 1 >= 0)
segu[i][j] = max(segu[i][j - 1], segu[i - (1 << (j - 1))][j - 1]);
}
}
for(int i = 0; i < w; i++)
segl[i][0] = segr[i][0] = b[i];
for(int j = 1; j < logn; j++){
for(int i = 0; i < w; i++){
if(i + (1 << j) - 1 < w)
segr[i][j] = max(segr[i][j - 1], segr[i + (1 << (j - 1))][j - 1]);
if(i - (1 << j) + 1 >= 0)
segl[i][j] = max(segl[i][j - 1], segl[i - (1 << (j - 1))][j - 1]);
}
}
}
int main(){
scanf("%lld%lld%lld", &h, &w, &q);
for(int i = 0; i < h; i++)
scanf("%lld", a + i);
for(int i = 0; i < w; i++)
scanf("%lld", b + i);
precalc();
while(q--){
ll x, y, ans = 0;
scanf("%lld%lld", &x, &y);
x--; y--;
for(int i = 0; i < 2; i++)
ans = max(ans, f(x, y, i));
printf("%lld\n", ans);
}
return 0;
}
Compilation message
abduction2.cpp: In function 'int main()':
abduction2.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld", &h, &w, &q);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:100:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", a + i);
~~~~~^~~~~~~~~~~~~~~
abduction2.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", b + i);
~~~~~^~~~~~~~~~~~~~~
abduction2.cpp:109:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
3 ms |
616 KB |
Output is correct |
5 |
Correct |
3 ms |
616 KB |
Output is correct |
6 |
Correct |
3 ms |
616 KB |
Output is correct |
7 |
Correct |
2 ms |
616 KB |
Output is correct |
8 |
Correct |
4 ms |
616 KB |
Output is correct |
9 |
Correct |
5 ms |
616 KB |
Output is correct |
10 |
Correct |
3 ms |
616 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
3 ms |
616 KB |
Output is correct |
5 |
Correct |
3 ms |
616 KB |
Output is correct |
6 |
Correct |
3 ms |
616 KB |
Output is correct |
7 |
Correct |
2 ms |
616 KB |
Output is correct |
8 |
Correct |
4 ms |
616 KB |
Output is correct |
9 |
Correct |
5 ms |
616 KB |
Output is correct |
10 |
Correct |
3 ms |
616 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
6 ms |
1572 KB |
Output is correct |
13 |
Correct |
5 ms |
1700 KB |
Output is correct |
14 |
Correct |
4 ms |
1700 KB |
Output is correct |
15 |
Correct |
6 ms |
1700 KB |
Output is correct |
16 |
Correct |
4 ms |
1700 KB |
Output is correct |
17 |
Correct |
6 ms |
1728 KB |
Output is correct |
18 |
Correct |
5 ms |
1872 KB |
Output is correct |
19 |
Correct |
7 ms |
1872 KB |
Output is correct |
20 |
Correct |
7 ms |
1952 KB |
Output is correct |
21 |
Correct |
7 ms |
1952 KB |
Output is correct |
22 |
Correct |
9 ms |
2152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
3 ms |
616 KB |
Output is correct |
5 |
Correct |
3 ms |
616 KB |
Output is correct |
6 |
Correct |
3 ms |
616 KB |
Output is correct |
7 |
Correct |
2 ms |
616 KB |
Output is correct |
8 |
Correct |
4 ms |
616 KB |
Output is correct |
9 |
Correct |
5 ms |
616 KB |
Output is correct |
10 |
Correct |
3 ms |
616 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
6 ms |
1572 KB |
Output is correct |
13 |
Correct |
5 ms |
1700 KB |
Output is correct |
14 |
Correct |
4 ms |
1700 KB |
Output is correct |
15 |
Correct |
6 ms |
1700 KB |
Output is correct |
16 |
Correct |
4 ms |
1700 KB |
Output is correct |
17 |
Correct |
6 ms |
1728 KB |
Output is correct |
18 |
Correct |
5 ms |
1872 KB |
Output is correct |
19 |
Correct |
7 ms |
1872 KB |
Output is correct |
20 |
Correct |
7 ms |
1952 KB |
Output is correct |
21 |
Correct |
7 ms |
1952 KB |
Output is correct |
22 |
Correct |
9 ms |
2152 KB |
Output is correct |
23 |
Correct |
105 ms |
28012 KB |
Output is correct |
24 |
Correct |
73 ms |
28016 KB |
Output is correct |
25 |
Correct |
73 ms |
28016 KB |
Output is correct |
26 |
Correct |
76 ms |
28124 KB |
Output is correct |
27 |
Correct |
73 ms |
28124 KB |
Output is correct |
28 |
Correct |
134 ms |
33040 KB |
Output is correct |
29 |
Correct |
108 ms |
33040 KB |
Output is correct |
30 |
Correct |
195 ms |
34416 KB |
Output is correct |
31 |
Correct |
194 ms |
36032 KB |
Output is correct |
32 |
Correct |
80 ms |
36032 KB |
Output is correct |
33 |
Correct |
113 ms |
36032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
36032 KB |
Output is correct |
2 |
Correct |
8 ms |
36032 KB |
Output is correct |
3 |
Correct |
8 ms |
36032 KB |
Output is correct |
4 |
Correct |
7 ms |
36032 KB |
Output is correct |
5 |
Correct |
7 ms |
36032 KB |
Output is correct |
6 |
Correct |
7 ms |
36032 KB |
Output is correct |
7 |
Correct |
6 ms |
36032 KB |
Output is correct |
8 |
Correct |
9 ms |
36032 KB |
Output is correct |
9 |
Correct |
10 ms |
36032 KB |
Output is correct |
10 |
Correct |
8 ms |
36032 KB |
Output is correct |
11 |
Correct |
9 ms |
36032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
3 ms |
616 KB |
Output is correct |
5 |
Correct |
3 ms |
616 KB |
Output is correct |
6 |
Correct |
3 ms |
616 KB |
Output is correct |
7 |
Correct |
2 ms |
616 KB |
Output is correct |
8 |
Correct |
4 ms |
616 KB |
Output is correct |
9 |
Correct |
5 ms |
616 KB |
Output is correct |
10 |
Correct |
3 ms |
616 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
6 ms |
1572 KB |
Output is correct |
13 |
Correct |
5 ms |
1700 KB |
Output is correct |
14 |
Correct |
4 ms |
1700 KB |
Output is correct |
15 |
Correct |
6 ms |
1700 KB |
Output is correct |
16 |
Correct |
4 ms |
1700 KB |
Output is correct |
17 |
Correct |
6 ms |
1728 KB |
Output is correct |
18 |
Correct |
5 ms |
1872 KB |
Output is correct |
19 |
Correct |
7 ms |
1872 KB |
Output is correct |
20 |
Correct |
7 ms |
1952 KB |
Output is correct |
21 |
Correct |
7 ms |
1952 KB |
Output is correct |
22 |
Correct |
9 ms |
2152 KB |
Output is correct |
23 |
Correct |
105 ms |
28012 KB |
Output is correct |
24 |
Correct |
73 ms |
28016 KB |
Output is correct |
25 |
Correct |
73 ms |
28016 KB |
Output is correct |
26 |
Correct |
76 ms |
28124 KB |
Output is correct |
27 |
Correct |
73 ms |
28124 KB |
Output is correct |
28 |
Correct |
134 ms |
33040 KB |
Output is correct |
29 |
Correct |
108 ms |
33040 KB |
Output is correct |
30 |
Correct |
195 ms |
34416 KB |
Output is correct |
31 |
Correct |
194 ms |
36032 KB |
Output is correct |
32 |
Correct |
80 ms |
36032 KB |
Output is correct |
33 |
Correct |
113 ms |
36032 KB |
Output is correct |
34 |
Correct |
7 ms |
36032 KB |
Output is correct |
35 |
Correct |
8 ms |
36032 KB |
Output is correct |
36 |
Correct |
8 ms |
36032 KB |
Output is correct |
37 |
Correct |
7 ms |
36032 KB |
Output is correct |
38 |
Correct |
7 ms |
36032 KB |
Output is correct |
39 |
Correct |
7 ms |
36032 KB |
Output is correct |
40 |
Correct |
6 ms |
36032 KB |
Output is correct |
41 |
Correct |
9 ms |
36032 KB |
Output is correct |
42 |
Correct |
10 ms |
36032 KB |
Output is correct |
43 |
Correct |
8 ms |
36032 KB |
Output is correct |
44 |
Correct |
9 ms |
36032 KB |
Output is correct |
45 |
Correct |
88 ms |
36032 KB |
Output is correct |
46 |
Correct |
87 ms |
36032 KB |
Output is correct |
47 |
Correct |
99 ms |
36032 KB |
Output is correct |
48 |
Correct |
96 ms |
36032 KB |
Output is correct |
49 |
Correct |
107 ms |
36032 KB |
Output is correct |
50 |
Correct |
125 ms |
36032 KB |
Output is correct |
51 |
Correct |
120 ms |
36032 KB |
Output is correct |
52 |
Correct |
218 ms |
38232 KB |
Output is correct |
53 |
Correct |
259 ms |
38232 KB |
Output is correct |
54 |
Correct |
277 ms |
38232 KB |
Output is correct |
55 |
Correct |
346 ms |
42728 KB |
Output is correct |
56 |
Incorrect |
2279 ms |
153428 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |