#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define ll long long
#define rep(i,n,N) for(int i = n; i<=N; ++i)
#define rap(i,n,N) for(int i = n; i>=N; --i)
#define For(i,n,N) for(int i = n; i< N; ++i)
#define endl '\n'
#define pb push_back
#define all(x) x.begin(),x.end()
#define mems(x,y) memset(x,y,sizeof x)
#define ari(x) array<int,x>
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fi first
#define se second
const int MAX = 1e5 + 5;
mt19937 rng(time(NULL));
int n,x[MAX],y[MAX],cnt;
vector<int> v;
#define lc (id<<1)
#define rc ((id<<1)|1)
struct rmqSegtree{
int tree[1<<18];
int n,a,b;
void build(int id,int le,int ri){
if(le==ri){
tree[id] = x[le];
return;
}
int mid = le+ri>>1;
build(lc,le,mid), build(rc,mid+1,ri);
tree[id] = max(tree[lc], tree[rc]);
return;
}
int que(int id,int le,int ri){
if(le>b || ri<a)return 0;
if(le>=a && ri<=b)return tree[id];
int mid = le+ri>>1;
return max(que(lc,le,mid), que(rc,mid+1,ri));
}
int que(int L,int R){
a = L, b = R;
return que(1,1,n);
}
}rmq;
struct ansSegtree{
int tree[1<<18];
int n,val,a,b;
void upd(int id,int le,int ri){
if(le==ri){
tree[id] = val;
return;
}
int mid = le+ri>>1;
if(a<=mid)upd(lc,le,mid);
else upd(rc,mid+1,ri);
tree[id] = max(tree[lc], tree[rc]);
return;
}
int que(int id,int le,int ri){
if(le>b || ri<a)return 0;
if(le>=a && ri<=b)return tree[id];
int mid = le+ri>>1;
return max(que(lc,le,mid), que(rc,mid+1,ri));
}
int que(int L,int R){
a = L, b = R;
return que(1,1,n);
}
void upd(int pos,int _v){
a = pos;
val = _v;
upd(1,1,n);
}
}tree[2];
vector<int> z[MAX];
int main(){
// freopen("input.4_j", "r", stdin);
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n;
rep(i,1,n)cin>>x[i], v.pb(x[i]);
sort(all(v));
v.erase(unique(all(v)), v.end());
rep(i,1,n)x[i] = upper_bound(all(v), x[i]) - v.begin();
auto it = v.begin();
rep(i,1,n){
cin>>y[i];
it = lower_bound(all(v), y[i]);
y[i] = it==v.end() || *it!=y[i] ? 0 : it-v.begin()+1;
}
rmq.n = tree[0].n = tree[1].n = n;
rmq.build(1,1,n);
rep(i,1,n)z[x[i]].pb(i);
int pos,val,wot;
rep(i,1,n){
if(x[i]<=y[i]){
it = lower_bound(all(z[y[i]]), i);
pos = -1;
if(it != z[y[i]].end()){
pos = *it;
val = max(tree[0].que(1,y[i]), tree[1].tree[1]) + (rmq.que(i,pos)==y[i]);
tree[0].upd(y[i], val);
}
if(pos==i){
tree[1].upd(y[i], val);
}
else if(it != z[y[i]].begin()){
pos = *prev(it);
tree[1].upd(y[i], tree[1].que(y[i],n) + (rmq.que(pos,i)==y[i]));
}
}
tree[1].upd(x[i], max(tree[0].que(1,x[i]), tree[1].tree[1]));
}
cout<<max(tree[0].tree[1], tree[1].tree[1])<<endl;
return 0;
}
Compilation message
exam.cpp: In member function 'void rmqSegtree::build(int, int, int)':
exam.cpp:37:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int mid = le+ri>>1;
| ~~^~~
exam.cpp: In member function 'int rmqSegtree::que(int, int, int)':
exam.cpp:45:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int mid = le+ri>>1;
| ~~^~~
exam.cpp: In member function 'void ansSegtree::upd(int, int, int)':
exam.cpp:62:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
62 | int mid = le+ri>>1;
| ~~^~~
exam.cpp: In member function 'int ansSegtree::que(int, int, int)':
exam.cpp:71:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int mid = le+ri>>1;
| ~~^~~
exam.cpp: In function 'int main()':
exam.cpp:105:14: warning: unused variable 'wot' [-Wunused-variable]
105 | int pos,val,wot;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
13 ms |
3304 KB |
Output is correct |
3 |
Correct |
42 ms |
8620 KB |
Output is correct |
4 |
Correct |
61 ms |
5620 KB |
Output is correct |
5 |
Correct |
87 ms |
9036 KB |
Output is correct |
6 |
Correct |
31 ms |
5804 KB |
Output is correct |
7 |
Correct |
60 ms |
5536 KB |
Output is correct |
8 |
Correct |
76 ms |
7500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
4 ms |
3028 KB |
Output is correct |
5 |
Correct |
4 ms |
3028 KB |
Output is correct |
6 |
Correct |
4 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3028 KB |
Output is correct |
2 |
Correct |
36 ms |
5972 KB |
Output is correct |
3 |
Correct |
92 ms |
10052 KB |
Output is correct |
4 |
Correct |
96 ms |
9932 KB |
Output is correct |
5 |
Correct |
94 ms |
9148 KB |
Output is correct |
6 |
Correct |
101 ms |
9936 KB |
Output is correct |
7 |
Correct |
101 ms |
10032 KB |
Output is correct |
8 |
Correct |
119 ms |
10048 KB |
Output is correct |
9 |
Correct |
127 ms |
9932 KB |
Output is correct |
10 |
Correct |
87 ms |
10012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
4 ms |
3028 KB |
Output is correct |
11 |
Correct |
4 ms |
3028 KB |
Output is correct |
12 |
Correct |
4 ms |
3064 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
1 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2692 KB |
Output is correct |
23 |
Correct |
3 ms |
2900 KB |
Output is correct |
24 |
Correct |
4 ms |
2900 KB |
Output is correct |
25 |
Correct |
4 ms |
2900 KB |
Output is correct |
26 |
Correct |
5 ms |
2900 KB |
Output is correct |
27 |
Correct |
5 ms |
2900 KB |
Output is correct |
28 |
Correct |
5 ms |
3028 KB |
Output is correct |
29 |
Correct |
4 ms |
2960 KB |
Output is correct |
30 |
Correct |
5 ms |
3088 KB |
Output is correct |
31 |
Correct |
5 ms |
3028 KB |
Output is correct |
32 |
Correct |
5 ms |
3028 KB |
Output is correct |