#include
<iostream>
#include <stdio.h>
#define MAXN 50100
#define MAXT 1000010
#define max (a ,b ) a >b ?a :b
#define min (a ,b ) a >b ?b :a
using namespace std ;
int a [MAXN ],tl [MAXT ],tr [MAXT ],tmax [MAXT ],tmin [MAXT ];
void creat ( int v , int f , int t )
{
tl [v ]=f ;
tr [v ]=t ;
if (f ==t )
{
tmax [v ]=a [f ];
tmin [v ]=a [f ];
return ;
}
int mid =(f +t )/ 2 ;
creat (v * 2 ,f ,mid );
creat (v * 2+1 ,mid +1 ,t );
tmax [v ]= max (tmax [v * 2 ],tmax [v * 2+1 ]);
tmin [v ]= min (tmin [v * 2 ],tmin [v * 2+1 ]);
}
int q ( int v , int l , int r , int flag )
{
if (l ==tl [v ]&&r ==tr [v ])
{
return flag ?tmin [v ]:tmax [v ];
}
int mid =(tl [v ]+tr [v ])/ 2 ;
if (r <=mid )
return q (v * 2 ,l ,r ,flag );
else if (l >mid )
return q (v * 2+1 ,l ,r ,flag );
else
{
if (flag == 1 )
return min ( q (v * 2 ,l ,mid ,flag ), q (v * 2+1 ,mid +1 ,r ,flag ));
else
return max ( q (v * 2 ,l ,mid ,flag ), q (v * 2+1 ,mid +1 ,r ,flag ));
}
}
int main ()
{
int n ,t ,a1 ,b1 ;
while ( scanf ( "%d%d" ,&n ,&t )!=EOF )
{
for ( int i = 1 ;i <=n ;i ++)
scanf ( "%d" ,&a [i ]);
creat ( 1 , 1 ,n );
while (t --)
{
scanf ( "%d%d" ,&a1 ,&b1 );
printf ( "%d \n " , q ( 1 ,a1 ,b1 , 0 )- q ( 1 ,a1 ,b1 , 1 ));
}
}
return 0 ;
}
#include <stdio.h>
#define MAXN 50100
#define MAXT 1000010
#define max (a ,b ) a >b ?a :b
#define min (a ,b ) a >b ?b :a
using namespace std ;
int a [MAXN ],tl [MAXT ],tr [MAXT ],tmax [MAXT ],tmin [MAXT ];
void creat ( int v , int f , int t )
{
tl [v ]=f ;
tr [v ]=t ;
if (f ==t )
{
tmax [v ]=a [f ];
tmin [v ]=a [f ];
return ;
}
int mid =(f +t )/ 2 ;
creat (v * 2 ,f ,mid );
creat (v * 2+1 ,mid +1 ,t );
tmax [v ]= max (tmax [v * 2 ],tmax [v * 2+1 ]);
tmin [v ]= min (tmin [v * 2 ],tmin [v * 2+1 ]);
}
int q ( int v , int l , int r , int flag )
{
if (l ==tl [v ]&&r ==tr [v ])
{
return flag ?tmin [v ]:tmax [v ];
}
int mid =(tl [v ]+tr [v ])/ 2 ;
if (r <=mid )
return q (v * 2 ,l ,r ,flag );
else if (l >mid )
return q (v * 2+1 ,l ,r ,flag );
else
{
if (flag == 1 )
return min ( q (v * 2 ,l ,mid ,flag ), q (v * 2+1 ,mid +1 ,r ,flag ));
else
return max ( q (v * 2 ,l ,mid ,flag ), q (v * 2+1 ,mid +1 ,r ,flag ));
}
}
int main ()
{
int n ,t ,a1 ,b1 ;
while ( scanf ( "%d%d" ,&n ,&t )!=EOF )
{
for ( int i = 1 ;i <=n ;i ++)
scanf ( "%d" ,&a [i ]);
creat ( 1 , 1 ,n );
while (t --)
{
scanf ( "%d%d" ,&a1 ,&b1 );
printf ( "%d \n " , q ( 1 ,a1 ,b1 , 0 )- q ( 1 ,a1 ,b1 , 1 ));
}
}
return 0 ;
}