#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <deque>
#include <map>
#include <set>
#include <complex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <chrono>
#define ft first
#define sc second
#define pb push_back
#define len(v) (int)v.size()
// #define int ll
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> logt;
void built_logt(int n) {
logt = vector<int> (n + 2, 0);
int cnt = 0, now = 1;
for (int i = 0; i <= n + 1; i++) {
if(now * 2 == i) {
now *= 2;
cnt++;
}
logt[i] = cnt;
}
return;
}
struct sparse {
vector<vector<int>> table;
int n, log;
sparse() {}
sparse(vector<int>& have) {
n = len(have);
log = 1;
while((1 << log) <= n)
log *= 2;
table = vector<vector<int>> (log + 1, vector<int> (n + 1, 0));
for (int i = 0; i < n; i++) {
table[0][i] = have[i];
}
for (int logn = 1; logn < log; logn++) {
for (int i = 0; i < n; i++) {
int nxt = min(n - 1, (1 << (logn - 1)) + i);
table[logn][i] = max(table[logn - 1][i], table[logn - 1][nxt]);
}
}
return;
}
int get(int l, int r) {
// int ans = 0;
// for (int i = l; i <= r; i++) {
// ans = max(table[0][i], ans);
// }
// return ans;
int lenn = (r - l + 1);
int logn = logt[lenn];
int pred = (r - (1 << logn) + 1);
int ans = max(table[logn][l], table[logn][pred]);
return ans;
}
};
map<pair<int, int>, int> dp;
int w, h;
vector<int> a;
vector<int> b;
sparse as;
sparse bs;
int solve(int i, int j) {
// cout << i << " " << j << endl;
// cout << a[i] << " " << b[j] << endl;
if(dp.count({i, j}))
return dp[{i, j}];
if(a[i] > b[j]) {
int l = j, r = w;
while(r - l > 1) {
int now = (l + r) / 2;
if(bs.get(j, now) > a[i])
r = now;
else
l = now;
}
if(r == w)
dp[{i, j}] = max(dp[{i, j}], w - j);
else
dp[{i, j}] = max(dp[{i, j}], r - j + solve(i, r));
l = -1, r = j;
while(r - l > 1) {
int now = (l + r) / 2;
if(bs.get(now, j) > a[i])
l = now;
else
r = now;
}
if(l == -1)
dp[{i, j}] = max(dp[{i, j}], j + 1);
else
dp[{i, j}] = max(dp[{i, j}], j - l + solve(i, l));
}
else {
int l = i, r = h;
while(r - l > 1) {
int now = (l + r) / 2;
if(as.get(i, now) > b[j])
r = now;
else
l = now;
}
if(r == h)
dp[{i, j}] = max(dp[{i, j}], h - i);
else
dp[{i, j}] = max(dp[{i, j}], r - i + solve(r, j));
l = -1, r = i;
while(r - l > 1) {
int now = (l + r) / 2;
if(as.get(now, i) > b[j])
l = now;
else
r = now;
}
if(l == -1)
dp[{i, j}] = max(dp[{i, j}], i + 1);
else
dp[{i, j}] = max(dp[{i, j}], i - l + solve(l, j));
}
return dp[{i, j}];
}
signed main() {
#ifdef PC
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
cin >> h >> w >> q;
a = vector<int> (h);
b = vector<int> (w);
for (int i = 0; i < h; i++) {
cin >> a[i];
}
for (int j = 0; j < w; j++) {
cin >> b[j];
}
built_logt(w + h);
as = *new sparse(a);
bs = *new sparse(b);
for (int k = 0; k < q; k++) {
int i, j;
cin >> i >> j;
i--, j--;
int ans = 1;
for (int j1 = j + 1; j1 < w; j1++) {
ans = max(ans, j1 - j + 1);
if(b[j1] > a[i]) {
ans = max(ans, j1 - j + solve(i, j1));
break;
}
}
for (int j1 = j - 1; j1 >= 0; j1--) {
ans = max(ans, j - j1 + 1);
if(b[j1] > a[i]) {
ans = max(ans, j - j1 + solve(i, j1));
break;
}
}
for (int i1 = i + 1; i1 < h; i1++) {
ans = max(ans, i1 - i + 1);
if(a[i1] > b[j]) {
ans = max(ans, i1 - i + solve(i1, j));
break;
}
}
for (int i1 = i - 1; i1 >= 0; i1--) {
ans = max(ans, i - i1 + 1);
if(a[i1] > b[j]) {
ans = max(ans, i - i1 + solve(i1, j));
break;
}
}
cout << ans - 1 << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
892 KB |
Output is correct |
13 |
Correct |
6 ms |
888 KB |
Output is correct |
14 |
Correct |
6 ms |
888 KB |
Output is correct |
15 |
Correct |
6 ms |
888 KB |
Output is correct |
16 |
Correct |
6 ms |
888 KB |
Output is correct |
17 |
Correct |
6 ms |
888 KB |
Output is correct |
18 |
Correct |
6 ms |
884 KB |
Output is correct |
19 |
Correct |
8 ms |
1144 KB |
Output is correct |
20 |
Correct |
10 ms |
1144 KB |
Output is correct |
21 |
Correct |
9 ms |
1144 KB |
Output is correct |
22 |
Correct |
11 ms |
1400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
892 KB |
Output is correct |
13 |
Correct |
6 ms |
888 KB |
Output is correct |
14 |
Correct |
6 ms |
888 KB |
Output is correct |
15 |
Correct |
6 ms |
888 KB |
Output is correct |
16 |
Correct |
6 ms |
888 KB |
Output is correct |
17 |
Correct |
6 ms |
888 KB |
Output is correct |
18 |
Correct |
6 ms |
884 KB |
Output is correct |
19 |
Correct |
8 ms |
1144 KB |
Output is correct |
20 |
Correct |
10 ms |
1144 KB |
Output is correct |
21 |
Correct |
9 ms |
1144 KB |
Output is correct |
22 |
Correct |
11 ms |
1400 KB |
Output is correct |
23 |
Correct |
32 ms |
14644 KB |
Output is correct |
24 |
Correct |
33 ms |
14648 KB |
Output is correct |
25 |
Correct |
36 ms |
14644 KB |
Output is correct |
26 |
Correct |
33 ms |
14644 KB |
Output is correct |
27 |
Correct |
32 ms |
14644 KB |
Output is correct |
28 |
Correct |
73 ms |
19128 KB |
Output is correct |
29 |
Correct |
39 ms |
16052 KB |
Output is correct |
30 |
Correct |
147 ms |
21428 KB |
Output is correct |
31 |
Correct |
176 ms |
23092 KB |
Output is correct |
32 |
Correct |
39 ms |
15540 KB |
Output is correct |
33 |
Correct |
65 ms |
17332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
1016 KB |
Output is correct |
2 |
Correct |
9 ms |
1016 KB |
Output is correct |
3 |
Correct |
9 ms |
1016 KB |
Output is correct |
4 |
Correct |
9 ms |
1016 KB |
Output is correct |
5 |
Correct |
9 ms |
1016 KB |
Output is correct |
6 |
Correct |
8 ms |
1144 KB |
Output is correct |
7 |
Correct |
7 ms |
1144 KB |
Output is correct |
8 |
Correct |
13 ms |
1276 KB |
Output is correct |
9 |
Correct |
15 ms |
1272 KB |
Output is correct |
10 |
Correct |
12 ms |
1272 KB |
Output is correct |
11 |
Correct |
14 ms |
1400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
892 KB |
Output is correct |
13 |
Correct |
6 ms |
888 KB |
Output is correct |
14 |
Correct |
6 ms |
888 KB |
Output is correct |
15 |
Correct |
6 ms |
888 KB |
Output is correct |
16 |
Correct |
6 ms |
888 KB |
Output is correct |
17 |
Correct |
6 ms |
888 KB |
Output is correct |
18 |
Correct |
6 ms |
884 KB |
Output is correct |
19 |
Correct |
8 ms |
1144 KB |
Output is correct |
20 |
Correct |
10 ms |
1144 KB |
Output is correct |
21 |
Correct |
9 ms |
1144 KB |
Output is correct |
22 |
Correct |
11 ms |
1400 KB |
Output is correct |
23 |
Correct |
32 ms |
14644 KB |
Output is correct |
24 |
Correct |
33 ms |
14648 KB |
Output is correct |
25 |
Correct |
36 ms |
14644 KB |
Output is correct |
26 |
Correct |
33 ms |
14644 KB |
Output is correct |
27 |
Correct |
32 ms |
14644 KB |
Output is correct |
28 |
Correct |
73 ms |
19128 KB |
Output is correct |
29 |
Correct |
39 ms |
16052 KB |
Output is correct |
30 |
Correct |
147 ms |
21428 KB |
Output is correct |
31 |
Correct |
176 ms |
23092 KB |
Output is correct |
32 |
Correct |
39 ms |
15540 KB |
Output is correct |
33 |
Correct |
65 ms |
17332 KB |
Output is correct |
34 |
Correct |
9 ms |
1016 KB |
Output is correct |
35 |
Correct |
9 ms |
1016 KB |
Output is correct |
36 |
Correct |
9 ms |
1016 KB |
Output is correct |
37 |
Correct |
9 ms |
1016 KB |
Output is correct |
38 |
Correct |
9 ms |
1016 KB |
Output is correct |
39 |
Correct |
8 ms |
1144 KB |
Output is correct |
40 |
Correct |
7 ms |
1144 KB |
Output is correct |
41 |
Correct |
13 ms |
1276 KB |
Output is correct |
42 |
Correct |
15 ms |
1272 KB |
Output is correct |
43 |
Correct |
12 ms |
1272 KB |
Output is correct |
44 |
Correct |
14 ms |
1400 KB |
Output is correct |
45 |
Correct |
41 ms |
15668 KB |
Output is correct |
46 |
Correct |
42 ms |
15672 KB |
Output is correct |
47 |
Correct |
40 ms |
15668 KB |
Output is correct |
48 |
Correct |
43 ms |
15668 KB |
Output is correct |
49 |
Correct |
40 ms |
15668 KB |
Output is correct |
50 |
Correct |
91 ms |
19636 KB |
Output is correct |
51 |
Correct |
94 ms |
20148 KB |
Output is correct |
52 |
Correct |
239 ms |
25524 KB |
Output is correct |
53 |
Correct |
245 ms |
24824 KB |
Output is correct |
54 |
Correct |
231 ms |
24500 KB |
Output is correct |
55 |
Incorrect |
367 ms |
29748 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |