#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _ <<' '<<
#define print(x) cout<<#x<<": "<<(x)<<'\n'
int n, res=0;
vector<int> a, b, lf, rt;
stack<int> s;
map<int, int> mp;
struct segtree{
int n;
vector<int> st;
segtree(const int n){
this->n=n;
st.assign(n<<2, 0);
}
void upd(const int l, const int r, const int cl, const int cr, const int i, const int d) {
if (r<=cl || cr<=l || cr<=cl) return;
if (cl+1==cr) {
st[i]=max(st[i], d);
return;
}
const auto m=cl+(cr-cl>>1);
upd(l, r, cl, m, (i<<1)+1, d);
upd(l, r, m, cr, (i<<1)+2, d);
st[i]=max(st[(i<<1)+1], st[(i<<1)+2]);
}
void update(const int i, const int d) {
upd(i, i+1, 0, n, 0, d);
}
int qry(const int l, const int r, const int cl, const int cr, const int i) {
if (r<=cl || cr<=l || cr<=cl) return 0;
if (l<=cl && cr<=r) return st[i];
const auto m=cl+(cr-cl>>1);
return max(qry(l, r, cl, m, (i<<1)+1), qry(l, r, m, cr, (i<<1)+2));
}
int query(const int l, const int r) {
return qry(l, r, 0, n, 0);
}
};
int main(){
cin.tie(0)->sync_with_stdio(0);
cout.tie(0);
cin>>n;
a.resize(n);
b.resize(n);
lf.assign(n, 0);
rt=lf;
for (int i=0; i<n; i++) {
cin>>a[i];
mp[a[i]]=i;
}
for (int i=0; i<n; i++) cin>>b[i];
for (int i=0; i<n; i++) {
while (!s.empty() && a[s.top()]<=a[i]) s.pop();
if (s.empty()) lf[i]=0;
else lf[i]=s.top()+1;
s.push(i);
}
s={};
for (int i=n-1; i>=0; --i) {
while (!s.empty() && a[s.top()]<=a[i]) s.pop();
if (s.empty()) rt[i]=n-1;
else rt[i]=s.top()-1;
s.push(i);
}
s={};
segtree f(n);
for (int i=0; i<n; i++) {
if (!mp.count(b[i])) continue;
const auto idx=mp[b[i]];
if (!(lf[idx]<=i && i<=rt[idx])) continue;
const auto v=f.query(0, i)+1;
f.update(i, v);
res=max(res, v);
}
cout<<res<<'\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... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |