#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 50005;
template<class T, int SZ> struct RMQ {
T stor[SZ][32-__builtin_clz(SZ)];
T comb(T a, T b) {
return max(a,b);
}
void build(vector<T>& x) {
F0R(i,sz(x)) stor[i][0] = x[i];
FOR(j,1,32-__builtin_clz(SZ)) F0R(i,SZ-(1<<(j-1)))
stor[i][j] = comb(stor[i][j-1],
stor[i+(1<<(j-1))][j-1]);
}
T query(int l, int r) {
int x = 31-__builtin_clz(r-l+1);
return comb(stor[l][x],stor[r-(1<<x)+1][x]);
}
int getLst(int pos, int val) {
int lo = 0, hi = pos-1;
while (lo < hi) {
int mid = (lo+hi+1)/2;
if (query(mid,pos-1) > val) lo = mid;
else hi = mid-1;
}
return lo;
}
int getFst(int pos, int val) {
int lo = pos+1, hi = SZ-1;
while (lo < hi) {
int mid = (lo+hi)/2;
if (query(pos+1,mid) > val) hi = mid;
else lo = mid+1;
}
return lo;
}
};
namespace mapOp {
const int tmp = chrono::high_resolution_clock::now().time_since_epoch().count();
template<class T> struct hsh {
size_t operator()(const T& x) const {
return hash<T>{}(x)^tmp; // avoid anti-hash tests?
}
};
template<class a, class b> using um = gp_hash_table<a,b,hsh<a>>;
template<class a, class b> b get(um<a,b>& u, a x) {
if (u.find(x) == u.end()) return 0;
return u[x];
}
}
using namespace mapOp;
RMQ<int,MX> R[2];
um<int,ll> dp[2][MX];
int H,W,Q;
vi A,B;
ll get(int ind, int r, int c) {
if (dp[ind][r].find(c) != dp[ind][r].end()) return dp[ind][r][c];
ll res = 0;
if (ind == 0) {
int r0 = R[0].getLst(r,B[c]), r1 = R[0].getFst(r,B[c]);
res = max(res,r0 >= 1?get(1,r0,c)+r-r0:r-1);
res = max(res,r1 <= H?get(1,r1,c)+r1-r:H-r);
} else {
int c0 = R[1].getLst(c,A[r]), c1 = R[1].getFst(c,A[r]);
res = max(res,c0 >= 1?get(0,r,c0)+c-c0:c-1);
res = max(res,c1 <= W?get(0,r,c1)+c1-c:W-c);
}
return dp[ind][r][c] = res;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> H >> W >> Q;
A.resize(H+1), B.resize(W+1);
FOR(i,1,H+1) cin >> A[i];
FOR(i,1,W+1) cin >> B[i];
R[0].build(A), R[1].build(B);
F0R(i,Q) {
int S,T; cin >> S >> T;
cout << max(get(0,S,T),get(1,S,T)) << "\n";
}
}
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
* if you have no idea just guess the appropriate well-known algo instead of doing nothing :/
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
37112 KB |
Output is correct |
2 |
Correct |
52 ms |
37112 KB |
Output is correct |
3 |
Correct |
51 ms |
37264 KB |
Output is correct |
4 |
Correct |
56 ms |
37272 KB |
Output is correct |
5 |
Correct |
51 ms |
37272 KB |
Output is correct |
6 |
Correct |
53 ms |
37332 KB |
Output is correct |
7 |
Correct |
48 ms |
37332 KB |
Output is correct |
8 |
Correct |
58 ms |
37480 KB |
Output is correct |
9 |
Correct |
53 ms |
37480 KB |
Output is correct |
10 |
Correct |
59 ms |
37480 KB |
Output is correct |
11 |
Correct |
52 ms |
37480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
37112 KB |
Output is correct |
2 |
Correct |
52 ms |
37112 KB |
Output is correct |
3 |
Correct |
51 ms |
37264 KB |
Output is correct |
4 |
Correct |
56 ms |
37272 KB |
Output is correct |
5 |
Correct |
51 ms |
37272 KB |
Output is correct |
6 |
Correct |
53 ms |
37332 KB |
Output is correct |
7 |
Correct |
48 ms |
37332 KB |
Output is correct |
8 |
Correct |
58 ms |
37480 KB |
Output is correct |
9 |
Correct |
53 ms |
37480 KB |
Output is correct |
10 |
Correct |
59 ms |
37480 KB |
Output is correct |
11 |
Correct |
52 ms |
37480 KB |
Output is correct |
12 |
Correct |
58 ms |
37480 KB |
Output is correct |
13 |
Correct |
51 ms |
37500 KB |
Output is correct |
14 |
Correct |
62 ms |
37652 KB |
Output is correct |
15 |
Correct |
67 ms |
37776 KB |
Output is correct |
16 |
Correct |
55 ms |
37888 KB |
Output is correct |
17 |
Correct |
59 ms |
37956 KB |
Output is correct |
18 |
Correct |
60 ms |
38056 KB |
Output is correct |
19 |
Correct |
53 ms |
38056 KB |
Output is correct |
20 |
Correct |
52 ms |
38056 KB |
Output is correct |
21 |
Correct |
52 ms |
38196 KB |
Output is correct |
22 |
Correct |
59 ms |
38196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
37112 KB |
Output is correct |
2 |
Correct |
52 ms |
37112 KB |
Output is correct |
3 |
Correct |
51 ms |
37264 KB |
Output is correct |
4 |
Correct |
56 ms |
37272 KB |
Output is correct |
5 |
Correct |
51 ms |
37272 KB |
Output is correct |
6 |
Correct |
53 ms |
37332 KB |
Output is correct |
7 |
Correct |
48 ms |
37332 KB |
Output is correct |
8 |
Correct |
58 ms |
37480 KB |
Output is correct |
9 |
Correct |
53 ms |
37480 KB |
Output is correct |
10 |
Correct |
59 ms |
37480 KB |
Output is correct |
11 |
Correct |
52 ms |
37480 KB |
Output is correct |
12 |
Correct |
58 ms |
37480 KB |
Output is correct |
13 |
Correct |
51 ms |
37500 KB |
Output is correct |
14 |
Correct |
62 ms |
37652 KB |
Output is correct |
15 |
Correct |
67 ms |
37776 KB |
Output is correct |
16 |
Correct |
55 ms |
37888 KB |
Output is correct |
17 |
Correct |
59 ms |
37956 KB |
Output is correct |
18 |
Correct |
60 ms |
38056 KB |
Output is correct |
19 |
Correct |
53 ms |
38056 KB |
Output is correct |
20 |
Correct |
52 ms |
38056 KB |
Output is correct |
21 |
Correct |
52 ms |
38196 KB |
Output is correct |
22 |
Correct |
59 ms |
38196 KB |
Output is correct |
23 |
Correct |
69 ms |
39424 KB |
Output is correct |
24 |
Correct |
70 ms |
40392 KB |
Output is correct |
25 |
Correct |
74 ms |
41320 KB |
Output is correct |
26 |
Correct |
61 ms |
42252 KB |
Output is correct |
27 |
Correct |
68 ms |
43216 KB |
Output is correct |
28 |
Correct |
96 ms |
46816 KB |
Output is correct |
29 |
Correct |
79 ms |
46816 KB |
Output is correct |
30 |
Correct |
140 ms |
48096 KB |
Output is correct |
31 |
Correct |
146 ms |
49452 KB |
Output is correct |
32 |
Correct |
72 ms |
49452 KB |
Output is correct |
33 |
Correct |
84 ms |
49452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
49452 KB |
Output is correct |
2 |
Correct |
59 ms |
49452 KB |
Output is correct |
3 |
Correct |
64 ms |
49452 KB |
Output is correct |
4 |
Correct |
62 ms |
49452 KB |
Output is correct |
5 |
Correct |
64 ms |
49452 KB |
Output is correct |
6 |
Correct |
62 ms |
49452 KB |
Output is correct |
7 |
Correct |
51 ms |
49452 KB |
Output is correct |
8 |
Correct |
68 ms |
49452 KB |
Output is correct |
9 |
Correct |
56 ms |
49452 KB |
Output is correct |
10 |
Correct |
57 ms |
49452 KB |
Output is correct |
11 |
Correct |
61 ms |
49452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
37112 KB |
Output is correct |
2 |
Correct |
52 ms |
37112 KB |
Output is correct |
3 |
Correct |
51 ms |
37264 KB |
Output is correct |
4 |
Correct |
56 ms |
37272 KB |
Output is correct |
5 |
Correct |
51 ms |
37272 KB |
Output is correct |
6 |
Correct |
53 ms |
37332 KB |
Output is correct |
7 |
Correct |
48 ms |
37332 KB |
Output is correct |
8 |
Correct |
58 ms |
37480 KB |
Output is correct |
9 |
Correct |
53 ms |
37480 KB |
Output is correct |
10 |
Correct |
59 ms |
37480 KB |
Output is correct |
11 |
Correct |
52 ms |
37480 KB |
Output is correct |
12 |
Correct |
58 ms |
37480 KB |
Output is correct |
13 |
Correct |
51 ms |
37500 KB |
Output is correct |
14 |
Correct |
62 ms |
37652 KB |
Output is correct |
15 |
Correct |
67 ms |
37776 KB |
Output is correct |
16 |
Correct |
55 ms |
37888 KB |
Output is correct |
17 |
Correct |
59 ms |
37956 KB |
Output is correct |
18 |
Correct |
60 ms |
38056 KB |
Output is correct |
19 |
Correct |
53 ms |
38056 KB |
Output is correct |
20 |
Correct |
52 ms |
38056 KB |
Output is correct |
21 |
Correct |
52 ms |
38196 KB |
Output is correct |
22 |
Correct |
59 ms |
38196 KB |
Output is correct |
23 |
Correct |
69 ms |
39424 KB |
Output is correct |
24 |
Correct |
70 ms |
40392 KB |
Output is correct |
25 |
Correct |
74 ms |
41320 KB |
Output is correct |
26 |
Correct |
61 ms |
42252 KB |
Output is correct |
27 |
Correct |
68 ms |
43216 KB |
Output is correct |
28 |
Correct |
96 ms |
46816 KB |
Output is correct |
29 |
Correct |
79 ms |
46816 KB |
Output is correct |
30 |
Correct |
140 ms |
48096 KB |
Output is correct |
31 |
Correct |
146 ms |
49452 KB |
Output is correct |
32 |
Correct |
72 ms |
49452 KB |
Output is correct |
33 |
Correct |
84 ms |
49452 KB |
Output is correct |
34 |
Correct |
72 ms |
49452 KB |
Output is correct |
35 |
Correct |
59 ms |
49452 KB |
Output is correct |
36 |
Correct |
64 ms |
49452 KB |
Output is correct |
37 |
Correct |
62 ms |
49452 KB |
Output is correct |
38 |
Correct |
64 ms |
49452 KB |
Output is correct |
39 |
Correct |
62 ms |
49452 KB |
Output is correct |
40 |
Correct |
51 ms |
49452 KB |
Output is correct |
41 |
Correct |
68 ms |
49452 KB |
Output is correct |
42 |
Correct |
56 ms |
49452 KB |
Output is correct |
43 |
Correct |
57 ms |
49452 KB |
Output is correct |
44 |
Correct |
61 ms |
49452 KB |
Output is correct |
45 |
Correct |
82 ms |
50716 KB |
Output is correct |
46 |
Correct |
94 ms |
51852 KB |
Output is correct |
47 |
Correct |
87 ms |
52632 KB |
Output is correct |
48 |
Correct |
80 ms |
53600 KB |
Output is correct |
49 |
Correct |
84 ms |
54584 KB |
Output is correct |
50 |
Correct |
126 ms |
56996 KB |
Output is correct |
51 |
Correct |
123 ms |
58204 KB |
Output is correct |
52 |
Correct |
177 ms |
59516 KB |
Output is correct |
53 |
Correct |
136 ms |
60400 KB |
Output is correct |
54 |
Correct |
176 ms |
61308 KB |
Output is correct |
55 |
Correct |
172 ms |
61996 KB |
Output is correct |
56 |
Correct |
1466 ms |
193652 KB |
Output is correct |
57 |
Correct |
452 ms |
193652 KB |
Output is correct |
58 |
Correct |
414 ms |
193652 KB |
Output is correct |
59 |
Correct |
407 ms |
193652 KB |
Output is correct |