4553: [Tjoi2016&Heoi2016]序列
我们可以得出递推式 d p [ i ] = d p [ j ] + 1 , j < i , a [ j ] < = L [ i ] , R [ j ] < = a [ i ] dp[i]=dp[j]+1,j<i,a[j]<=L[i],R[j]<=a[i] dp[i]=dp[j]+1,j<i,a[j]<=L[i],R[j]<=a[i]
典型的三维偏序,然后CDQ就好了。
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,mx,F[100005],g[100005];
#include<cctype>
int read(){
int ret=0;char ch=getchar();bool f=1;
for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');
for(; isdigit(ch);ch=getchar()) ret=ret*10+ch-48;
return f?ret:-ret;
}
struct xcw{int L,R,p,id;}a[100005];
bool cmpp(xcw x,xcw y){return x.p<y.p;}
bool cmpL(xcw x,xcw y){return x.L<y.L;}
bool cmpid(xcw x,xcw y){return x.id<y.id;}
void put(int x,int p){for(;x<=mx;x+=x&-x) g[x]=max(g[x],p);}
int get(int x){int Sum=0;for(;x;x-=x&-x) Sum=max(g[x],Sum);return Sum;}
int clear(int x){for(;g[x]&&x<=mx;x+=x&-x) g[x]=0;}
void Solve(int L,int R){
if(L>=R) return;
int mid=(R+L)>>1;
Solve(L,mid);
sort(a+mid+1,a+R+1,cmpL);
for(int i=mid+1,j=L;i<=R;i++){
for(;a[j].p<=a[i].L&&j<=mid;j++) put(a[j].R,F[a[j].id]);
F[a[i].id]=max(F[a[i].id],get(a[i].p)+1);
}
for(int i=L;i<=mid;i++) clear(a[i].R);
sort(a+mid+1,a+R+1,cmpid);
Solve(mid+1,R);
sort(a+L,a+R+1,cmpp);
}
int main(){
n=read(),m=read();
for(int i=1;i<=n;i++) a[i].L=a[i].R=a[i].p=read(),a[i].id=i;
for(int i=1;i<=m;i++){
int x=read(),y=read();
a[x].L=min(y,a[x].L);
a[x].R=max(y,a[x].R);mx=max(a[x].R,mx);
}
for(int i=1;i<=n;i++) F[i]=1;
Solve(1,n);int Ans=0;
for(int i=1;i<=n;i++) Ans=max(F[i],Ans);
printf("%d\n",Ans);
return 0;
}