#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define forn(i, n) for(ll i = 0; i < n; i ++)
#define revn(i, n) for(ll i = n - 1; i >= 0; i --)
typedef long long ll;
template<class T, template<class T2, class A=allocator<T2> > class cont> inline ostream &operator <<(ostream &out, const cont<T> &x) { for(const auto &it : x) { out << it << " ";} return out;}
template<class T, template<class T2, class A=allocator<T2> > class cont> inline istream &operator >>(istream &in, cont<T> &x) { for(auto &it : x) { in >> it;} return in;}
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
const ll MAX_N = 1e5 + 10, MAX_LOG = 20;
ll a[MAX_N], b[MAX_N];
ll n, m, q;
map<pair<ll, ll>, ll> dp[4];
ll rmqa[MAX_N][MAX_LOG], rmqb[MAX_N][MAX_LOG];
void prec() {
forn(i, n) {
rmqa[i][0] = a[i];
}
for(int l = 1; l < MAX_LOG; l ++) {
forn(i, n) {
rmqa[i][l] = rmqa[i][l - 1];
if(i + (1 << (l - 1)) < n) {
chkmax(rmqa[i][l], rmqa[i + (1 << (l - 1))][l - 1]);
}
}
}
forn(i, m) {
rmqb[i][0] = b[i];
}
for(int l = 1; l < MAX_LOG; l ++) {
forn(i, m) {
rmqb[i][l] = rmqb[i][l - 1];
if(i + (1 << (l - 1)) < m) {
chkmax(rmqb[i][l], rmqb[i + (1 << (l - 1))][l - 1]);
}
}
}
}
ll solve(ll x, ll y, ll dir) {
cerr << x << " " << y << " " << dir << endl;
if(x < 0 || y < 0 || x >= n || y >= m) {
while(true) {
}
}
if(dp[dir].find({x, y}) != dp[dir].end()) {
return dp[dir][{x, y}];
}
if(dir == 0) { // Right;
ll nowCost = a[x];
cerr << nowCost << endl;
ll nxt = y;
revn(l, MAX_LOG) {
if(nowCost > rmqb[nxt][l]) {
nxt += (1 << l);
}
if(nxt >= m) {
return dp[dir][{x, y}] = m - 1 - y;
}
}
return dp[dir][{x, y}] = nxt - y + max(solve(x, nxt, 1), solve(x, nxt, 3));
} else
if(dir == 1) { // Down;
ll nowCost = b[y];
cerr << nowCost << endl;
ll nxt = x;
revn(l, MAX_LOG) {
if(nowCost > rmqa[nxt][l]) {
nxt += (1 << l);
}
if(nxt >= n) {
return dp[dir][{x, y}] = n - 1 - x;
}
}
cerr << "going to " << nxt << endl;
return dp[dir][{x, y}] = nxt - x + max(solve(nxt, y, 0), solve(nxt, y, 2));
} else
if(dir == 2) { // Left;
ll nowCost = a[x];
cerr << nowCost << endl;
ll nxt = y;
revn(l, MAX_LOG) {
if(nxt - (1 << l) + 1 < 0) {continue;}
if(nowCost > rmqb[nxt - (1 << l) + 1][l]) {
nxt -= (1 << l);
}
if(nxt < 0) {
return dp[dir][{x, y}] = y;
}
}
return dp[dir][{x, y}] = y - nxt + max(solve(x, nxt, 1), solve(x, nxt, 3));
} else
if(dir == 3) { // Up;
ll nowCost = b[y];
cerr << nowCost << endl;
ll nxt = x;
revn(l, MAX_LOG) {
if(nxt - (1 << l) + 1 < 0) {continue;}
if(nowCost > rmqa[nxt - (1 << l) + 1][l]) {
nxt -= (1 << l);
}
if(nxt < 0) {
return dp[dir][{x, y}] = x;
}
}
return dp[dir][{x, y}] = x - nxt + max(solve(nxt, y, 2), solve(nxt, y, 0));
}
}
signed main() {
// ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> m >> q;
forn(i, n) {
cin >> a[i];
}
forn(i, m) {
cin >> b[i];
}
prec();
while(q --) {
ll x, y;
cin >> x >> y;
x --;
y --;
ll ret = 0;
if(x != n - 1) {
chkmax(ret, solve(x + 1, y, 1));
}
if(x != 0) {
chkmax(ret, solve(x - 1, y, 3));
}
if(y != m - 1) {
chkmax(ret, solve(x, y + 1, 0));
}
if(y != 0) {
chkmax(ret, solve(x, y - 1, 2));
}
cout << ret + 1 << endl;
}
return 0;
forn(dir, 4) {
cout << dir << endl;
for(auto it : dp[dir]) {
cout << "[" << it.first << "] " << it.second << endl;
}
}
return 0;
}
Compilation message
abduction2.cpp: In function 'll solve(ll, ll, ll)':
abduction2.cpp:125:1: warning: control reaches end of non-void function [-Wreturn-type]
125 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
220 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
220 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
2 ms |
972 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
972 KB |
Output is correct |
15 |
Correct |
2 ms |
972 KB |
Output is correct |
16 |
Correct |
2 ms |
972 KB |
Output is correct |
17 |
Correct |
2 ms |
972 KB |
Output is correct |
18 |
Correct |
3 ms |
972 KB |
Output is correct |
19 |
Correct |
4 ms |
1228 KB |
Output is correct |
20 |
Correct |
6 ms |
1484 KB |
Output is correct |
21 |
Correct |
6 ms |
1356 KB |
Output is correct |
22 |
Correct |
8 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
220 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
2 ms |
972 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
972 KB |
Output is correct |
15 |
Correct |
2 ms |
972 KB |
Output is correct |
16 |
Correct |
2 ms |
972 KB |
Output is correct |
17 |
Correct |
2 ms |
972 KB |
Output is correct |
18 |
Correct |
3 ms |
972 KB |
Output is correct |
19 |
Correct |
4 ms |
1228 KB |
Output is correct |
20 |
Correct |
6 ms |
1484 KB |
Output is correct |
21 |
Correct |
6 ms |
1356 KB |
Output is correct |
22 |
Correct |
8 ms |
1868 KB |
Output is correct |
23 |
Correct |
50 ms |
17720 KB |
Output is correct |
24 |
Correct |
54 ms |
17604 KB |
Output is correct |
25 |
Correct |
50 ms |
17636 KB |
Output is correct |
26 |
Correct |
49 ms |
17692 KB |
Output is correct |
27 |
Correct |
49 ms |
17604 KB |
Output is correct |
28 |
Correct |
70 ms |
25304 KB |
Output is correct |
29 |
Correct |
49 ms |
18772 KB |
Output is correct |
30 |
Correct |
143 ms |
28872 KB |
Output is correct |
31 |
Correct |
181 ms |
32240 KB |
Output is correct |
32 |
Correct |
54 ms |
18116 KB |
Output is correct |
33 |
Correct |
76 ms |
21120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1228 KB |
Output is correct |
2 |
Correct |
5 ms |
1228 KB |
Output is correct |
3 |
Correct |
5 ms |
1240 KB |
Output is correct |
4 |
Correct |
5 ms |
1288 KB |
Output is correct |
5 |
Correct |
5 ms |
1228 KB |
Output is correct |
6 |
Correct |
3 ms |
1356 KB |
Output is correct |
7 |
Correct |
3 ms |
1228 KB |
Output is correct |
8 |
Correct |
8 ms |
1788 KB |
Output is correct |
9 |
Correct |
9 ms |
1820 KB |
Output is correct |
10 |
Correct |
9 ms |
1816 KB |
Output is correct |
11 |
Correct |
11 ms |
1996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
220 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
2 ms |
972 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
972 KB |
Output is correct |
15 |
Correct |
2 ms |
972 KB |
Output is correct |
16 |
Correct |
2 ms |
972 KB |
Output is correct |
17 |
Correct |
2 ms |
972 KB |
Output is correct |
18 |
Correct |
3 ms |
972 KB |
Output is correct |
19 |
Correct |
4 ms |
1228 KB |
Output is correct |
20 |
Correct |
6 ms |
1484 KB |
Output is correct |
21 |
Correct |
6 ms |
1356 KB |
Output is correct |
22 |
Correct |
8 ms |
1868 KB |
Output is correct |
23 |
Correct |
50 ms |
17720 KB |
Output is correct |
24 |
Correct |
54 ms |
17604 KB |
Output is correct |
25 |
Correct |
50 ms |
17636 KB |
Output is correct |
26 |
Correct |
49 ms |
17692 KB |
Output is correct |
27 |
Correct |
49 ms |
17604 KB |
Output is correct |
28 |
Correct |
70 ms |
25304 KB |
Output is correct |
29 |
Correct |
49 ms |
18772 KB |
Output is correct |
30 |
Correct |
143 ms |
28872 KB |
Output is correct |
31 |
Correct |
181 ms |
32240 KB |
Output is correct |
32 |
Correct |
54 ms |
18116 KB |
Output is correct |
33 |
Correct |
76 ms |
21120 KB |
Output is correct |
34 |
Correct |
5 ms |
1228 KB |
Output is correct |
35 |
Correct |
5 ms |
1228 KB |
Output is correct |
36 |
Correct |
5 ms |
1240 KB |
Output is correct |
37 |
Correct |
5 ms |
1288 KB |
Output is correct |
38 |
Correct |
5 ms |
1228 KB |
Output is correct |
39 |
Correct |
3 ms |
1356 KB |
Output is correct |
40 |
Correct |
3 ms |
1228 KB |
Output is correct |
41 |
Correct |
8 ms |
1788 KB |
Output is correct |
42 |
Correct |
9 ms |
1820 KB |
Output is correct |
43 |
Correct |
9 ms |
1816 KB |
Output is correct |
44 |
Correct |
11 ms |
1996 KB |
Output is correct |
45 |
Correct |
55 ms |
18208 KB |
Output is correct |
46 |
Correct |
59 ms |
18328 KB |
Output is correct |
47 |
Correct |
54 ms |
18244 KB |
Output is correct |
48 |
Correct |
58 ms |
18248 KB |
Output is correct |
49 |
Correct |
53 ms |
18268 KB |
Output is correct |
50 |
Correct |
81 ms |
25440 KB |
Output is correct |
51 |
Correct |
80 ms |
26112 KB |
Output is correct |
52 |
Correct |
235 ms |
37316 KB |
Output is correct |
53 |
Correct |
222 ms |
36336 KB |
Output is correct |
54 |
Correct |
231 ms |
35288 KB |
Output is correct |
55 |
Correct |
424 ms |
44996 KB |
Output is correct |
56 |
Correct |
3049 ms |
267748 KB |
Output is correct |
57 |
Correct |
721 ms |
82272 KB |
Output is correct |
58 |
Correct |
661 ms |
79044 KB |
Output is correct |
59 |
Correct |
645 ms |
78652 KB |
Output is correct |