#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
using namespace std;
///~~~~~~~~~~~~~~~~~~~~~~~~~~
template <typename H, typename T>
ostream& operator<<(ostream& os, pair<H, T> m){
return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H>
ostream& operator<<(ostream& os, vector<H> V){
os<<"{";
for(int i=0; i<V.size(); i++){
if(i)os<<" ";
os<<V[i];
}
os<<"}";
return os;
}
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
//#define deb(x...) ;
///~~~~~~~~~~~~~~~~~~~~~~~~~
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int K=16, N=1<<K, INF=1e9+5, mod=1e9+7, mod2=998244353;
struct modint{
int n=0;
modint(){}
modint(ll x){
n=x%mod;
if(n<0)n+=mod;
}
operator int(){
return n;
}
modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;}
modint operator+=(modint a){return (*this)=(*this)+a;}
modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;}
modint operator-=(modint a){return (*this)=(*this)-a;}
modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;}
modint operator*=(modint a){return (*this)=(*this)*a;}
modint operator^(const ll &m)const{
modint a(1);
if(m==0)return a;
if(m==1)return (*this);
a=(*this)^(m/2);
a*=a;
return a*((*this)^(m&1));
}
modint odw(){
return (*this)^((ll)mod-2);
}
modint operator/(modint a){return (*this)*a.odw();}
modint operator/=(modint a){return (*this)=(*this)/a;}
bool operator==(modint a){return a.n==n;}
friend ostream& operator<<(ostream& os, modint m) {
return os << m.n;
}
};
modint fact[N], fact2[N];
typedef vector<modint> vm;
void factinit(){
fact[0]=1;
for(int i=1; i<N; i++){
fact[i]=(fact[i-1]*modint(i))%mod;
}
fact2[N-1]=fact[N-1].odw();
for(int i=N-2; i>=0; i--){
fact2[i]=(fact2[i+1]*modint(i+1))%mod;
}
}
modint npok(int _n, int _k){
return fact[_n]*fact2[_k]*fact2[_n-_k];
}
int HM[K][N], WM[K][N], A[N], B[N];
unordered_map<ll, ll> M;
int H, W;
ll go(int x, int y, bool b){
//deb(x, y, b);
if(max(A[x], B[y])==2e9)return 0;
ll hasz=W*1ll*x+y;
if(b)hasz+=W*1ll*H;
if(M.find(hasz)!=M.end())return M[hasz];
if(b==0){
int l=x-1, r=x+1;
for(int i=K-1; i>=0; i--){
if(HM[i][r]<B[y])r+=(1<<i);
}
for(int i=K-1; i>=0; i--){
if(l+1>=(1<<i) && HM[i][l+1-(1<<i)]<B[y])l-=(1<<i);
}
return M[hasz]=max(x-l+go(l, y, b^1), r-x+go(r, y, b^1));
}
else{
int l=y-1, r=y+1;
for(int i=K-1; i>=0; i--){
if(WM[i][r]<A[x])r+=(1<<i);
}
for(int i=K-1; i>=0; i--){
if(l+1>=(1<<i) && WM[i][l+1-(1<<i)]<A[x])l-=(1<<i);
}
assert(l!=-1);
return M[hasz]=max(r-y+go(x, r, b^1), y-l+go(x, l, b^1));
}
}
int main(){
//factinit();
//BOOST;
int Q;
cin>>H>>W>>Q;
for(int i=0; i<=H+1; i++){
if(i && i-H-1)cin>>A[i];
else A[i]=2e9;
HM[0][i]=A[i];
}
for(int i=0; i<=W+1; i++){
if(i && i-W-1)cin>>B[i];
else B[i]=2e9;
WM[0][i]=B[i];
}
for(int k=1; k<K; k++){
for(int i=0; i<=H+1; i++){
HM[k][i]=HM[k-1][i];
if(i+(1<<(k-1))<=H+1){
HM[k][i]=max(HM[k][i], HM[k-1][i+(1<<(k-1))]);
}
}
for(int i=0; i<=W+1; i++){
WM[k][i]=WM[k-1][i];
if(i+(1<<(k-1))<=W+1){
WM[k][i]=max(WM[k][i], WM[k-1][i+(1<<(k-1))]);
}
}
}
while(Q--){
int x, y;
cin>>x>>y;
cout<<max(go(x,y, 1), go(x, y, 0))-1<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
0 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
0 ms |
852 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Correct |
2 ms |
1236 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
2 ms |
1236 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
2 ms |
1240 KB |
Output is correct |
19 |
Correct |
3 ms |
1364 KB |
Output is correct |
20 |
Correct |
4 ms |
1364 KB |
Output is correct |
21 |
Correct |
3 ms |
1364 KB |
Output is correct |
22 |
Correct |
4 ms |
1588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
0 ms |
852 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Correct |
2 ms |
1236 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
2 ms |
1236 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
2 ms |
1240 KB |
Output is correct |
19 |
Correct |
3 ms |
1364 KB |
Output is correct |
20 |
Correct |
4 ms |
1364 KB |
Output is correct |
21 |
Correct |
3 ms |
1364 KB |
Output is correct |
22 |
Correct |
4 ms |
1588 KB |
Output is correct |
23 |
Correct |
52 ms |
7620 KB |
Output is correct |
24 |
Correct |
44 ms |
7516 KB |
Output is correct |
25 |
Correct |
40 ms |
7516 KB |
Output is correct |
26 |
Correct |
40 ms |
7520 KB |
Output is correct |
27 |
Correct |
40 ms |
7560 KB |
Output is correct |
28 |
Correct |
59 ms |
11504 KB |
Output is correct |
29 |
Correct |
40 ms |
8164 KB |
Output is correct |
30 |
Correct |
62 ms |
11812 KB |
Output is correct |
31 |
Correct |
72 ms |
13852 KB |
Output is correct |
32 |
Correct |
40 ms |
7732 KB |
Output is correct |
33 |
Correct |
46 ms |
9128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1352 KB |
Output is correct |
2 |
Correct |
3 ms |
1364 KB |
Output is correct |
3 |
Correct |
3 ms |
1364 KB |
Output is correct |
4 |
Correct |
3 ms |
1364 KB |
Output is correct |
5 |
Correct |
3 ms |
1364 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
1364 KB |
Output is correct |
8 |
Correct |
4 ms |
1492 KB |
Output is correct |
9 |
Correct |
5 ms |
1492 KB |
Output is correct |
10 |
Correct |
4 ms |
1492 KB |
Output is correct |
11 |
Correct |
4 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
0 ms |
852 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Correct |
2 ms |
1236 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
2 ms |
1236 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
2 ms |
1240 KB |
Output is correct |
19 |
Correct |
3 ms |
1364 KB |
Output is correct |
20 |
Correct |
4 ms |
1364 KB |
Output is correct |
21 |
Correct |
3 ms |
1364 KB |
Output is correct |
22 |
Correct |
4 ms |
1588 KB |
Output is correct |
23 |
Correct |
52 ms |
7620 KB |
Output is correct |
24 |
Correct |
44 ms |
7516 KB |
Output is correct |
25 |
Correct |
40 ms |
7516 KB |
Output is correct |
26 |
Correct |
40 ms |
7520 KB |
Output is correct |
27 |
Correct |
40 ms |
7560 KB |
Output is correct |
28 |
Correct |
59 ms |
11504 KB |
Output is correct |
29 |
Correct |
40 ms |
8164 KB |
Output is correct |
30 |
Correct |
62 ms |
11812 KB |
Output is correct |
31 |
Correct |
72 ms |
13852 KB |
Output is correct |
32 |
Correct |
40 ms |
7732 KB |
Output is correct |
33 |
Correct |
46 ms |
9128 KB |
Output is correct |
34 |
Correct |
3 ms |
1352 KB |
Output is correct |
35 |
Correct |
3 ms |
1364 KB |
Output is correct |
36 |
Correct |
3 ms |
1364 KB |
Output is correct |
37 |
Correct |
3 ms |
1364 KB |
Output is correct |
38 |
Correct |
3 ms |
1364 KB |
Output is correct |
39 |
Correct |
3 ms |
1364 KB |
Output is correct |
40 |
Correct |
3 ms |
1364 KB |
Output is correct |
41 |
Correct |
4 ms |
1492 KB |
Output is correct |
42 |
Correct |
5 ms |
1492 KB |
Output is correct |
43 |
Correct |
4 ms |
1492 KB |
Output is correct |
44 |
Correct |
4 ms |
1492 KB |
Output is correct |
45 |
Correct |
43 ms |
7760 KB |
Output is correct |
46 |
Correct |
41 ms |
7760 KB |
Output is correct |
47 |
Correct |
41 ms |
7792 KB |
Output is correct |
48 |
Correct |
42 ms |
7680 KB |
Output is correct |
49 |
Correct |
45 ms |
7792 KB |
Output is correct |
50 |
Correct |
56 ms |
11432 KB |
Output is correct |
51 |
Correct |
63 ms |
11688 KB |
Output is correct |
52 |
Correct |
92 ms |
14400 KB |
Output is correct |
53 |
Correct |
81 ms |
14140 KB |
Output is correct |
54 |
Correct |
78 ms |
13960 KB |
Output is correct |
55 |
Correct |
110 ms |
18912 KB |
Output is correct |
56 |
Correct |
1032 ms |
93424 KB |
Output is correct |
57 |
Correct |
230 ms |
30220 KB |
Output is correct |
58 |
Correct |
245 ms |
29512 KB |
Output is correct |
59 |
Correct |
226 ms |
29340 KB |
Output is correct |