#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
struct RMQ
{
pii max_[50001][16];
void setup(vi v)
{
int n = siz(v);
rep(i,n) max_[i][0] = {v[i],i};
rep2(bit,1,15)
{
rep(i,n)
{
max_[i][bit] = max(max_[i][bit-1],(i+(1<<(bit-1)) < n ? max_[i+(1 << (bit-1))][bit-1] : (pii){-1,-1}));
}
}
}
int query(int l, int r)
{
int lg = __lg(r-l+1);
pii ans = max(max_[l][lg],max_[r-(1<<lg)+1][lg]);
return ans.ss;
}
};
unordered_map<ll,ll> ansmap;
int n,m;
ll A[50001];
ll B[50001];
RMQ xrmq;
RMQ yrmq;
ll solve(int x, int y, int d)
{
ll h = d + x*4LL + y*4LL*500001LL;
if(ansmap.find(h) != ansmap.end()) return ansmap[h];
ll final_ans = 0;
if(d == 0)
{
int l = y+1;
int r = m-1;
int ans = -1;
while(l <= r)
{
int mid = (l+r)/2;
if(B[yrmq.query(y+1,mid)] > A[x])
{
ans = mid;
r = mid-1;
}
else
{
l = mid+1;
}
}
if(ans == -1)
{
final_ans = (m-1)-y;
}
else
{
final_ans = ans-y+max(solve(x,ans,1),solve(x,ans,3));
}
}
if(d == 1)
{
int l = x+1;
int r = n-1;
int ans = -1;
while(l <= r)
{
int mid = (l+r)/2;
if(A[xrmq.query(x+1,mid)] > B[y])
{
ans = mid;
r = mid-1;
}
else
{
l = mid+1;
}
}
if(ans == -1)
{
final_ans = (n-1)-x;
}
else
{
final_ans = ans-x+max(solve(ans,y,0),solve(ans,y,2));
}
}
if(d == 2)
{
int l = 0;
int r = y-1;
int ans = -1;
while(l <= r)
{
int mid = (l+r)/2;
if(B[yrmq.query(mid,y-1)] > A[x])
{
ans = mid;
l = mid+1;
}
else
{
r = mid-1;
}
}
if(ans == -1)
{
final_ans = y;
}
else
{
final_ans = y-ans+max(solve(x,ans,1),solve(x,ans,3));
}
}
if(d == 3)
{
int l = 0;
int r = x-1;
int ans = -1;
while(l <= r)
{
int mid = (l+r)/2;
if(A[xrmq.query(mid,x-1)] > B[y])
{
ans = mid;
l = mid+1;
}
else
{
r = mid-1;
}
}
if(ans == -1)
{
final_ans = x;
}
else
{
final_ans = x-ans+max(solve(ans,y,0),solve(ans,y,2));
}
}
ansmap[h] = final_ans;
return final_ans;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
int q;
cin >> n >> m >> q;
rep(i,n) cin >> A[i];
rep(i,m) cin >> B[i];
vi a(n);
vi b(m);
rep(i,n) a[i] = A[i];
rep(i,m) b[i] = B[i];
xrmq.setup(a);
yrmq.setup(b);
rep(qq,q)
{
int x,y;
cin >> x >> y;
x--;
y--;
cout << max({solve(x,y,0),solve(x,y,1),solve(x,y,2),solve(x,y,3)}) << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |