//Subtask 2
#include<bits/stdc++.h>
using namespace std;
int st[400005];
int a[100005], b[100005], dp[100005], sparse[100005][20];
void update(int node, int l, int r, int p, int v)
{
if(l > p || r < p) return;
if(l == p && r == p) st[node] = v;
else{
int mid = (l+r)/2;
update(node*2, l, mid, p, v);
update(node*2+1, mid+1, r, p, v);
st[node] = max(st[node*2], st[node*2+1]);
}
}
int query(int node, int l, int r, int tl, int tr)
{
if(l > tr || r < tl) return -1e9;
if(l >= tl && r <= tr) return st[node];
else return max(query(node*2, l, (l+r)/2, tl, tr), query(node*2+1, (l+r)/2+1, r, tl, tr));
}
int mquery(int l, int r)
{
if(l > r) swap(l, r);
int x = __lg(r-l+1);
return max(sparse[l][x], sparse[r-(1<<x)+1][x]);
}
void preprocess(int n)
{
for(int j = 0; j < 19; j++){
for(int i = 1; i <= n; i++){
sparse[i][j+1] = sparse[i][j];
if(i+(1 << j) <= n) sparse[i][j+1] = max(sparse[i][j+1], sparse[i+(1 << j)][j]);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n; cin>>n;
for(int i = 1; i <= n; i++) cin>>a[i];
for(int i = 1; i <= n; i++) cin>>b[i];
for(int i = 1; i <= n; i++) sparse[i][0] = a[i];
preprocess(n);
map<int, int> place;
for(int i = 1; i <= n; i++) place[a[i]] = i;
int ans = 0;
for(int i = 1; i <= n; i++){
if(place[b[i]] == 0 || mquery(place[b[i]], i) > b[i]) dp[i] = -1e9;
else{
dp[i] = query(1, 1, n, 1, place[b[i]])+1;
update(1, 1, n, place[b[i]], dp[i]);
//cout<<i<<" "<<dp[i]<<" "<<place[b[i]]<<endl;
}
ans = max(ans, dp[i]);
}
cout<<ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |