// #pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

//typedef tree<
//        pair<int, int>, // change type
//        null_type,
//        less<pair<int, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

#define     pii             pair<int ,int>
#define     ff              first
#define     ss              second
#define     pb(x)           push_back(x )
#define     mp              make_pair
#define     sz(x)           x.size()
#define     clr(x)          x.clear()
#define     mem(x ,y)       memset(x , y , sizeof x )
#define     all(a )         a.begin() , a.end()
#define     endl            "\n"
#define     si(x )          scanf("%d", &x)
#define     sl(x )          scanf("%I64d", &x)
#define     sd(x )          scanf("%lf", &x)
#define     sii(x,y )       scanf("%d%d", &x,&y)
#define     siii(x,y ,z)    scanf("%d%d%d ", &x,&y , &z)
#define     rep(i ,n )      for(int i = 0 ; i < n ; i++ )
#define     repi(i ,n )     for(int i = 1 ; i <= n ; i++ )
#define     forn(i ,x,n )   for(int i = x ; i <= n ; i++ )
#define     repit(i, c)     for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )
#define     dbg(args...)    do {   cerr << #args << ' ' ;  print(args); } while(0); cerr<< endl ;

typedef long long ll  ;
typedef unsigned long long  ull ;
typedef vector<int> vi  ;

const int  mod =  1e9+7;
const int  inf = 1e9+7  ;
const long long  infL = 1e18 ;

int setbit(int n, int pos  ) { return n = n|(1<< pos) ; }
int resetbit(int n,int pos ) {  return n =  n & ~(1<<pos ); }
bool checkbit(int  n,int pos ) { return (bool ) (n&(1<<pos))  ; }

template < typename T > T mul(T x, T y   ) { x %= mod ; y %= mod ; return (x*y)%mod  ;  }
template < typename T > T add(T x, T y  ) { x %= mod ; y %= mod ; return (x+y)%mod ;  }
template < typename T > T modPow(T x, T k ,T mod ) { if(k == 0 ) return 1; if(k&1  ) { return ( (x%mod)*modPow(x,k-1, mod )%mod )%mod ; } else { T ret = modPow(x,k/2 , mod ) ; ret %= mod ; return (ret*ret )%mod; } }
template < typename T > T ext_gcd(T num1 , T num2, T &X , T &Y ) { T r2,r1, q2, q1 , x2, x1, y2 ,y1 , x,  y, r   ; x2 = 1; y2 = 0;  x1 = 0; y1 = 1; for( r2 =num1 ,r1 = num2 ;r1 != 0 ; y2 = y1, y1 = y,x2 = x1 ,x1 = x ,r2 = r1, r1 = r) {q1 = r2/r1; x = x2 - q1*x1; y = y2 - q1*y1; r = r2%r1 ; } X = x2 ; Y = y2 ; return r2 ;  }
template < typename T > T modInv( T x , T m ) {  return modPow(x,m-2 , m) ; }  // long long ret, t ;// ext_gcd( x, m ,ret ,t  ) ;// ret %= m ;// if(ret<0) ret += m  ; // return ret;}
template < typename T > void print(const T& v) {    cerr << v << ' ' ;}
template < typename T1, typename... T2 > void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}
// debuging ...
template<typename T >
void dbga(T a[] ,int n=10 ) {
    rep(i,n )cerr<<a[i]<<' ' ;
    cerr<< endl;
}

template < typename F,  typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "[ " << p.first << ", " << p.second << " ]";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "[ "; rep(i , sz( v)  )  os << v[i] << ' ' ; return os << " ]" ;
}

//int dx[] = {0 , 0 ,+1, -1 } ;
//int dy[] = {-1, +1,0 , 0 };

int modAdd(int &x , int y) {
    x += y ;
    if(x>= mod ) x-=mod;
    return x ;
}
const int M = (int )1e6+7;
const int mx = (int)5000+123;
const int nx  = 500+ 2 ;

////  code from here ...

int a[M+2 ], ans[M +2];
int  upd[M+2 ] ;
void solve() {
    int n, q, x,type ,r=-1 ,lptr,rptr ; cin >> n>> q;
    repi(i,n )   cin >>a[i] ;
    repi(i, q) {
        cin >> type >> x  ;
        upd[x   ] = type   ;
        r = max( r , x )  ;

    }

    for(int i= n; i>r;i--)
        ans[i] = a[i] ;

    sort(a+1,a+r+1) ;
    lptr=1;
    rptr=r ;

    for(int i=r; i>0 ; i--) {
        if(upd[i ]==0 )upd[i] = upd[i+1]  ;
        if(upd[i]==1) ans[i] = a[rptr--] ;
        else ans[i] = a[lptr++] ;

    }

    //   dbg(r,lptr, rptr);
    repi(i,n) cout<< ans[i] << ' ';
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
     //   freopen("out.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(false);  cin.tie(0); cout.tie(0);
    int t=1 , c= 0 ; // si( t) ;
    //cin>>t ;
    while(t--) {
       // printf("Case %d: ",++c)  ;
        solve();

    }

}


/*
5 2
1 2 3  4 5
2 3
1 5

*/
// #pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

//typedef tree<
//        pair<int, int>, // change type
//        null_type,
//        less<pair<int, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

#define     pii             pair<int ,int>
#define     ff              first
#define     ss              second
#define     pb(x)           push_back(x )
#define     mp              make_pair
#define     sz(x)           x.size()
#define     clr(x)          x.clear()
#define     mem(x ,y)       memset(x , y , sizeof x )
#define     all(a )         a.begin() , a.end()
#define     endl            "\n"
#define     si(x )          scanf("%d", &x)
#define     sl(x )          scanf("%I64d", &x)
#define     sd(x )          scanf("%lf", &x)
#define     sii(x,y )       scanf("%d%d", &x,&y)
#define     siii(x,y ,z)    scanf("%d%d%d ", &x,&y , &z)
#define     rep(i ,n )      for(int i = 0 ; i < n ; i++ )
#define     repi(i ,n )     for(int i = 1 ; i <= n ; i++ )
#define     forn(i ,x,n )   for(int i = x ; i <= n ; i++ )
#define     repit(i, c)     for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )
#define     dbg(args...)    do {   cerr << #args << ' ' ;  print(args); } while(0); cerr<< endl ;

typedef long long ll  ;
typedef unsigned long long  ull ;
typedef vector<int> vi  ;

const int  mod =  1e9+7;
const int  inf = 1e9+7  ;
const long long  infL = 1e18 ;

int setbit(int n, int pos  ) { return n = n|(1<< pos) ; }
int resetbit(int n,int pos ) {  return n =  n & ~(1<<pos ); }
bool checkbit(int  n,int pos ) { return (bool ) (n&(1<<pos))  ; }

template < typename T > T mul(T x, T y   ) { x %= mod ; y %= mod ; return (x*y)%mod  ;  }
template < typename T > T add(T x, T y  ) { x %= mod ; y %= mod ; return (x+y)%mod ;  }
template < typename T > T modPow(T x, T k ,T mod ) { if(k == 0 ) return 1; if(k&1  ) { return ( (x%mod)*modPow(x,k-1, mod )%mod )%mod ; } else { T ret = modPow(x,k/2 , mod ) ; ret %= mod ; return (ret*ret )%mod; } }
template < typename T > T ext_gcd(T num1 , T num2, T &X , T &Y ) { T r2,r1, q2, q1 , x2, x1, y2 ,y1 , x,  y, r   ; x2 = 1; y2 = 0;  x1 = 0; y1 = 1; for( r2 =num1 ,r1 = num2 ;r1 != 0 ; y2 = y1, y1 = y,x2 = x1 ,x1 = x ,r2 = r1, r1 = r) {q1 = r2/r1; x = x2 - q1*x1; y = y2 - q1*y1; r = r2%r1 ; } X = x2 ; Y = y2 ; return r2 ;  }
template < typename T > T modInv( T x , T m ) {  return modPow(x,m-2 , m) ; }  // long long ret, t ;// ext_gcd( x, m ,ret ,t  ) ;// ret %= m ;// if(ret<0) ret += m  ; // return ret;}
template < typename T > void print(const T& v) {    cerr << v << ' ' ;}
template < typename T1, typename... T2 > void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}
// debuging ...
template<typename T >
void dbga(T a[] ,int n=10 ) {
    rep(i,n )cerr<<a[i]<<' ' ;
    cerr<< endl;
}

template < typename F,  typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "[ " << p.first << ", " << p.second << " ]";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "[ "; rep(i , sz( v)  )  os << v[i] << ' ' ; return os << " ]" ;
}

//int dx[] = {0 , 0 ,+1, -1 } ;
//int dy[] = {-1, +1,0 , 0 };

int modAdd(int &x , int y) {
    x += y ;
    if(x>= mod ) x-=mod;
    return x ;
}
const int M = (int )1e6+7;
const int mx = (int)5000+123;
const int nx  = 500+ 2 ;

////  code from here ...

int a[M+2 ], ans[M +2];
pii upd[M+2 ] ;
void solve() {
    int n, q, x,type ,r=-1 ,lptr,rptr ; cin >> n>> q;
    repi(i,n )   cin >>a[i] ;
    repi(i, q) {
        cin >> type >> x  ;
        upd[x   ] = mp(i,type)   ;
        r = max( r , x )  ;

    }

    for(int i= n; i>r;i--)
        ans[i] = a[i] ;

    sort(a+1,a+r+1) ;
    lptr=1;
    rptr=r ;
    pii last = upd[r] ;
    for(int i=r; i>0 ; i--) {
        if(upd[i].ff>last.ff )
            last= upd[i] ;

        if(last.ss==1) ans[i] = a[rptr--] ;
        else ans[i] = a[lptr++] ;

    }

    //   dbg(r,lptr, rptr);
    repi(i,n) cout<< ans[i] << ' ';
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
     //   freopen("out.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(false);  cin.tie(0); cout.tie(0);
    int t=1 , c= 0 ; // si( t) ;
    //cin>>t ;
    while(t--) {
       // printf("Case %d: ",++c)  ;
        solve();

    }

}


// #pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

//typedef tree<
//        pair<int, int>, // change type
//        null_type,
//        less<pair<int, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

#define     pii             pair<int ,int>
#define     ff              first
#define     ss              second
#define     pb(x)           push_back(x )
#define     mp              make_pair
#define     sz(x)           x.size()
#define     clr(x)          x.clear()
#define     mem(x ,y)       memset(x , y , sizeof x )
#define     all(a )         a.begin() , a.end()
#define     endl            "\n"
#define     si(x )          scanf("%d", &x)
#define     sl(x )          scanf("%I64d", &x)
#define     sd(x )          scanf("%lf", &x)
#define     sii(x,y )       scanf("%d%d", &x,&y)
#define     siii(x,y ,z)    scanf("%d%d%d ", &x,&y , &z)
#define     rep(i ,n )      for(int i = 0 ; i < n ; i++ )
#define     repi(i ,n )     for(int i = 1 ; i <= n ; i++ )
#define     forn(i ,x,n )   for(int i = x ; i <= n ; i++ )
#define     repit(i, c)     for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )
#define     dbg(args...)    do {   cerr << #args << ' ' ;  print(args); } while(0); cerr<< endl ;

typedef long long ll  ;
typedef unsigned long long  ull ;
typedef vector<int> vi  ;

const int  mod =  1e9+7;
const int  inf = 1e9+7  ;
const long long  infL = 1e18 ;

int setbit(int n, int pos  ) { return n = n|(1<< pos) ; }
int resetbit(int n,int pos ) {  return n =  n & ~(1<<pos ); }
bool checkbit(int  n,int pos ) { return (bool ) (n&(1<<pos))  ; }

template < typename T > T mul(T x, T y   ) { x %= mod ; y %= mod ; return (x*y)%mod  ;  }
template < typename T > T add(T x, T y  ) { x %= mod ; y %= mod ; return (x+y)%mod ;  }
template < typename T > T modPow(T x, T k ,T mod ) { if(k == 0 ) return 1; if(k&1  ) { return ( (x%mod)*modPow(x,k-1, mod )%mod )%mod ; } else { T ret = modPow(x,k/2 , mod ) ; ret %= mod ; return (ret*ret )%mod; } }
template < typename T > T ext_gcd(T num1 , T num2, T &X , T &Y ) { T r2,r1, q2, q1 , x2, x1, y2 ,y1 , x,  y, r   ; x2 = 1; y2 = 0;  x1 = 0; y1 = 1; for( r2 =num1 ,r1 = num2 ;r1 != 0 ; y2 = y1, y1 = y,x2 = x1 ,x1 = x ,r2 = r1, r1 = r) {q1 = r2/r1; x = x2 - q1*x1; y = y2 - q1*y1; r = r2%r1 ; } X = x2 ; Y = y2 ; return r2 ;  }
template < typename T > T modInv( T x , T m ) {  return modPow(x,m-2 , m) ; }  // long long ret, t ;// ext_gcd( x, m ,ret ,t  ) ;// ret %= m ;// if(ret<0) ret += m  ; // return ret;}
template < typename T > void print(const T& v) {    cerr << v << ' ' ;}
template < typename T1, typename... T2 > void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}
// debuging ...
template<typename T >
void dbga(T a[] ,int n=10 ) {
    rep(i,n )cerr<<a[i]<<' ' ;
    cerr<< endl;
}

template < typename F,  typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "[ " << p.first << ", " << p.second << " ]";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "[ "; rep(i , sz( v)  )  os << v[i] << ' ' ; return os << " ]" ;
}

//int dx[] = {0 , 0 ,+1, -1 } ;
//int dy[] = {-1, +1,0 , 0 };

int modAdd(int &x , int y) {
    x += y ;
    if(x>= mod ) x-=mod;
    return x ;
}
const int M = (int )1e6+7;
const int mx = (int)5000+123;
const int nx  = 500+ 2 ;

////  code from here ...

int a[M+2 ], ans[M +2];
int  upd[M+2 ] ;
void solve() {
    int n, q, x,type ,r=-1 ,lptr,rptr ; cin >> n>> q;
    repi(i,n )   cin >>a[i] ;
    repi(i, q) {
        cin >> type >> x  ;
        upd[x   ] = type   ;
        r = max( r , x )  ;

    }

    for(int i= n; i>r;i--)
        ans[i] = a[i] ;

    sort(a+1,a+r+1) ;
    lptr=1;
    rptr=r ;
    for(int i=r; i>0 ; i--) {
        if(upd[i]==1) ans[i] = a[rptr--] ;
        else ans[i] = a[lptr++] ;

    }

    //   dbg(r,lptr, rptr);
    repi(i,n) cout<< ans[i] << ' ';
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
     //   freopen("out.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(false);  cin.tie(0); cout.tie(0);
    int t=1 , c= 0 ; // si( t) ;
    //cin>>t ;
    while(t--) {
       // printf("Case %d: ",++c)  ;
        solve();

    }

}


// #pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

//typedef tree<
//        pair<int, int>, // change type
//        null_type,
//        less<pair<int, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

#define     pii             pair<int ,int>
#define     ff              first
#define     ss              second
#define     pb(x)           push_back(x )
#define     mp              make_pair
#define     sz(x)           x.size()
#define     clr(x)          x.clear()
#define     mem(x ,y)       memset(x , y , sizeof x )
#define     all(a )         a.begin() , a.end()
#define     endl            "\n"
#define     si(x )          scanf("%d", &x)
#define     sl(x )          scanf("%I64d", &x)
#define     sd(x )          scanf("%lf", &x)
#define     sii(x,y )       scanf("%d%d", &x,&y)
#define     siii(x,y ,z)    scanf("%d%d%d ", &x,&y , &z)
#define     rep(i ,n )      for(int i = 0 ; i < n ; i++ )
#define     repi(i ,n )     for(int i = 1 ; i <= n ; i++ )
#define     forn(i ,x,n )   for(int i = x ; i <= n ; i++ )
#define     repit(i, c)     for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )
#define     dbg(args...)    do {   cerr << #args << ' ' ;  print(args); } while(0); cerr<< endl ;

typedef long long ll  ;
typedef unsigned long long  ull ;
typedef vector<int> vi  ;

const int  mod =  1e9+7;
const int  inf = 1e9+7  ;
const long long  infL = 1e18 ;

int setbit(int n, int pos  ) { return n = n|(1<< pos) ; }
int resetbit(int n,int pos ) {  return n =  n & ~(1<<pos ); }
bool checkbit(int  n,int pos ) { return (bool ) (n&(1<<pos))  ; }

template < typename T > T mul(T x, T y   ) { x %= mod ; y %= mod ; return (x*y)%mod  ;  }
template < typename T > T add(T x, T y  ) { x %= mod ; y %= mod ; return (x+y)%mod ;  }
template < typename T > T modPow(T x, T k ,T mod ) { if(k == 0 ) return 1; if(k&1  ) { return ( (x%mod)*modPow(x,k-1, mod )%mod )%mod ; } else { T ret = modPow(x,k/2 , mod ) ; ret %= mod ; return (ret*ret )%mod; } }
template < typename T > T ext_gcd(T num1 , T num2, T &X , T &Y ) { T r2,r1, q2, q1 , x2, x1, y2 ,y1 , x,  y, r   ; x2 = 1; y2 = 0;  x1 = 0; y1 = 1; for( r2 =num1 ,r1 = num2 ;r1 != 0 ; y2 = y1, y1 = y,x2 = x1 ,x1 = x ,r2 = r1, r1 = r) {q1 = r2/r1; x = x2 - q1*x1; y = y2 - q1*y1; r = r2%r1 ; } X = x2 ; Y = y2 ; return r2 ;  }
template < typename T > T modInv( T x , T m ) {  return modPow(x,m-2 , m) ; }  // long long ret, t ;// ext_gcd( x, m ,ret ,t  ) ;// ret %= m ;// if(ret<0) ret += m  ; // return ret;}
template < typename T > void print(const T& v) {    cerr << v << ' ' ;}
template < typename T1, typename... T2 > void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}
// debuging ...
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "[ " << p.first << ", " << p.second << " ]";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "[ "; rep(i , sz( v)  )  os << v[i] << ' ' ; return os << " ]" ;
}

//int dx[] = {0 , 0 ,+1, -1 } ;
//int dy[] = {-1, +1,0 , 0 };

int modAdd(int &x , int y) {
    x += y ;
    if(x>= mod ) x-=mod;
    return x ;
}
const int M = (int )5e5+7;
const int mx = (int)5000+123;
const int nx  = 500+ 2 ;

////  code from here ...
ll fail[M+2 ] , cnt[M+2] ,n, m ;
ll cnt2[M +2] ;
string  s  , str ;

void kmp() {
    fail[1]= 0 ;
    s = s ; // 1  based index
    for(int i=2; i<sz(s) ;i++) {
        int curr= fail[i-1];
        while(curr>0 and s[curr+1]!=s[i] and cnt[curr+1]!=cnt[i] ) curr=fail[curr ];
        if(s[curr+1]==s[i] and cnt[curr+1]== cnt[i ])  ++curr ;
        fail[i] = curr ;

    }
    //repi(i, sz(s)-1) cout << fail[i] << " " ; cout <<endl;
}

ll func() {
    ll  ans =0 ;
    int curr=  0  ,k =1;
   // dbg(s, str ) ;
    m = sz(s)-1;
    rep(i,sz(str ) ) {

        while(curr>0 and str[i]!=s[curr+1] and cnt2[i]!=cnt[curr+1] )
            curr=fail[curr] ;
        //first pos
        // dbg("u " ,i , curr) ;
        if(curr+1== 1  ) {
            if(str[i]==s[curr+1] and cnt2[i]>=cnt[curr+1 ] ) {
          //      dbg("first " , i,  curr  ,str[i] , s[curr+1]  ) ;
                curr++ ;
            }

        } else if(curr+1<m ) {
            if(str[i ]==s[curr+1] and cnt2[i]== cnt[curr+1] )
                curr++ ;
        //        dbg("sec " , i , curr )  ;
        }

        if(curr+1==m ) {
            int x = i-m+2;
           // dbg("out  " , i+1,  curr+1  ,str[i+1] , s[curr+1] ,x  ) ;

            if(str[i+1]==s[curr+1 ] and cnt2[i+1]>=cnt[curr+1 ] and
               str[x]==s[1] and  cnt2[x]>=cnt[1] ) {

                ans++ ;
                curr= fail[curr]  ;
             //   dbg("thr  " , i,  curr  ,str[i+1] , s[curr+1]  ) ;

            }
        }
        // dbg(i, curr, ans );
        // cout<<endl;
    }
    return ans ;

}

void  init(){
    int x, n , f=1 , r,c ;
    sii( n,m ) ;  char ch ;
    repi(i,n ) {
        scanf("%d-%c", &x, &ch    ) ;
        if(i==0) {
            str+= ch ;
            cnt2[i]+=x ;
        }else {
            if(str[sz(str)-1] == ch) {
                cnt2[sz(str)-1] += x;

            } else {
                str+= ch ;
                cnt2[sz(str)-1] += x;

            }
        }

    }
    s += '-' ;

    repi(i, m ) {
        scanf("%d-%c", &x, &ch    ) ;
        if(i==0) {
            s += ch ;
            cnt[i]+=x ;
        }else {
            if(s[sz(s )-1] == ch) {
                cnt[sz(s)-1] += x;

            } else {
                s+= ch ;
                cnt[sz(s )-1] += x;

            }
        }

    }

    // dbg(sz(s) , sz(str )  ) ;
    // repi(i, sz(s)-1 ) cout << cnt[i] << ' ' ; cout << endl ;
    // rep(i, sz(str) ) cout << cnt2[i] << ' ' ; cout << endl ;

}

void solve() {
    init() ;
    ll ans=0;
    if(sz(s) ==2) {
        rep(i, sz(str ) ) {
            if(str[i]==s[1] ) {
                if(cnt2[i]>= cnt[1]   ) {

                    ans += (cnt2[i]-cnt[1]+1LL)*1LL  ;

                }
            }

        }
        printf("%I64d", ans   ) ;
        return ;
    }
    kmp() ;
    ans =  func( ) ;
    printf("%I64d", ans   ) ;

//    dbg(ans );
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
     //   freopen("out.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(false);  cin.tie(0); cout.tie(0);
    int t=1 , c= 0 ; // si( t) ;
    //cin>>t ;
    while(t--) {
       // printf("Case %d: ",++c)  ;
        solve();

    }

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
using namespace __gnu_pbds;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define int long long
// int dx[]={-1,1,0,0}; int dy[]={0,0,1,-1};
// int dx[]={2,2,-2,-2,1,1,-1,-1}; int dy[]={1,-1,1,-1,2,-2,2,-2};
const long long mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> ii;
typedef vector< pair< int, int > > vii;
typedef map<int, int> mii;
typedef pair<int, ii> pip;
typedef pair<ii, int> ppi;
#define arrinp(arr,init,final,size,type) type* arr=new type[size];for(int i=init;i<final;i++)cin>>arr[i];
#define cr2d(arr,n,m,t) t**arr=new t*[n];for(int i=0;i<n;i++)arr[i]=new t[m];
#define w(t) int t;cin>>t; while(t--)
#define takeInp(n) int n;cin>>n;
#define fr(i,init,final) for(int i=init;i<final;i++)
#define frr(i,init,final) for(int i=init;i>=final;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>  nds;
vi init(string s)
{
	istringstream sin(s);
	int n;
	vi arr;
	while(sin>>n)arr.push_back(n);
	return arr;
}
int pow(int x, int y)
{
	if(y==0)return 1;
	int u=pow(x,y/2);
	u=u*u;
	if(y%2)u=x*u;
	return u;

}
int gcd(int a,int b)
{
	if(a<b)return gcd(b,a);
	return (b==0?a:(a%b?gcd(b,a%b):b));
}
int gcd_e(int a,int b,int &x,int &y)
{

	if(b==0){x=1; y=0; return a;}
	int x1,y1;
	int p=gcd_e(b,a%b,x1,y1);
	x=y1;
	y=x1-(a/b)*y1;
	return p;
}
int a[200001];
int n,m;
signed main()
{
	IOS
	cin>>n>>m;
	fr(i,0,n) cin>>a[i];
	priority_queue<pip>pq;
	fr(i,0,m)
	{
		int a,b;
		cin>>b>>a;
		pq.push({a,{i,b}});

	}
	int processed=-1;
	vector<pip> v;
	fr(i,0,m)
	{
		pip cur=pq.top();
		pq.pop();
		if(cur.se.fi<processed)continue;
		processed=cur.se.fi;
		v.pb(cur);
	}
	int b[n+2],c[n+2];

	fr(i,0,n+2)c[i]=b[i]=0;
	if(v[0].se.se==1)
	sort(a,a+v[0].fi);
	else sort(a,a+v[0].fi,greater<int>());
	int cur=v[0].se.se;
	//fr(i,0,n)cout<<a[i]<<" ";
	//cout<<endl;
	int xx=1;
	fr(i,1,v.size())
	{  if(v[i].se.se==cur)continue;
	cur=v[i].se.se;
		 c[0]+=xx*(v[i].fi-1);
		 b[0]+=1;
		 b[v[i].fi]--;
		 c[v[i].fi]-=xx*(v[i].fi-1);
		 xx*=-1;
	}
	fr(i,1,n+2)c[i]+=c[i-1],b[i]+=b[i-1];
	//fr(i,0,n) cout<<c[i]<<" ";

	//cout<<endl;
	fr(i,0,n)c[i]+=((b[i]&1?-1:1)*i);
	//fr(i,0,n) cout<<b[i]<<" ";
	//cout<<endl;
//fr(i,0,n) cout<<c[i]<<" ";
//cout<<endl;
	fr(i,0,n) cout<<a[c[i]]<<" ";

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
using namespace __gnu_pbds;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define int long long
// int dx[]={-1,1,0,0}; int dy[]={0,0,1,-1};
// int dx[]={2,2,-2,-2,1,1,-1,-1}; int dy[]={1,-1,1,-1,2,-2,2,-2};
const long long mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> ii;
typedef vector< pair< int, int > > vii;
typedef map<int, int> mii;
typedef pair<int, ii> pip;
typedef pair<ii, int> ppi;
#define arrinp(arr,init,final,size,type) type* arr=new type[size];for(int i=init;i<final;i++)cin>>arr[i];
#define cr2d(arr,n,m,t) t**arr=new t*[n];for(int i=0;i<n;i++)arr[i]=new t[m];
#define w(t) int t;cin>>t; while(t--)
#define takeInp(n) int n;cin>>n;
#define fr(i,init,final) for(int i=init;i<final;i++)
#define frr(i,init,final) for(int i=init;i>=final;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>  nds;
vi init(string s)
{
	istringstream sin(s);
	int n;
	vi arr;
	while(sin>>n)arr.push_back(n);
	return arr;
}
int pow(int x, int y)
{
	if(y==0)return 1;
	int u=pow(x,y/2);
	u=u*u;
	if(y%2)u=x*u;
	return u;

}
int gcd(int a,int b)
{
	if(a<b)return gcd(b,a);
	return (b==0?a:(a%b?gcd(b,a%b):b));
}
int gcd_e(int a,int b,int &x,int &y)
{

	if(b==0){x=1; y=0; return a;}
	int x1,y1;
	int p=gcd_e(b,a%b,x1,y1);
	x=y1;
	y=x1-(a/b)*y1;
	return p;
}
int a[200001];
int n,m;
signed main()
{
	IOS
	cin>>n>>m;
	fr(i,0,n) cin>>a[i];
	priority_queue<pip>pq;
	fr(i,0,m)
	{
		int a,b;
		cin>>b>>a;
		pq.push({a,{i,b}});

	}
	int processed=-1;
	vector<pip> v;
	fr(i,0,m)
	{
		pip cur=pq.top();
		pq.pop();
		if(cur.se.fi<processed)continue;
		processed=cur.se.fi;
		v.pb(cur);
	}
	int b[n+2],c[n+2];
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	if(v[0].se.se==1)
	sort(a,a+v[0].fi);
	else sort(a,a+v[0].fi,greater<int>());
	int cur=v[0].se.se;
	//fr(i,0,n)cout<<a[i]<<" ";
	//cout<<endl;
	fr(i,1,v.size())
	{  if(v[i].se.se==cur)continue;
	cur=v[i].se.se;
		 c[0]+=v[i].fi-1;
		 b[0]+=1;
		 b[v[i].fi]--;
		 c[v[i].fi]-=v[i].fi-1;
	}
	fr(i,1,n+2)c[i]+=c[i-1],b[i]+=b[i-1];
	//fr(i,0,n) cout<<c[i]<<" ";

//	cout<<endl;
	fr(i,0,n)c[i]-=(b[i]?b[i]:b[i]-1)*i;
//	fr(i,0,n) cout<<b[i]<<" ";
//	cout<<endl;
//fr(i,0,n) cout<<c[i]<<" ";
//cout<<endl;
	fr(i,0,n) cout<<a[c[i]]<<" ";

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
using namespace __gnu_pbds;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
//#define int long long
// int dx[]={-1,1,0,0}; int dy[]={0,0,1,-1};
// int dx[]={2,2,-2,-2,1,1,-1,-1}; int dy[]={1,-1,1,-1,2,-2,2,-2};
const long long mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> ii;
typedef vector< pair< int, int > > vii;
typedef map<int, int> mii;
typedef pair<int, ii> pip;
typedef pair<ii, int> ppi;
#define arrinp(arr,init,final,size,type) type* arr=new type[size];for(int i=init;i<final;i++)cin>>arr[i];
#define cr2d(arr,n,m,t) t**arr=new t*[n];for(int i=0;i<n;i++)arr[i]=new t[m];
#define w(t) int t;cin>>t; while(t--)
#define takeInp(n) int n;cin>>n;
#define fr(i,init,final) for(int i=init;i<final;i++)
#define frr(i,init,final) for(int i=init;i>=final;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>  nds;
vi init(string s)
{
	istringstream sin(s);
	int n;
	vi arr;
	while(sin>>n)arr.push_back(n);
	return arr;
}
int pow(int x, int y)
{
	if(y==0)return 1;
	int u=pow(x,y/2);
	u=u*u;
	if(y%2)u=x*u;
	return u;

}
int gcd(int a,int b)
{
	if(a<b)return gcd(b,a);
	return (b==0?a:(a%b?gcd(b,a%b):b));
}
int gcd_e(int a,int b,int &x,int &y)
{

	if(b==0){x=1; y=0; return a;}
	int x1,y1;
	int p=gcd_e(b,a%b,x1,y1);
	x=y1;
	y=x1-(a/b)*y1;
	return p;
}
int a[200001];
int n,m;
signed main()
{
	IOS
	cin>>n>>m;
	fr(i,0,n) cin>>a[i];
	priority_queue<pip>pq;
	fr(i,0,m)
	{
		int a,b;
		cin>>b>>a;
		pq.push({a,{i,b}});

	}
	int processed=-1;
	vector<pip> v;
	fr(i,0,m)
	{
		pip cur=pq.top();
		pq.pop();
		if(cur.se.fi<processed)continue;
		processed=cur.se.fi;
		v.pb(cur);
	}
	int b[n+2],c[n+2];
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	if(v[0].se.se==1)
	sort(a,a+v[0].fi);
	else sort(a,a+v[0].fi,greater<int>());
	int cur=v[0].se.se;
	//fr(i,0,n)cout<<a[i]<<" ";
	//cout<<endl;
	fr(i,1,v.size())
	{  if(v[i].se.se==cur)continue;
	cur=v[i].se.se;
		 c[0]+=v[i].fi-1;
		 b[0]+=1;
		 b[v[i].fi]--;
		 c[v[i].fi]-=v[i].fi-1;
	}
	fr(i,1,n+2)c[i]+=c[i-1],b[i]+=b[i-1];
	//fr(i,0,n) cout<<c[i]<<" ";

//	cout<<endl;
	fr(i,0,n)c[i]-=(b[i]?b[i]:b[i]-1)*i;
//	fr(i,0,n) cout<<b[i]<<" ";
//	cout<<endl;
//fr(i,0,n) cout<<c[i]<<" ";
//cout<<endl;
	fr(i,0,n) cout<<a[c[i]]<<" ";

}


#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define inf (10e15)
#define FILE                          \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);
#define FIO                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v);
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v);
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &v);
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v);
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v);

// ------------------------------------MAIN------------------------------------
//
//
//
//
int main()
{
#ifndef ONLINE_JUDGE
    FILE;
#endif

    ll N, M;
    cin >> N >> M;
    vector<ll> a(N);
    stack<pair<ll, ll>> st;
    vector<pair<ll, ll>> managers;

    for (ll i = 0; i < N; i++)
    {
        cin >> a[i];
    }

    for (ll i = 0; i < M; i++)
    {
        ll t, s;
        cin >> t >> s;

        while (!st.empty())
        {
            if (st.top().second <= s)
                st.pop();
            else
                break;
        }
        st.push(make_pair(t, s));
    }

    while (!st.empty())
    {
        managers.push_back(st.top());
        st.pop();
    }

    // cout << managers;
    ll top = managers[managers.size() - 1].second, bottom = 0;
    vector<ll> b;

    for (ll i = 0; i < top; i++)
        b.push_back(a[i]);

    sort(b.begin(), b.end());

    for (ll i = managers.size() - 1; i > 0; i--)
    {
        for (ll j = managers[i].second - 1; j >= managers[i - 1].second; j--)
        {
            a[j] = managers[i].first == 1 ? b[--top] : b[bottom++];
        }
    }

    for (ll i = managers[0].second - 1; i >= 0; i--)
    {
        a[i] = managers[0].first == 1 ? b[--top] : b[bottom++];
    }
    cout << a;
}

//
//
//
//
// ------------------------------------PRINTING------------------------------------
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
    os << "[";
    for (int i = 0; i < v.size(); ++i)
    {
        os << v[i];
        if (i != v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &v)
{
    os << "[";
    for (auto it : v)
    {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v)
{
    for (auto it : v)
        os << it.first << " : "
           << it.second << "\n";

    return os;
}

template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &v)
{
    os << "[";
    for (auto it : v)
    {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "]\n";
    return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    return os;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define inf (10e15)
#define FILE                          \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);
#define FIO                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v);
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v);
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &v);
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v);
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v);

// ------------------------------------MAIN------------------------------------
//
//
//
//
int main()
{
#ifndef ONLINE_JUDGE
    FILE;
#endif

    ll N, M;
    cin >> N >> M;
    vector<ll> a(N);
    stack<pair<ll, ll>> st;
    vector<pair<ll, ll>> managers;

    for (ll i = 0; i < N; i++)
    {
        cin >> a[i];
    }

    for (ll i = 0; i < M; i++)
    {
        ll t, s;
        cin >> t >> s;

        while (!st.empty())
        {
            if (st.top().second <= s)
                st.pop();
            else
                break;
        }
        st.push(make_pair(t, s));
    }

    while (!st.empty())
    {
        managers.push_back(st.top());
        st.pop();
    }

    // cout << managers;
    ll top = managers[managers.size() - 1].second, bottom = 0;
    vector<ll> b;

    for (ll i = 0; i < top; i++)
        b.push_back(a[i]);

    sort(b.begin(), b.end());

    for (ll i = managers.size() - 1; i > 0; i--)
    {
        for (ll j = managers[i].second - 1; j >= managers[i - 1].second; j--)
        {
            a[j] = managers[i].first == 1 ? b[--top] : b[bottom++];
        }
    }

    for (ll i = managers[0].second - 1; i >= 0; i--)
    {
        a[i] = managers[0].first == 1 ? b[--top] : b[bottom++];
    }

    for (ll i = 0; i < a.size(); i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
}

//
//
//
//
// ------------------------------------PRINTING------------------------------------
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
    os << "[";
    for (int i = 0; i < v.size(); ++i)
    {
        os << v[i];
        if (i != v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &v)
{
    os << "[";
    for (auto it : v)
    {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v)
{
    for (auto it : v)
        os << it.first << " : "
           << it.second << "\n";

    return os;
}

template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &v)
{
    os << "[";
    for (auto it : v)
    {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "]\n";
    return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    return os;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,int>P;
const int len=2e5+5;
const ll mod=998244353;
const ull seed=131;
int a[len];
int b[len];
int c[len];
int d[len];
int n,m;
int get_max(int s)
{
	int maxx=0,id=-1;
	for(int i=s;i<=m;++i)
	{
		if(c[i]>=maxx)
		{
			maxx=c[i];
			id=i;
		}
	}
	return id;
}
int main()
{
	cin>>n>>m;
	int maxx=0,f,g;
	for(int i=1;i<=n;++i)scanf("%d",&a[i]);
	for(int i=1;i<=m;++i)
	{
		scanf("%d%d",&b[i],&c[i]);
		if(maxx<=c[i])
		{
			maxx=c[i];
			f=b[i];
			g=i;
		}
	}
	for(int i=1;i<=n;++i)d[i]=a[i];
	if(f==1)sort(a+1,a+maxx+1);
	else sort(a+1,a+maxx+1,greater<int>());
	if(f==1)for(int i=1;i<=maxx;++i)d[i]=a[i];
	else for(int i=1;i<=maxx;++i)d[i]=a[maxx+1-i];
	int s=1,e=maxx;
	while(1)
	{
		int id=get_max(g+1);
		for(int i=c[g];i>c[id];--i)
		{
			if(b[g]==2)a[i]=d[s++];
			else a[i]=d[e--];
		}
		g=id;
		if(g==m)break;
	}
	for(int i=1;i<=c[g];++i)
		if(b[g]==1)a[i]=d[s++];
		else a[i]=d[e--];
	for(int i=1;i<=n;++i)printf("%d ",a[i]);
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,int>P;
const int len=2e5+5;
const ll mod=998244353;
const ull seed=131;
int a[len];
int b[len];
int c[len];
int d[len];
int n,m;
int get_max(int s)
{
	int maxx=0,id=-1;
	for(int i=s;i<=m;++i)
	{
		if(c[i]>=maxx)
		{
			maxx=c[i];
			id=i;
		}
	}
	return id;
}
int main()
{
	cin>>n>>m;
	int maxx=0,f,g;
	for(int i=1;i<=n;++i)scanf("%d",&a[i]);
	for(int i=1;i<=m;++i)
	{
		scanf("%d%d",&b[i],&c[i]);
		if(maxx<=c[i])
		{
			maxx=c[i];
			f=b[i];
			g=i;
		}
	}
	for(int i=1;i<=n;++i)d[i]=a[i];
	if(f==1)sort(a+1,a+maxx+1);
	else sort(a+1,a+maxx+1,greater<int>());
	if(f==1)for(int i=1;i<=maxx;++i)d[i]=a[i];
	else for(int i=1;i<=maxx;++i)d[i]=a[maxx+1-i];
	sort(d+1,d+maxx+1);
	int s=1,e=maxx;
	while(1)
	{
		int id=get_max(g+1);
		if(id==-1)break;
		for(int i=c[g];i>c[id];--i)
		{
			if(b[g]==2)a[i]=d[s++];
			else a[i]=d[e--];
		}
		g=id;
	}
	for(int i=1;i<=c[g];++i)
		if(b[g]==1)a[i]=d[s++];
		else a[i]=d[e--];
	for(int i=1;i<=n;++i)printf("%d ",a[i]);
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,int>P;
const int len=2e5+5;
const ll mod=998244353;
const ull seed=131;
int a[len],t[len],r[len];
int b[len];
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;++i)cin>>a[i];
	for(int i=1;i<=m;++i)cin>>t[i]>>r[i];
	stack<int>st;
	for(int i=1;i<=m;++i)
	{
		if(st.size()==0||r[st.top()]>r[i])st.push(i);
		else
		{
			while(st.size()&&r[st.top()]<=r[i])st.pop();
			st.push(i);
		}
	}
	vector<int>ve;
	while(st.size())ve.push_back(st.top()),st.pop();
	int maxx=r[ve.back()];
	for(int i=1;i<=n;++i)b[i]=a[i];
	sort(b+1,b+1+maxx);
	if(t[ve.back()]==1)sort(a+1,a+1+maxx);
	else sort(a+1,a+1+maxx,greater<int>());
	int s=1,e=maxx;
	for(int i=ve.size()-1;i>=1;--i)
	{
		for(int j=r[ve[i]];j>r[ve[i-1]];--j)
			if(t[ve[i]]==1)a[j]=b[e--];
			else a[j]=b[s++];
	}
	for(int j=r[ve[0]];j>=1;--j)
		if(t[ve[0]]==1)a[j]=b[e--];
		else a[j]=b[s++];
	for(int i=1;i<=n;++i)cout<<a[i]<<' ';
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,int>P;
const int len=2e5+5;
const ll mod=998244353;
const ull seed=131;
int a[len],t[len],r[len];
int b[len];
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;++i)cin>>a[i];
	for(int i=1;i<=m;++i)cin>>t[i]>>r[i];
	stack<int>st;
	for(int i=1;i<=n;++i)
	{
		if(st.size()==0||r[st.top()]>r[i])st.push(i);
		else
		{
			while(st.size()&&r[st.top()]<=r[i])st.pop();
			st.push(i);
		}
	}
	vector<int>ve;
	while(st.size())ve.push_back(st.top()),st.pop();
	int maxx=r[ve.back()];
	for(int i=1;i<=n;++i)b[i]=a[i];
	sort(b+1,b+1+maxx);
	if(t[ve.back()]==1)sort(a+1,a+1+maxx);
	else sort(a+1,a+1+maxx,greater<int>());
	int s=1,e=maxx;
	for(int i=ve.size()-1;i>=1;--i)
	{
		for(int j=r[ve[i]];j>r[ve[i-1]];--j)
			if(t[ve[i]]==1)a[j]=b[e--];
			else a[j]=b[s++];
	}
	for(int j=r[ve[0]];j>=1;--j)
		if(t[ve[0]]==1)a[j]=b[e--];
		else a[j]=b[s++];
	for(int i=1;i<=n;++i)cout<<a[i]<<' ';
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

int a[211111];
int t[211111];
int r[211111];

pair<int, int> v[211111];

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    int imax = 0;
    for (int i = 0; i < m; i++) {
        cin >> t[i] >> r[i];
        if (r[i] >= r[imax]) {
            imax = i;
        }
    }
    for (int i = imax + 1; i < m; i++) {
        v[r[i] - 1] = {i, t[i]};
    }
    map<int, int> s;
    for (int i = 0; i < r[imax]; i++)
        s[a[i]]++;
    pair<int, int> cur = {-1, -1};
    for (int i = r[imax] - 1; i >= 0; i--) {
        if (v[i].first > cur.first) {
            cur = v[i];
        }
        if (cur.second == 1) {
            auto item = *s.rbegin();
            a[i] = item.first;
            if (item.second == 1)
                s.erase(item.first);
            else
                s[item.first]--;
        } else {
            auto item = *s.begin();
            a[i] = item.first;
            if (item.second == 1)
                s.erase(item.first);
            else
                s[item.first]--;
        }
    }
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

int a[211111];
int t[211111];
int r[211111];

pair<int, int> v[211111];

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    int imax = 0;
    for (int i = 0; i < m; i++) {
        cin >> t[i] >> r[i];
        if (r[i] >= r[imax]) {
            imax = i;
        }
    }
    for (int i = imax; i < m; i++) {
        v[r[i] - 1] = {i, t[i]};
    }
    map<int, int> s;
    for (int i = 0; i < r[imax]; i++)
        s[a[i]]++;
    pair<int, int> cur = {-1, -1};
    for (int i = r[imax] - 1; i >= 0; i--) {
        if (v[i].first > cur.first) {
            cur = v[i];
        }
        if (cur.second == 1) {
            auto item = *s.rbegin();
            a[i] = item.first;
            if (item.second == 1)
                s.erase(item.first);
            else
                s[item.first]--;
        } else {
            auto item = *s.begin();
            a[i] = item.first;
            if (item.second == 1)
                s.erase(item.first);
            else
                s[item.first]--;
        }
    }
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

#define MAXI 1000000

int main()
{

	fio;
    ll n,i,j,m,x,t,r;
    cin>>n>>m;
    ll arr[n];
    for(i=0;i<n;i++)
    {
    	cin>>arr[i];
	}
	stack<pii>s2,s;
	ll maxi=0;
	for(i=0;i<m;i++)
	{
		cin>>t>>r;
		r--;
		while(!s2.empty()&&s2.top().se<=r)
		{
			s2.pop();
		}
		s2.push(mp(t,r));
		maxi=max(maxi,r);

	}
	ll index=maxi;
	maxpq pq1;
	minpq pq2;
	for(i=0;i<=index;i++)
	{
		pq1.push(arr[i]);
		pq2.push(arr[i]);
	}
	while(!s2.empty())
	{
	    s.push(s2.top());
	    s2.pop();
	}
	bool visited[n+1]={false};
	while(!s.empty())
	{
		auto itr=s.top();
		t=itr.fi;
		r=itr.se;
		s.pop();
	 // cout<<t<<" "<<r<<endl;
		if(t==1)
		{
			ll count=r+1;
			if(!s.empty())
			count=count-s.top().se-1;
			while(count!=0)
			{
				arr[r]=pq1.top();
				pq1.pop();
				count--;
				r--;
			}
		}
		else
		{
			ll count=r+1;
			if(!s.empty())
			count=count-s.top().se-1;
			while(count!=0)
			{
			 //   cout<<r<<" "<<pq2.top()<<endl;
				arr[r]=pq2.top();
				pq2.pop();
				count--;
				r--;
			}
		}

	}
	for(i=0;i<n;i++)
	{
		cout<<arr[i]<<" ";
	}

}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
	ll n,m,i,j,max=0;
	cin>>n>>m;
	ll arr[n];
	for(i=0;i<n;i++)
	cin>>arr[i];
	vector<pair<ll,ll> >vec;
	stack<ll>s;

	for(i=0;i<m;i++)
	{
		ll t,r;
		cin>>t>>r;
		vec.push_back(make_pair(t,r));
		if(r>max)
		max=r;

	}
	for(i=0;i<m;i++)
	{
		while(!s.empty()&&vec[i].second>=vec[s.top()].second)
		{
			s.pop();
		}
		s.push(i);
	}
	vector<pair<ll,ll> >vec2;
	while(!s.empty())
	{
		vec2.push_back(make_pair(vec[s.top()].first,vec[s.top()].second));
		s.pop();
	}
	reverse(vec2.begin(),vec2.end());
	priority_queue<ll>pq;
	priority_queue<ll,vector<ll>,greater<ll> >pq2;
	for(i=0;i<max;i++)
	{
		pq.push(arr[i]);
		pq2.push(arr[i]);
	}
//	bool visited[n];
//	memset(visited,false,sizeof(visited));
	for(i=0;i<vec2.size()-1;i++)
	{
		ll t=vec2[i].first;
		ll y=vec2[i].second-vec2[i+1].second;
		ll index=vec2[i].second-1;
		if(t==1)
		{
			while(y!=0)
			{
			    arr[index]=pq.top();
			    pq.pop();
			    y--;
			    index--;
			}
		}
		else
		{
			while(y!=0)
			{
				arr[index]=pq2.top();
				pq2.pop();
				y--;
				index--;
			}
		}
	}
		ll t=vec2[i].first;
			ll y=vec2[i].second;
				ll index=vec2[i].second-1;
					if(t==1)
		{
			while(y!=0)
			{
			    arr[index]=pq.top();
			    pq.pop();
			    y--;
			    index--;
			}
		}
		else
		{
			while(y!=0)
			{
				arr[index]=pq2.top();
				pq2.pop();
				y--;
				index--;
			}
		}
		for(i=0;i<n;i++)
		cout<<arr[i]<<" ";

}


#include<bits/stdc++.h>
using namespace std;

#define double long double
//#define int long long
#define vec vector
#define pb push_back
#define unset unordered_set
#define ii pair<int, int>
#define X first
#define Y second
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORE(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, a, b) for(int i = (a); i >= (b); i--)
#define bitcount(n) __builtin_popcountll(n)
template <typename T,typename U>
std::pair<T,U> operator+(const std::pair<T,U> & l,const std::pair<T,U> & r) {
  return {l.first+r.first,l.second+r.second};
}
typedef void (*callback_function)(void); // type for conciseness
const long long ZERO = 0LL;
const long long INF64 = 1e18;
const int INF32 = 1e9;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0L);
const double EPS = static_cast<double>(1e-9);
typedef long long ll;

inline int Pow(int a, int k) {
  int s = 1;
  for(; k; k >>= 1) {
    k & 1 ? s = 1LL * s * a % MOD : 0;
    a = 1LL * a * a % MOD;
  }
  return s;
}

// Init variables here
const int N = 1e6 + 7;
int n, m;
int a[N];
int t[N];
int r[N];
int b[N];
int cnt;

void input() {
  //freopen("??.in", "r", stdin);
  //freopen("??.out", "w", stdout);
  cin >> n >> m;
  FOR(i, 0, n) {cin >> a[i]; b[i] = a[i];}
  FOR(i, 0, m) {
    int tt, rr;
    cin >> tt >> rr;
    while (cnt > 0 and rr > r[cnt - 1]) {
      cnt--;
    }
    t[cnt] = tt;
    r[cnt] = rr;
    cnt++;
  }
}

void output() {
  FOR(i, 0, n) {cout << a[i] << ' ';}
  cout << endl;
}

void preprocess() {

}

void debug() {
  if (true) {

  }
}

void solve() {
  preprocess();
  int L = 0, R = r[0];
  r[cnt] = 0;
  sort(b, b + R);
  FORE(i, 1, cnt) {
    FORD(j, r[i - 1] - 1, r[i]) {
      a[j] = (t[i - 1] == 1 ? b[--R] : b[L++]);
    }
  }
  //debug();
  // read the question correctly (is y a vowel?)
  // look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)
}

#undef int
int main() {
#define int long long
	ios_base::sync_with_stdio(false);
	cin.tie();
  //auto beginProgram = chrono::steady_clock::now();

  //int t; cin >> t;
  //while (t--)
  {
    input();
    solve();
    output();
  }

  //auto endProgram = chrono::steady_clock::now();

  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define double long double
//#define int long long
#define vec vector
#define pb push_back
#define unset unordered_set
#define ii pair<int, int>
#define X first
#define Y second
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORE(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, a, b) for(int i = (a); i >= (b); i--)
#define bitcount(n) __builtin_popcountll(n)
template <typename T,typename U>
std::pair<T,U> operator+(const std::pair<T,U> & l,const std::pair<T,U> & r) {
  return {l.first+r.first,l.second+r.second};
}
typedef void (*callback_function)(void); // type for conciseness
const long long ZERO = 0LL;
const long long INF64 = 1e18;
const int INF32 = 1e9;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0L);
const double EPS = static_cast<double>(1e-9);
typedef long long ll;

inline int Pow(int a, int k) {
  int s = 1;
  for(; k; k >>= 1) {
    k & 1 ? s = 1LL * s * a % MOD : 0;
    a = 1LL * a * a % MOD;
  }
  return s;
}

// Init variables here
const int N = 1e6 + 7;
int n, m;
int a[N];
int t[N];
int r[N];
int b[N];
int cnt;

void input() {
  //freopen("??.in", "r", stdin);
  //freopen("??.out", "w", stdout);
  cin >> n >> m;
  FOR(i, 0, n) {cin >> a[i]; b[i] = a[i];}
  FOR(i, 0, m) {
    int tt, rr;
    cin >> tt >> rr;
    while (cnt > 0 and rr > r[cnt - 1]) {
      cnt--;
    }
    t[cnt] = tt;
    r[cnt] = rr;
    cnt++;
  }
}

void output() {
  FOR(i, 0, n) {cout << a[i] << ' ';}
  cout << endl;
}

void preprocess() {

}

void debug() {
  if (true) {

  }
}

void solve() {
  preprocess();
  int L = 0, R = r[0];
  // CAUTION: Must set r[cnt] = 0 because r[cnt] may have had a different value set (check the
  // while loop inside the last FOR loop in function input()!)
  r[cnt] = 0;
  sort(b, b + R);
  FORE(i, 1, cnt) {
    FORD(j, r[i - 1] - 1, r[i]) {
      a[j] = (t[i - 1] == 1 ? b[--R] : b[L++]);
    }
  }
  //debug();
  // read the question correctly (is y a vowel?)
  // look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)
}

#undef int
int main() {
#define int long long
	ios_base::sync_with_stdio(false);
	cin.tie();
  //auto beginProgram = chrono::steady_clock::now();

  //int t; cin >> t;
  //while (t--)
  {
    input();
    solve();
    output();
  }

  //auto endProgram = chrono::steady_clock::now();

  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define double long double
//#define int long long
#define vec vector
#define pb push_back
#define unset unordered_set
#define ii pair<int, int>
#define X first
#define Y second
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORE(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, a, b) for(int i = (a); i >= (b); i--)
#define bitcount(n) __builtin_popcountll(n)
template <typename T,typename U>
std::pair<T,U> operator+(const std::pair<T,U> & l,const std::pair<T,U> & r) {
  return {l.first+r.first,l.second+r.second};
}
typedef void (*callback_function)(void); // type for conciseness
const long long ZERO = 0LL;
const long long INF64 = 1e18;
const int INF32 = 1e9;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0L);
const double EPS = static_cast<double>(1e-9);
typedef long long ll;

inline int Pow(int a, int k) {
  int s = 1;
  for(; k; k >>= 1) {
    k & 1 ? s = 1LL * s * a % MOD : 0;
    a = 1LL * a * a % MOD;
  }
  return s;
}

// Init variables here
const int N = 1e6 + 7;
int n, m;
int a[N];
int t[N];
int r[N];
int b[N];
int cnt;

void input() {
  //freopen("??.in", "r", stdin);
  //freopen("??.out", "w", stdout);
  cin >> n >> m;
  FOR(i, 0, n) {cin >> a[i]; b[i] = a[i];}
  FOR(i, 0, m) {
    int tt, rr;
    cin >> tt >> rr;
    while (cnt > 0 and rr > r[cnt - 1]) {
      cnt--;
    }
    t[cnt] = tt;
    r[cnt] = rr;
    cnt++;
  }
}

void output() {
  FOR(i, 0, n) {cout << a[i] << ' ';}
  cout << endl;
}

void preprocess() {

}

void debug() {
  if (true) {

  }
}

void solve() {
  preprocess();
  int L = 0, R = r[0];
  sort(b, b + R);
  FORE(i, 1, cnt) {
    FORD(j, r[i - 1] - 1, r[i]) {
      a[j] = (t[i - 1] == 1 ? b[--R] : b[L++]);
    }
  }
  //debug();
  // read the question correctly (is y a vowel?)
  // look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)
}

#undef int
int main() {
#define int long long
	ios_base::sync_with_stdio(false);
	cin.tie();
  //auto beginProgram = chrono::steady_clock::now();

  //int t; cin >> t;
  //while (t--)
  {
    input();
    solve();
    output();
  }

  //auto endProgram = chrono::steady_clock::now();

  return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5;

pair<int, int> mx[2], last[2];
int n, m;
int a[N];

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i=1; i<=n; i++) cin >> a[i];
    for (int i=0; i<2; i++)
    {
        mx[i] = {0, 0};
        last[i] = {0, 0};
    }
    for (int i=1; i<=m; i++)
    {
        int t, x; cin >> t >> x;
        --t;
        if (x>mx[t].second)
        {
            mx[t].first = i;
            mx[t].second = x;
        }
        last[t] = {i, x};
    }
    if (mx[0].first<mx[1].first)
    {
        sort(a+1,a+mx[0].second+1);
        sort(a+1,a+mx[1].second+1,greater<int>());
    }
    else
    {
        sort(a+1,a+mx[1].second+1,greater<int>());
        sort(a+1,a+mx[0].second+1);
    }
    if (last[0].first<last[1].first)
    {
        sort(a+1,a+last[0].second+1);
        sort(a+1,a+last[1].second+1,greater<int>());
    }
    else
    {
        sort(a+1,a+last[1].second+1,greater<int>());
        sort(a+1,a+last[0].second+1);
    }
    for (int i=1; i<=n; i++) cout << a[i] << " ";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5;

pair<int, int> mx[2], last[2];
int n, m;
int a[N];

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i=1; i<=n; i++) cin >> a[i];
    for (int i=0; i<2; i++)
    {
        mx[i] = {0, 0};
        last[i] = {0, 0};
    }
    for (int i=1; i<=m; i++)
    {
        int t, x; cin >> t >> x;
        --t;
        if (x>=mx[t].second)
        {
            mx[t].first = i;
            mx[t].second = x;
        }
        last[t] = {i, x};
    }
    pair<pair<int, int>, int> p[4];
    p[0] = {{mx[0].first, mx[0].second}, 0};
    p[1] = {{mx[1].first, mx[1].second}, 1};
    p[2] = {{last[0].first, last[0].second}, 0};
    p[3] = {{last[1].first, last[1].second}, 1};
    sort(p,p+4);
    for (int i=0; i<4; i++)
    {
        int op = p[i].second;
        int r = p[i].first.second;
        if (op==0) sort(a+1,a+r+1);
        else sort(a+1,a+r+1,greater<int>());
    }
    for (int i=1; i<=n; i++) cout << a[i] << " ";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5;

pair<int, int> mx[2], last[2];
int n, m;
int a[N];

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i=1; i<=n; i++) cin >> a[i];
    for (int i=0; i<2; i++)
    {
        mx[i] = {0, 0};
        last[i] = {0, 0};
    }
    for (int i=1; i<=m; i++)
    {
        int t, x; cin >> t >> x;
        --t;
        if (x>=mx[t].second)
        {
            mx[t].first = i;
            mx[t].second = x;
        }
        last[t] = {i, x};
    }
    if (mx[0].first<mx[1].first)
    {
        sort(a+1,a+mx[0].second+1);
        sort(a+1,a+mx[1].second+1,greater<int>());
    }
    else
    {
        sort(a+1,a+mx[1].second+1,greater<int>());
        sort(a+1,a+mx[0].second+1);
    }
    if (last[0].first<last[1].first)
    {
        sort(a+1,a+last[0].second+1);
        sort(a+1,a+last[1].second+1,greater<int>());
    }
    else
    {
        sort(a+1,a+last[1].second+1,greater<int>());
        sort(a+1,a+last[0].second+1);
    }
    for (int i=1; i<=n; i++) cout << a[i] << " ";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5;
