1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 #include <algorithm>
5 using namespace std;
6 const int N=300000;
7 int n,m,bit[N+100],f[N+100];
8 struct num{int val,maxv,minv;}x[N+100];
9 struct cdq{int x,y,id;}a[N+100];
10 inline int lowbit(int a){return a&(-a);}
11 inline bool mt(const cdq &a,const cdq &b)
12 {return (a.x==b.x)?a.id<b.id:a.x<b.x;}
13 inline void add(int i,int val)
14 {
15 while(i<=N)
16 {
17 bit[i]=(val==0)?0:max(bit[i],val);
18 i+=lowbit(i);
19 }
20 }
21 inline int sum(int i)
22 {
23 int ret=0;
24 while(i)
25 ret=max(ret,bit[i]),i-=lowbit(i);
26 return ret;
27 }
28 void cdq(int l,int r)
29 {
30 if(l==r){f[l]=max(f[l],1);return;}
31 int mi=(l+r)>>1;
32 cdq(l,mi);
33 for(int i=l;i<=r;i++)
34 {
35 if(i<=mi)a[i].x=x[i].val,a[i].y=x[i].maxv;
36 else a[i].x=x[i].minv,a[i].y=x[i].val;
37 a[i].id=i;
38 }
39 sort(a+l,a+r+1,mt);
40 for(int i=l;i<=r;i++)
41 {
42 if(a[i].id<=mi)add(a[i].y,f[a[i].id]);
43 else f[a[i].id]=max(sum(a[i].y)+1,f[a[i].id]);
44 }
45 for(int i=l;i<=r;i++)add(a[i].y,0);
46 cdq(mi+1,r);
47 }
48 int main()
49 {
50 scanf("%d%d",&n,&m);int u,v,ans=0;
51 for(int i=1;i<=n;i++)
52 scanf("%d",&x[i].val),x[i].minv=x[i].maxv=x[i].val;
53 while(m--)
54 {
55 scanf("%d%d",&u,&v);
56 x[u].maxv=max(x[u].maxv,v);
57 x[u].minv=min(x[u].minv,v);
58 }
59 cdq(1,n);
60 for(int i=1;i<=n;i++)ans=max(ans,f[i]);
61 printf("%d\n",ans);
62 }