# 4553: [Tjoi2016&Heoi2016]序列

#include<bits/stdc++.h>
#define rep(i,k,n) for(int i=k;i<=n;i++)
#define rep2(i,k,n) for(int i=k;i>=n;i--)
#define ls x<<1
#define rs x<<1|1
using namespace std;
const int N=4e5+7;
int n,a[N],mx[N],mi[N],m,dp[N],b[N],M[N],len=0;
bool cmp_a(int x,int y){
return a[x]<a[y];
}
bool cmp_mx(int x,int y){
return mx[x]<mx[y];
}
void ch(int x,int l,int r,int pos,int d){
if(l==r){if(!d)M[x]=0;else M[x]=max(M[x],d);return;}
int mid=(l+r)>>1;
if(pos<=mid)ch(ls,l,mid,pos,d);
else ch(rs,mid+1,r,pos,d);
M[x]=max(M[ls],M[rs]);
}
int Q(int x,int l,int r,int pos){
if(r<=pos)return M[x];
int mid=(l+r)>>1;
if(pos<=mid)return Q(ls,l,mid,pos);
else return max(M[ls],Q(rs,mid+1,r,pos));
}
void solve(int l,int r){
if(l>=r)return;
int mid=(l+r)>>1;
solve(l,mid);
rep(i,l,r)b[i]=i;
sort(b+l,b+mid+1,cmp_mx);
sort(b+mid+1,b+r+1,cmp_a);
int pos=mid+1;
rep(i,l,mid){
ch(1,1,len,a[b[i]],dp[b[i]]);

for(;pos<=r && a[b[pos]]<mx[b[i]];pos++); ////

for(;pos<=r && a[b[pos]]>=mx[b[i]] && (a[b[pos]]<mx[b[i+1]] || i==mid);pos++){
dp[b[pos]]=max(dp[b[pos]],Q(1,1,len,mi[b[pos]])+1);
}
}
rep(i,l,mid)ch(1,1,len,a[b[i]],0);

solve(mid+1,r);
}
int main(){
scanf("%d%d",&n,&m);
rep(i,1,n)scanf("%d",&a[i]),mx[i]=mi[i]=a[i],dp[i]=1,len=max(len,a[i]);
int x,y;
rep(i,1,m){
scanf("%d%d",&x,&y);
mx[x]=max(mx[x],y);
mi[x]=min(mi[x],y);
len=max(len,y);
}
solve(1,n);
int ans=0;
rep(i,1,n)ans=max(ans,dp[i]);
printf("%d\n",ans);
}

#### BZOJ4553 [Tjoi2016&Heoi2016]序列

2016-05-16 21:12:27

#### bzoj 4553: [Tjoi2016&Heoi2016]序列

2016-05-06 15:14:42

#### BZOJ 4553: [Tjoi2016&Heoi2016]序列

2016-05-20 19:58:01

#### BZOJ 4553 [Tjoi2016&Heoi2016]序列

2017-04-02 00:31:09

#### [Tjoi2016&Heoi2016]序列

2016-07-11 15:45:58

#### bzoj4553【TJOI2016&HEOI2016】序列

2016-06-15 00:13:39

#### 【bzoj4552】【Tjoi2016&Heoi2016】【排序】【线段树】

2016-05-17 09:46:27

#### bzoj 4553: [Tjoi2016&Heoi2016]序列 （CDQ分治+DP+树状数组）

2017-03-25 18:31:41

#### bzoj 4553: [Tjoi2016&Heoi2016]序列 cdq分治+树状数组

2017-04-02 15:09:41