int n, m;
int a[N], b[N];
int r[N], t[N];

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i=0; i<n; i++) cin >> a[i];
    int s = 0;
    for (int i=0; i<m; i++)
    {
        int ti, ri; cin >> ti >> ri;
        while (s && ri>=r[s-1]) --s;
        t[s] = ti, r[s] = ri, ++s;
    }
    int bl = 0, br = r[0];
    r[s] = 0, ++s;
    for (int i=0; i<br; i++) b[i] = a[i];
    sort(b,b+br);
    for (int i=1; i<s; i++)
    {
        for (int j=r[i-1]; j>r[i]; j--)
        {
            a[j-1] = (t[i-1]==1) ? b[--br] : b[bl++];
        }
    }
    for (int i=0; i<n; i++) cout << a[i] << " ";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[200005];
int temp[200005];
struct P
{
	int p;int c;int shun;
}b[200005];
int zhen[200005];
int zhen2[200005];
int ans[200005];
int comp(P x,P y)
{
	if (x.c!=y.c)
	return x.c>y.c;
	else
	return x.shun>y.shun;
}
int comp2(int x,int y)
{
	if (zhen[0]==1)
	return x<y;
	else
	return x>y;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for (int i=0;i<m;i++)
	{
		scanf("%d%d",&b[i].p,&b[i].c);
		b[i].shun=i;
	}
	sort(b,b+m,comp);
	int wei=-1;
	int j=0;
	for (int i=0;i<m;i++)//拿出数组中的最大值，去掉最大值及其顺序前的数，再找后面的最大值，以此类推
	{
		if (b[i].shun>wei)
		{
			zhen[j]=b[i].p;
			zhen2[j++]=b[i].c;
			wei=b[i].shun;
		}
	}
	//基础排序
	sort(a,a+zhen2[0],comp2);
	for (int i=0;i<n;i++)
	ans[i]=a[i];
	zhen2[j]=0;
	//for (int i=1;i<j;i++)
	//{
	//	if (zhen[i]+zhen[0]==3)//反序
	//	{
	//		int startnum=zhen2[i+1];
	//		int endnum=zhen2[i];
	//		int ji=endnum-1;
	//		for (int k=startnum;k<endnum;k++)
	//		{
	//			ans[ji--]=a[k];
	//		}
	//	}
	//}
	int lp=0;int rp=zhen2[0]-1;
	for (int i=1;i<j;i++)
	{
		for (int k=zhen2[i+1]-1;k>=zhen2[i];k--)
		{
			if (zhen[i]==1)ans[k]=a[rp--];
			else ans[k]=a[lp++];
		}
	}
	for (int i=0;i<n;i++)
	printf("%d ",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
int a[200005];
int temp[200005];
struct P
{
	int p;int c;int shun;
}b[200005];
int zhen[200005];
int zhen2[200005];
int comp(P x,P y)
{
	if (x.c!=y.c)
	return x.c>y.c;
	else
	return x.shun>y.shun;
}
int comp2(int x,int y)
{
	if (zhen[0]==1)
	return x<y;
	else
	return x>y;
}
int diao(int shu)
{
	int j=0;
	for (int i=shu-1;i>=0;i--)
	{
		temp[j++]=a[i];
	}
	for (int i=0;i<shu;i++)
	{
		a[i]=temp[i];
	}
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for (int i=0;i<m;i++)
	{
		scanf("%d%d",&b[i].p,&b[i].c);
		b[i].shun=i;
	}
	sort(b,b+m,comp);
	int wei=-1;
	int j=0;
	for (int i=0;i<m;i++)//拿出数组中的最大值，去掉最大值及其顺序前的数，再找后面的最大值，以此类推
	{
		if (b[i].shun>wei)
		{
			zhen[j]=b[i].p;
			zhen2[j++]=b[i].c;
			wei=b[i].shun;
		}
	}
	//基础排序
	sort(a,a+zhen2[0],comp2);
	for (int i=1;i<j;i++)
	{
		if (zhen[i]!=zhen[i-1])
		{
			diao(zhen2[i]);
		}
	}
	for (int i=0;i<n;i++)
	printf("%d ",a[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[200005];
int temp[200005];
struct P
{
	int p;int c;int shun;
}b[200005];
int zhen[200005];
int zhen2[200005];
int ans[200005];
int comp(P x,P y)
{
	if (x.c!=y.c)
	return x.c>y.c;
	else
	return x.shun>y.shun;
}
int comp2(int x,int y)
{
	if (zhen[0]==1)
	return x<=y;
	else
	return x>=y;
}
int diao(int shu)
{
	int j=0;
	for (int i=shu-1;i>=0;i--)
	{
		temp[j++]=a[i];
	}
	for (int i=0;i<shu;i++)
	{
		a[i]=temp[i];
	}
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for (int i=0;i<m;i++)
	{
		scanf("%d%d",&b[i].p,&b[i].c);
		b[i].shun=i;
	}
	sort(b,b+m,comp);
	int wei=-1;
	int j=0;
	for (int i=0;i<m;i++)//拿出数组中的最大值，去掉最大值及其顺序前的数，再找后面的最大值，以此类推
	{
		if (b[i].shun>wei)
		{
			zhen[j]=b[i].p;
			zhen2[j++]=b[i].c;
			wei=b[i].shun;
		}
	}
	//基础排序
	sort(a,a+zhen2[0],comp2);
	for (int i=0;i<n;i++)
	ans[i]=a[i];
	zhen2[j]=0;
	for (int i=1;i<j;i++)
	{
		if (zhen[i]+zhen[0]==3)//反序
		{
			int startnum=zhen2[i+1];
			int endnum=zhen2[i];
			int ji=endnum-1;
			for (int k=startnum;k<endnum;k++)
			{
				ans[ji--]=a[k];
			}
		}
	}
	for (int i=0;i<n;i++)
	printf("%d ",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
int a[200005];
int temp[200005];
struct P
{
	int p;int c;int shun;
}b[200005];
int zhen[200005];
int zhen2[200005];
int comp(P x,P y)
{
	if (x.c!=y.c)
	return x.c>y.c;
	else
	return x.shun>y.shun;
}
int comp2(int x,int y)
{
	if (zhen[0]==1)
	return x<y;
	else
	return x>y;
}
int diao(int shu)
{
	int j=0;
	for (int i=shu-1;i>=0;i--)
	{
		temp[j++]=a[i];
	}
	for (int i=0;i<shu;i++)
	{
		a[i]=temp[i];
	}
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for (int i=0;i<m;i++)
	{
		scanf("%d%d",&b[i].p,&b[i].c);
		b[i].shun=i;
	}
	sort(b,b+m,comp);
	int wei=-1;
	int j=0;
	for (int i=0;i<m;i++)//拿出数组中的最大值，去掉最大值及其顺序前的数，再找后面的最大值，以此类推
	{
		if (b[i].shun>wei)
		{
			zhen[j]=b[i].p;
			zhen2[j++]=b[i].c;
			wei=b[i].shun;
		}
	}
	//基础排序
	sort(a,a+zhen2[0],comp2);
	for (int i=1;i<j;i++)
	{
		if (zhen[i]!=zhen[i-1])
		{
			diao(zhen2[i]);
		}
	}
	for (int i=0;i<n;i++)
	printf("%d ",a[i]);
}


#include <bits/stdc++.h>
using namespace std;
int a[200005];
int temp[200005];
struct P
{
	int p;int c;int shun;
}b[200005];
int zhen[200005];
int zhen2[200005];
int ans[200005];
int comp(P x,P y)
{
	if (x.c!=y.c)
	return x.c>y.c;
	else
	return x.shun>y.shun;
}
int comp2(int x,int y)
{
	if (zhen[0]==1)
	return x<y;
	else
	return x>y;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for (int i=0;i<m;i++)
	{
		scanf("%d%d",&b[i].p,&b[i].c);
		b[i].shun=i;
	}
	sort(b,b+m,comp);
	int wei=-1;
	int j=0;
	for (int i=0;i<m;i++)//拿出数组中的最大值，去掉最大值及其顺序前的数，再找后面的最大值，以此类推
	{
		if (b[i].shun>wei)
		{
			zhen[j]=b[i].p;
			zhen2[j++]=b[i].c;
			wei=b[i].shun;
		}
	}
	//基础排序
	sort(a,a+zhen2[0],comp2);
	for (int i=0;i<n;i++)
	ans[i]=a[i];
	zhen2[j]=0;
	for (int i=1;i<j;i++)
	{
		if (zhen[i]+zhen[0]==3)//反序
		{
			int startnum=zhen2[i+1];
			int endnum=zhen2[i];
			int ji=endnum-1;
			for (int k=startnum;k<endnum;k++)
			{
				ans[ji--]=a[k];
			}
		}
	}
	zhen[0]=zhen[j-1];
	sort(a,a+zhen2[j-1],comp2);
	for (int i=0;i<zhen2[j-1];i++)
	{
		ans[i]=a[i];
	}
	for (int i=0;i<n;i++)
	printf("%d ",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
int a[200005];
int temp[200005];
struct P
{
	int p;int c;int shun;
}b[200005];
int zhen[200005];
int zhen2[200005];
int ans[200005];
int comp(P x,P y)
{
	if (x.c!=y.c)
	return x.c>y.c;
	else
	return x.shun>y.shun;
}
int comp2(int x,int y)
{
	if (zhen[0]==1)
	return x<y;
	else
	return x>y;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for (int i=0;i<m;i++)
	{
		scanf("%d%d",&b[i].p,&b[i].c);
		b[i].shun=i;
	}
	sort(b,b+m,comp);
	int wei=-1;
	int j=0;
	for (int i=0;i<m;i++)//拿出数组中的最大值，去掉最大值及其顺序前的数，再找后面的最大值，以此类推
	{
		if (b[i].shun>wei)
		{
			zhen[j]=b[i].p;
			zhen2[j++]=b[i].c;
			wei=b[i].shun;
		}
	}
	//基础排序
	sort(a,a+zhen2[0],comp2);
	for (int i=0;i<n;i++)
	ans[i]=a[i];
	zhen2[j]=0;
	//for (int i=1;i<j;i++)
	//{
	//	if (zhen[i]+zhen[0]==3)//反序
	//	{
	//		int startnum=zhen2[i+1];
	//		int endnum=zhen2[i];
	//		int ji=endnum-1;
	//		for (int k=startnum;k<endnum;k++)
	//		{
	//			ans[ji--]=a[k];
	//		}
	//	}
	//}
	int lp=0;int rp=zhen2[0]-1;
	/*
10 3
6 4 0 2 -3 7 8 -9 1 5
-9 -3 0 2 4 6 7 8 1 5
1 8
1 4
2 2
-3 -9 0 2 4 6 7 8 1 5
	*/
	for (int i=0;i<j;i++)
	{
		for (int k=zhen2[i]-1;k>=zhen2[i+1];k--)
		{
			if (zhen[i]+zhen[0]!=3)
			{
				ans[k]=a[rp--];

			}
			else
			{
				ans[k]=a[lp++];
			}
		}
	}
	for (int i=0;i<n;i++)
	printf("%d ",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
int a[200005];
int temp[200005];
struct P
{
	int p;int c;int shun;
}b[200005];
int zhen[200005];
int zhen2[200005];
int ans[200005];
int comp(P x,P y)
{
	if (x.c!=y.c)
	return x.c>y.c;
	else
	return x.shun>y.shun;
}
int comp2(int x,int y)
{
	if (zhen[0]==1)
	return x<y;
	else
	return x>y;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for (int i=0;i<m;i++)
	{
		scanf("%d%d",&b[i].p,&b[i].c);
		b[i].shun=i;
	}
	sort(b,b+m,comp);
	int wei=-1;
	int j=0;
	for (int i=0;i<m;i++)//拿出数组中的最大值，去掉最大值及其顺序前的数，再找后面的最大值，以此类推
	{
		if (b[i].shun>wei)
		{
			zhen[j]=b[i].p;
			zhen2[j++]=b[i].c;
			wei=b[i].shun;
		}
	}
	//基础排序
	sort(a,a+zhen2[0],comp2);
	for (int i=0;i<n;i++)
	ans[i]=a[i];
	zhen2[j]=0;
	for (int i=1;i<j;i++)
	{
		if (zhen[i]+zhen[0]==3)//反序
		{
			int startnum=zhen2[i+1];
			int endnum=zhen2[i];
			int ji=endnum-1;
			for (int k=startnum;k<endnum;k++)
			{
				ans[ji--]=a[k];
			}
		}
	}
	for (int i=0;i<n;i++)
	printf("%d ",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
int a[200005];
int temp[200005];
struct P
{
	int p;int c;int shun;
}b[200005];
int zhen[200005];
int zhen2[200005];
int ans[200005];
int comp(P x,P y)
{
	if (x.c!=y.c)
	return x.c>y.c;
	else
	return x.shun>y.shun;
}
int comp2(int x,int y)
{
	if (zhen[0]==1)
	return x<y;
	else
	return x>y;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for (int i=0;i<m;i++)
	{
		scanf("%d%d",&b[i].p,&b[i].c);
		b[i].shun=i;
	}
	sort(b,b+m,comp);
	int wei=-1;
	int j=0;
	for (int i=0;i<m;i++)//拿出数组中的最大值，去掉最大值及其顺序前的数，再找后面的最大值，以此类推
	{
		if (b[i].shun>wei)
		{
			zhen[j]=b[i].p;
			zhen2[j++]=b[i].c;
			wei=b[i].shun;
		}
	}
	//基础排序
	sort(a,a+zhen2[0],comp2);
	for (int i=0;i<n;i++)
	ans[i]=a[i];
	zhen2[j]=0;
	//for (int i=1;i<j;i++)
	//{
	//	if (zhen[i]+zhen[0]==3)//反序
	//	{
	//		int startnum=zhen2[i+1];
	//		int endnum=zhen2[i];
	//		int ji=endnum-1;
	//		for (int k=startnum;k<endnum;k++)
	//		{
	//			ans[ji--]=a[k];
	//		}
	//	}
	//}
	int lp=0;int rp=zhen2[0]-1;
	for (int i=1;i<j;i++)
	{
		for (int k=zhen2[i]-1;k>=zhen2[i+1];k--)
		{
			if (zhen[i]+zhen[0]!=3)
			{
				ans[k]=a[rp--];
			}
			else
			{
				ans[k]=a[lp++];
			}
		}
	}
	for (int i=0;i<n;i++)
	printf("%d ",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define all(x) begin(x), end(x)
#define fill(a,b) memset(a,b,sizeof(a))
#define dbg(x) cout << (#x) << " is " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int MAX_N = 2e5+5;
const int mod = 1e9+7;
const ll INF = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
// inline bool flood_fill_check(int x, int y)
// { return (x >= 1 && x <= n && y >= 1 && y <= m); }

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v.pb(num);
    }
    int last_r = 0, start = 0;
    vector<ii> queries;
    for (int i = 0; i < m; i++)
    {
        int t, r;
        cin >> t >> r;
        if (!queries.empty() && r >= queries.back().sc) queries.pop_back();
        queries.pb(mp(t, r));
    }
    for (int i = 0; i < queries.size(); i++)
    {
        if (queries[i].ft == 1) sort(v.begin(), v.begin()+queries[i].sc);
        else sort(v.begin(), v.begin()+queries[i].sc, greater<int>());
    }
    for (int i = 0; i < n; i++) i == n-1?cout<< v[i] << endl:cout << v[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    clock_t start = clock();
    solve();
    debug("Total Time: %.4f\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define all(x) begin(x), end(x)
#define fill(a,b) memset(a,b,sizeof(a))
#define dbg(x) cout << (#x) << " is " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int MAX_N = 2e5+5;
const int mod = 1e9+7;
const ll INF = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
// inline bool flood_fill_check(int x, int y)
// { return (x >= 1 && x <= n && y >= 1 && y <= m); }

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v.pb(num);
    }
    int mx = 0, start = 0;
    vector<ii> range;
    for (int i = 0; i < m; i++)
    {
        int t, r;
        cin >> t >> r;
        range.pb(mp(t, r));
        if (r > mx) mx = r, start = i;
    }
    for (int i = start; i < m; i++)
    {
        if (range[i].ft == 1) sort(v.begin(), v.begin()+range[i].sc);
        else sort(v.begin(), v.begin()+range[i].sc, greater<int>());
    }
    for (int i = 0; i < n; i++) i == n-1?cout<< v[i] << endl:cout << v[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    clock_t start = clock();
    solve();
    debug("Total Time: %.4f\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define all(x) begin(x), end(x)
#define fill(a,b) memset(a,b,sizeof(a))
#define dbg(x) cout << (#x) << " is " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int MAX_N = 2e5+5;
const int mod = 1e9+7;
const ll INF = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
// inline bool flood_fill_check(int x, int y)
// { return (x >= 1 && x <= n && y >= 1 && y <= m); }

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v.pb(num);
    }
    int last_r = 0, start = 0, mx = 0;
    vector<ii> queries;
    for (int i = 0; i < m; i++)
    {
        int t, r;
        cin >> t >> r;
        if (!queries.empty() && r >= queries.back().sc) queries.pop_back();
        queries.pb(mp(t, r));
        if (mx <= r) mx = r, start = queries.size()-1;
    }
    for (int i = start; i < queries.size(); i++)
    {
        if (queries[i].ft == 1) sort(v.begin(), v.begin()+queries[i].sc);
        else sort(v.begin(), v.begin()+queries[i].sc, greater<int>());
    }
    for (int i = 0; i < n; i++) i == n-1?cout<< v[i] << endl:cout << v[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    clock_t start = clock();
    solve();
    debug("Total Time: %.4f\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define all(x) begin(x), end(x)
#define fill(a,b) memset(a,b,sizeof(a))
#define dbg(x) cout << (#x) << " is " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int MAX_N = 2e5+5;
const int mod = 1e9+7;
const ll INF = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
// inline bool flood_fill_check(int x, int y)
// { return (x >= 1 && x <= n && y >= 1 && y <= m); }

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v.pb(num);
    }
    int last_r = 0, start = 0, mx = 0;
    vector<ii> queries;
    for (int i = 0; i < m; i++)
    {
        int t, r;
        cin >> t >> r;
        if (!queries.empty() && r >= queries.back().sc) queries.pop_back();
        queries.pb(mp(t, r));
        if (mx <= r) mx = r, start = queries.size()-1;
    }
    cout << start << endl;
    for (int i = 0; i < queries.size(); i++) cout << queries[i].sc << " ";
    cout << endl;
    for (int i = start; i < queries.size(); i++)
    {
        if (queries[i].ft == 1) sort(v.begin(), v.begin()+queries[i].sc);
        else sort(v.begin(), v.begin()+queries[i].sc, greater<int>());
    }
    for (int i = 0; i < n; i++) i == n-1?cout<< v[i] << endl:cout << v[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    clock_t start = clock();
    solve();
    debug("Total Time: %.4f\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define all(x) begin(x), end(x)
#define fill(a,b) memset(a,b,sizeof(a))
#define dbg(x) cout << (#x) << " is " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int MAX_N = 2e5+5;
const int mod = 1e9+7;
const ll INF = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
// inline bool flood_fill_check(int x, int y)
// { return (x >= 1 && x <= n && y >= 1 && y <= m); }

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> v;
    multiset<int> have;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v.pb(num);
        have.insert(num);
    }
    int last_r = 0, start = 0, mx = 0;
    vector<ii> queries;
    for (int i = 0; i < m; i++)
    {
        int t, r;
        cin >> t >> r;
        r--;
        // forcing the order of queries to be descending
        while (!queries.empty() && r >= queries.back().sc) queries.pop_back();
        queries.pb(mp(t, r));
    }
    for (int i = n-1; i > queries[0].sc; i--) have.erase(have.find(v[i]));
    // just need to sort elements with recent updates
    for (int i = 0; i < queries.size(); i++)
    {
        int end = i == queries.size()-1?-1:queries[i+1].sc, start = queries[i].sc;
        int type = queries[i].ft;
        for (int j = start; j > end; j--)
        {
            if (type == 2) v[j] = *have.begin();
            else v[j] = *have.rbegin();
            have.erase(have.find(v[j]));
        }
    }
    for (int i = 0; i < n; i++) i == n-1?cout<< v[i] << endl:cout << v[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    clock_t start = clock();
    solve();
    debug("Total Time: %.4f\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define all(x) begin(x), end(x)
#define fill(a,b) memset(a,b,sizeof(a))
#define dbg(x) cout << (#x) << " is " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int MAX_N = 2e5+5;
const int mod = 1e9+7;
const ll INF = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
// inline bool flood_fill_check(int x, int y)
// { return (x >= 1 && x <= n && y >= 1 && y <= m); }

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v.pb(num);
    }
    int last_r = 0, start = 0, mx = 0;
    vector<ii> queries;
    for (int i = 0; i < m; i++)
    {
        int t, r;
        cin >> t >> r;
        if (!queries.empty() && r >= queries.back().sc) queries.pop_back();
        if (mx <= r) mx = r, start = i;
        queries.pb(mp(t, r));
    }
    for (int i = start; i < queries.size(); i++)
    {
        if (queries[i].ft == 1) sort(v.begin(), v.begin()+queries[i].sc);
        else sort(v.begin(), v.begin()+queries[i].sc, greater<int>());
    }
    for (int i = 0; i < n; i++) i == n-1?cout<< v[i] << endl:cout << v[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    clock_t start = clock();
    solve();
    debug("Total Time: %.4f\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define all(x) begin(x), end(x)
#define fill(a,b) memset(a,b,sizeof(a))
#define dbg(x) cout << (#x) << " is " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int MAX_N = 2e5+5;
const int mod = 1e9+7;
const ll INF = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
// inline bool flood_fill_check(int x, int y)
// { return (x >= 1 && x <= n && y >= 1 && y <= m); }

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v.pb(num);
    }
    int last_r = 0, start = 0, mx = 0;
    vector<ii> queries;
    for (int i = 0; i < m; i++)
    {
        int t, r;
        cin >> t >> r;
        if (!queries.empty() && r >= queries.back().sc) queries.pop_back();
        if (mx < r) mx = r, start = i;
        queries.pb(mp(t, r));
    }
    for (int i = start; i < queries.size(); i++)
    {
        if (queries[i].ft == 1) sort(v.begin(), v.begin()+queries[i].sc);
        else sort(v.begin(), v.begin()+queries[i].sc, greater<int>());
    }
    for (int i = 0; i < n; i++) i == n-1?cout<< v[i] << endl:cout << v[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    clock_t start = clock();
    solve();
    debug("Total Time: %.4f\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define all(x) begin(x), end(x)
#define fill(a,b) memset(a,b,sizeof(a))
#define dbg(x) cout << (#x) << " is " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int MAX_N = 2e5+5;
const int mod = 1e9+7;
const ll INF = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
// inline bool flood_fill_check(int x, int y)
// { return (x >= 1 && x <= n && y >= 1 && y <= m); }

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v.pb(num);
    }
    int mx = 0, start = 0;
    vector<ii> range;
    for (int i = 0; i < m; i++)
    {
        int t, r;
        cin >> t >> r;
        range.pb(mp(t, r));
        if (r >= mx) mx = r, start = i;
    }
    for (int i = start; i < m; i++)
    {
        if (range[i].ft == 1) sort(v.begin(), v.begin()+range[i].sc);
        else sort(v.begin(), v.begin()+range[i].sc, greater<int>());
    }
    for (int i = 0; i < n; i++) i == n-1?cout<< v[i] << endl:cout << v[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    clock_t start = clock();
    solve();
    debug("Total Time: %.4f\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define all(x) begin(x), end(x)
#define fill(a,b) memset(a,b,sizeof(a))
#define dbg(x) cout << (#x) << " is " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int MAX_N = 2e5+5;
const int mod = 1e9+7;
const ll INF = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
// inline bool flood_fill_check(int x, int y)
// { return (x >= 1 && x <= n && y >= 1 && y <= m); }

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v.pb(num);
    }
    int last_r = 0, start = 0, mx = 0;
    vector<ii> queries;
    for (int i = 0; i < m; i++)
    {
        int t, r;
        cin >> t >> r;
        if (!queries.empty() && r >= queries.back().sc) queries.pop_back();
        if (mx <= r) mx = r, start = queries.size()-1;
        queries.pb(mp(t, r));
    }
    cout << start << endl;
    for (int i = 0; i < queries.size(); i++) cout << queries[i].sc << " ";
    cout << endl;
    for (int i = start; i < queries.size(); i++)
    {
        if (queries[i].ft == 1) sort(v.begin(), v.begin()+queries[i].sc);
        else sort(v.begin(), v.begin()+queries[i].sc, greater<int>());
    }
    for (int i = 0; i < n; i++) i == n-1?cout<< v[i] << endl:cout << v[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    clock_t start = clock();
    solve();
    debug("Total Time: %.4f\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define all(x) begin(x), end(x)
#define fill(a,b) memset(a,b,sizeof(a))
#define dbg(x) cout << (#x) << " is " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int MAX_N = 2e5+5;
const int mod = 1e9+7;
const ll INF = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
// inline bool flood_fill_check(int x, int y)
// { return (x >= 1 && x <= n && y >= 1 && y <= m); }

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v.pb(num);
    }
    int last_r = 0, start = 0, mx = 0;
    vector<ii> queries;
    for (int i = 0; i < m; i++)
    {
        int t, r;
        cin >> t >> r;
        while (!queries.empty() && r >= queries.back().sc) queries.pop_back();
        queries.pb(mp(t, r));
        // if (mx <= r) mx = r, start = queries.size()-1;
    }
    for (int i = start; i < queries.size(); i++)
    {
        if (queries[i].ft == 1) sort(v.begin(), v.begin()+queries[i].sc);
        else sort(v.begin(), v.begin()+queries[i].sc, greater<int>());
    }
    for (int i = 0; i < n; i++) i == n-1?cout<< v[i] << endl:cout << v[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    clock_t start = clock();
    solve();
    debug("Total Time: %.4f\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1e9
#define pb push_back
#define mp  make_pair
#define loop(a,n) for(long long i=a;i<n;i++)
#define vil vector<long long int>
#define vi vector<int>
#define sz(v) v.size()
#define setbits(x)          __builtin_popcountll(x)
#define ff first
#define ss second
using namespace std;

int nCrevenoroddfinder(int n,int r)
{
    int a=r;
    int b=n-r;
    for(int i=0;i<30;i++)
    {
        int x=1<<i;
        if((x&a)&&(x&b))
        {
            return 0;//even
        }
    }
    return 1;//odd
}

ll gcd(ll a, ll b) {
  if (a == 0)return b;
  return gcd(b % a, a);
}
bool isperfect(ll n) {
  ll y = sqrt(n);
  if (n % y == 0 && y * y == n)return true;
  return false;
}
bool comp(pair<int,int> a, pair<int,int> b) {
    if(a.first == b.first) return a.second>b.second;
  return a.first < b.first;
}
 ll powi(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)ans = (ans * a);
    b = b >> 1;
    a = (a * a);
  }
  return ans;
}
bool isprime(ll n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)return false;
  }
  return true;
}
bool prime[10000000];
void seive() {

  prime[1] = true;
  for (int i = 2; i * i < 10000000; i++) {

    if (prime[i])continue;
    for (int j = i * i; j < 10000000; j += i) {
      prime[j] = true;
    }

  }
}

int main()
{
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int n,m;
  cin>>n>>m;
  vil v(n);
  for(int i=0;i<n;i++) cin>>v[i];
  while(m--){
        int ti;cin>>ti;
        int ri;cin>>ri;
        if(ti==1){
              sort(v.begin(),v.begin()+ri);
        }else{
              sort(v.begin(),v.begin()+ri);
              reverse(v.begin(),v.begin()+ri);
        }
  }
  for(auto x:v) cout<<x<<" ";
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1e9
#define pb push_back
#define mp  make_pair
#define loop(a,n) for(long long i=a;i<n;i++)
#define vil vector<long long int>
#define vi vector<int>
#define sz(v) v.size()
#define setbits(x)          __builtin_popcountll(x)
#define ff first
#define ss second
using namespace std;

int nCrevenoroddfinder(int n,int r)
{
    int a=r;
    int b=n-r;
    for(int i=0;i<30;i++)
    {
        int x=1<<i;
        if((x&a)&&(x&b))
        {
            return 0;//even
        }
    }
    return 1;//odd
}

ll gcd(ll a, ll b) {
  if (a == 0)return b;
  return gcd(b % a, a);
}
bool isperfect(ll n) {
  ll y = sqrt(n);
  if (n % y == 0 && y * y == n)return true;
  return false;
}
bool comp(pair<int,int> a, pair<int,int> b) {
    if(a.first == b.first) return a.second>b.second;
  return a.first < b.first;
}
 ll powi(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)ans = (ans * a);
    b = b >> 1;
    a = (a * a);
  }
  return ans;
}
bool isprime(ll n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)return false;
  }
  return true;
}
bool prime[10000000];
void seive() {

  prime[1] = true;
  for (int i = 2; i * i < 10000000; i++) {

    if (prime[i])continue;
    for (int j = i * i; j < 10000000; j += i) {
      prime[j] = true;
    }

  }
}

int main()
{
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int n,m;
  cin>>n>>m;
  vil v(n+1);
  for(int i=1;i<=n;i++) cin>>v[i];
  int lastid=-2;
  vector<pair<ll,ll>> vI;
  while(m--){
        int ti;cin>>ti;
        int ri;cin>>ri;
       while(!vI.empty()&&vI.back().ss<=ri) vI.pop_back();
       vI.pb({ti,ri});
  }
  multiset<ll> ms;vI.pb({0,0});
  for(int i=1;i<=vI[0].ss;i++ ) ms.insert(v[i]);
  for(int i=0;i<vI.size()-1;i++){
        int r=vI[i].ss,l=vI[i+1].ss+1;
        for(int j=r;j>=l;j--){
              if(vI[i].ff==1){
                    v[j]=*ms.rbegin();
                    auto it=--ms.end();
                    ms.erase(it);
              }else{
                    v[j]=*ms.begin();ms.erase(ms.begin());
              }
        }
  }
  int x;
  for(int i=1;i<=n;++i)  x=v[i], cout<<x<<" ";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
const int N=500005;
struct Option
{
    int r,t;
    int id;
} op[N];
int n,m;
int a[N];
int vis[N];
vector<Option> v;

bool cmp1(int a,int b)
{
    return a<b;
}
bool cmp2(int a,int b)
{
    return a>b;
}
int main()
{

    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
    }

    int maxxr=-1,order;

    for(int i=1; i<=m; i++)
    {
        //cout<<i<<endl;
        scanf("%d%d",&op[i].t,&op[i].r);
        op[i].id=i;
    }
    int r=op[m].r;
    v.emplace_back(op[m]);
    for(int i=m-1; i>=1; i--)
    {
        if(op[i].r>r)
        {
            //cout<<r<<endl;
            v.emplace_back(op[i]);
            r=op[i].r;
        }
    }

    int pre=v[v.size()-1].t;

    for(int i=v.size()-1; i>=0; i--)
    {
    	//cout<<op[i].r<<endl;
        if(i==v.size()-1)
        {
            if(v[i].t==1)
            {
                sort(a+1,a+v[i].r+1,cmp1);
            }
            else if(v[i].t==2)
                sort(a+1,a+v[i].r+1,cmp2);
			pre=v[i].t;
			continue;
        }
       // cout<<pre<<" "<<v[i].t<<endl;

        if(pre==v[i].t)
            continue;
		else
		{
			//
			for(int j=1;j<=v[i].r/2;j++)
			{ // cout<<j<<" "<<v[i].r-j+1<<endl;
				  swap(a[j],a[v[i].r-j+1]);
			}
		}

        pre=v[i].t;

        //cout<<v[i].r<<endl;
    }

    for(int i=1; i<=n; i++)
    {
        printf("%d ",a[i]);
    }

    return 0;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
const int N=500005;
struct Option
{
    int r,t;
    int id;
} op[N];
int n,m;
int a[N],ans[N];
int vis[N];
vector<Option> v;
int main()
{

    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        ans[i]=a[i];
    }

    int maxxr=-1,order;

    for(int i=1; i<=m; i++)
    {
        //cout<<i<<endl;
        scanf("%d%d",&op[i].t,&op[i].r);
        op[i].id=i;
    }
    int r=op[m].r;
    v.emplace_back(op[m]);
    for(int i=m-1; i>=1; i--)
    {
        if(op[i].r>r)
        {
            //cout<<r<<endl;
            v.emplace_back(op[i]);
            r=op[i].r;
        }
    }
    int maxr=v[v.size()-1].r;
    sort(a+1,a+maxr+1);

    int pre=1;
    for(int i=0; i<v.size(); i++)
    {
    	//cout<<pre<<"*"<<v[i].r<<endl;
        for(int j=pre; j<=v[i].r; j++)
        {
            vis[j]=v[i].t;
        }
        pre=v[i].r+1;
    }
    int l=1;
    r=maxr;

    for(int i=maxr; i>=1; i--)
    {
    	//cout<<vis[i]<<" ";
        if(vis[i]==2)
        {
          ans[i]=a[l++];
        }
        else
        {
        	ans[i]=a[r--];
        }
    }

    for(int i=1; i<=n; i++)
    {
        printf("%d ",ans[i]);
    }

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define pi 3.14159265359
#define int long long
#define pii pair<int,int>
const int mod=998244353;

int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};

int32_t  main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  #ifndef ONLINE_JUDGE
  freopen("input.txt","rt",stdin);
  freopen("output.txt","wt",stdout);
  #endif
int n,m;
cin>>n>>m;;

vector<int>v(n+1),ans(n+1);

for(int i=1;i<=n;i++)
{
  cin>>v[i]; ans[i]=v[i];
}

vector<int>type(m+1),val(m+1);
for(int i=1;i<=m;i++)
cin>>type[i]>>val[i];

vector<int>suf(m+1);
vector<int>id;
suf[m]=val[m];
id.push_back(0); id.push_back(m);

for(int i=m-1;i>=1;i--)
{
  if(val[i]>suf[i+1])
  {
    suf[i]=val[i];
    id.push_back(i);
  }
  else suf[i]=suf[i+1];
}

multiset<int>st;

for(int i=1;i<=val[id.back()];i++)
{
   st.insert(v[i]);
}

for(int i=id.size()-1;i>=1;i--)
{
  int x=val[id[i]],y=val[id[i-1]];

  for(int j=x;j>y;j--)
  {
    if(type[i]==1)
    {
      auto u = st.end(); u--;
        ans[j]=*u;
        st.erase(u);
    }
    else {
      ans[j]=*st.begin();
      st.erase(st.begin());
    }

  }
}

for(int i=1;i<=n;i++)cout<<ans[i]<<" ";

}


#include<bits/stdc++.h>
using namespace std;
#define pi 3.14159265359
#define int long long
#define pii pair<int,int>
const int mod=998244353;

int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};

int32_t  main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  #ifndef ONLINE_JUDGE
  freopen("input.txt","rt",stdin);
  freopen("output.txt","wt",stdout);
  #endif
int n,m;
cin>>n>>m;;

vector<int>v(n+1),ans(n+1);

for(int i=1;i<=n;i++)
{
  cin>>v[i]; ans[i]=v[i];
}

vector<int>type(m+1),val(m+1);
for(int i=1;i<=m;i++)
cin>>type[i]>>val[i];

vector<int>suf(m+1);
vector<int>id;
suf[m]=val[m];
id.push_back(0); id.push_back(m);

for(int i=m-1;i>=1;i--)
{
  if(val[i]>suf[i+1])
  {
    suf[i]=val[i];
    id.push_back(i);
  }
  else suf[i]=suf[i+1];
}

multiset<int>st;

for(int i=1;i<=val[id.back()];i++)
{
   st.insert(v[i]);
}

for(int i=id.size()-1;i>=1;i--)
{
  int x=val[id[i]],y=val[id[i-1]];

  for(int j=x;j>y;j--)
  {
    if(type[id[i]]==1)
    {
      auto u = st.end(); u--;
        ans[j]=*u;
        st.erase(u);
    }
    else {
      ans[j]=*st.begin();
      st.erase(st.begin());
    }

  }
}

for(int i=1;i<=n;i++)cout<<ans[i]<<" ";

}


/* For any given position i (1<=i<=n),
    it is only affected by the most recent update r such that r >= i.
    Firstly, the integers towards the end of the array
    which are not affected by any updates stay as they are.
    Store the elements at all positions affected atleast
    once by some update
upd[i] stores the most recent update made such that the 'r' of the update is i.
    So, for all j such that 1 <= j <= i, j has been affected by that update.
    Alternatively you can say that the last update which affects a position i is the
    most recent of the updates among positions j, i <= j <= n.
*/
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define MOD (1000000007LL)
#define LEFT(n) (2*(n))
#define RIGHT(n) (2*(n)+1)

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

ll pwr(ll base, ll p, ll mod = MOD){
ll ans = 1;while(p){if(p&1)ans=(ans*base)%mod;base=(base*base)%mod;p/=2;}return ans;
}

int n, max_affected, arr[200005], r[200005], t[200005], upd[200005];

int main() {

    int m;
    scanf("%d%d", &n, &m);
    for(int i=1;i<=n;i++)
        scanf("%d", &arr[i]);

    for(int i=1;i<=m;i++){
        scanf("%d%d", &t[i], &r[i]);
        upd[r[i]] = max(upd[r[i]], i);
        max_affected = max(max_affected, r[i]);
    }
    multiset<int> ele;
    for(int i=1;i<=max_affected;i++)
        ele.insert(arr[i]);

    int latest_update = 0;
    for(int i=n;i>=1;i--){
        latest_update = max(latest_update, upd[i]);
        if(latest_update == 0)  continue;
        if(t[latest_update] == 1){
            multiset<int>::iterator it = ele.end();
            it--;
            arr[i] = (*it);
            ele.erase(it);
        }
        else{
            arr[i] = (*ele.begin());
            ele.erase(ele.begin());
        }
    }

    for(int i=1;i<=n;i++)
        printf("%d ", arr[i]);
    return 0;
}


#pragma GCC optimize("Ofast")  // remove in mingw32 bit ;
#pragma GCC target("avx,avx2,fma")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ALL(x) x.begin(),x.end()
#define PB push_back
#define EB emplace_back
#define F first
#define S second
#define ll long long
#define double long double
#define MP make_pair
// #define int long long
// #define MAX LONG_LONG_MAX
// #define MIN LONG_LONG_MIN

using namespace std;

#ifdef CUST_DEBUG
template<class K, class V>ostream& operator<<(ostream&s,const pair<K,V>&p){s<<'<'<<p.F<<','<<p.S<<'>';return s;}
template<class T, class=typename T::value_type, class=typename enable_if<!is_same<T,string>::value>::type>
ostream& operator<<(ostream&s,const T&v){s<<'[';for(auto&x:v){s<<x<<", ";}if(!v.empty()){s<<"\b\b";}s<<']';return s;}
void __prnt(){cerr<<endl;} template<class T, class...Ts>void __prnt(T&&a,Ts&&...etc){cerr<<a<<' ';__prnt(etc...);}
#define print(...) __prnt(__VA_ARGS__)
#else
#define print(...)
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p){return cout<<"("<<p.F<<", "<<p.S<<")";}
template<typename A> ostream& operator<<(ostream &cout,vector<A> const &v){cout<<"[";for(int i=0;i<v.size();i++){if(i)cout<<", ";cout<<v[i];}return cout<<"]";}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p){cin>>p.F;return cin>>p.S;}
#endif

const long long MAXN = 2e5 +7;

ll suf[MAXN] = {0} ,a[MAXN] ,idx[MAXN] ,op[MAXN] ,ans[MAXN];

void check(){
	ll order = -1 ,n ,m;
	cin >> n >> m;
	for(ll i = 1 ;i <= n ;i++){
		cin >> a[i];
	}

	for(ll i =  1; i <= m ;i++){
		cin >> op[i] >> idx[i];
	}
	ll f = -1;
	for(ll i = n ;i > 0 ;i--){
		suf[i] = max(suf[i + 1] , idx[i]);
		f = suf[i] * (f == -1);
	}
	ll l = 1 ,r = f;
	sort(a+l ,a+r+1);

	for(ll i = n ;i >= 1 ;i--){
		if(i > f) {
			ans[i] = a[i];
			continue;
		}
		ll now = op[suf[i]];
		if(now == 1){
			ans[i] = a[r--];
		} else {
			ans[i] = a[l++];
		}
	}
	for(ll i = 1; i <= n ;i++) cout << a[i] <<" \n"[i == n];
}

int32_t main(){
	#ifndef CUST_DEBUG
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	#endif
	// cin.exceptions(cin.Failbit);
	int t = 1;
	// cin >> t;
	for(int i = 1 ; i <= t ;i++){
		// cout << "Case "<< i << ":\n";
		check();
	}
	return 0;
}


#pragma GCC optimize("Ofast")  // remove in mingw32 bit ;
#pragma GCC target("avx,avx2,fma")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ALL(x) x.begin(),x.end()
#define PB push_back
#define EB emplace_back
#define F first
#define S second
#define ll long long
#define double long double
#define MP make_pair
// #define int long long
// #define MAX LONG_LONG_MAX
// #define MIN LONG_LONG_MIN

using namespace std;

#ifdef CUST_DEBUG
template<class K, class V>ostream& operator<<(ostream&s,const pair<K,V>&p){s<<'<'<<p.F<<','<<p.S<<'>';return s;}
template<class T, class=typename T::value_type, class=typename enable_if<!is_same<T,string>::value>::type>
ostream& operator<<(ostream&s,const T&v){s<<'[';for(auto&x:v){s<<x<<", ";}if(!v.empty()){s<<"\b\b";}s<<']';return s;}
void __prnt(){cerr<<endl;} template<class T, class...Ts>void __prnt(T&&a,Ts&&...etc){cerr<<a<<' ';__prnt(etc...);}
#define print(...) __prnt(__VA_ARGS__)
#else
#define print(...)
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p){return cout<<"("<<p.F<<", "<<p.S<<")";}
template<typename A> ostream& operator<<(ostream &cout,vector<A> const &v){cout<<"[";for(int i=0;i<v.size();i++){if(i)cout<<", ";cout<<v[i];}return cout<<"]";}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p){cin>>p.F;return cin>>p.S;}
#endif

const long long MAXN = 2e5 +7;

ll suf[MAXN] = {0} ,a[MAXN] ,idx ,op[MAXN] ,ans[MAXN];

void check(){
	ll order = -1 ,n ,m;
	cin >> n >> m;
	for(ll i = 1 ;i <= n ;i++){
		cin >> a[i];
	}

	for(ll i =  1; i <= m ;i++){
		cin >> op[i] >> idx;
		suf[idx] = i;
	}
	ll f = -1;
	for(ll i = n ;i > 0 ;i--){
		suf[i] = max(suf[i + 1] , suf[i]);
		if(f == -1 && suf[i]) f = i;
	}
	ll l = 1 ,r = f;
	sort(a+l ,a+r+1);

	for(ll i = n ;i >= 1 ;i--){
		if(i > f) {
			ans[i] = a[i];
			continue;
		}
		ll now = op[suf[i]];
		if(now == 1){
			ans[i] = a[r--];
		} else {
			ans[i] = a[l++];
		}
	}
	for(ll i = 1; i <= n ;i++) cout << ans[i] <<" \n"[i == n];
}

int32_t main(){
	#ifndef CUST_DEBUG
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	#endif
	// cin.exceptions(cin.Failbit);
	int t = 1;
	// cin >> t;
	for(int i = 1 ; i <= t ;i++){
		// cout << "Case "<< i << ":\n";
		check();
	}
	return 0;
}