// In The Name Of God :
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std ;
using namespace __gnu_pbds;
#define X first
#define Y second
//#define int long long
typedef int ll ;
typedef pair < long long , long long > pii ;
typedef pair < pii , long long > pii_i ;
#define ordered_set(TYPE) tree<TYPE, null_type, less<TYPE>, rb_tree_tag, tree_order_statistics_node_update>
//#define "%I64d" "%I64d"
const long long maxn = 1e5 + 20 ;
const long long maxL = 20 ;
//const int inf = 1e12 ;
int n , m , ar [ maxn ] ;
ll data [ 4 * maxn ] ;
ll flag [ 4 * maxn ] ;
set < int > ST ;

inline void relax_flag ( const ll &st , const ll &en ,const  int id )
{
  data [ id ] += (en-st)*flag[id] ;
  if ( en - st > 1 )
    {
      flag [ id<<1 ] += flag [ id ] ;
      flag [ id<<1^1 ] += flag [ id ] ;
    }
  flag [ id ] = 0 ;
}

inline int get ( const int &l ,const  int &r ,const ll &st = 0 , const ll &en = n , const int &id = 1 )
{
  relax_flag ( st , en , id ) ;
  if ( r <= st || en <= l )
    return 0 ;
  if ( l <= st && en <= r )
    return data [ id ] ;
  int mid = ( st + en ) >> 1 ;
  return get ( l , r , st , mid , id<<1 ) +
    get( l , r , mid , en , id<<1^1 ) ;
}

inline void add (const  int &l ,const int &r ,const ll &val , const ll &st = 0 , const ll &en = n , const int &id = 1 )
{
  relax_flag ( st , en , id ) ;
  if ( r <= st || en <= l )
    return ;
  if ( l <= st && en <= r )
    {
      flag [ id ] += val ;
      relax_flag ( st , en , id ) ;
      return ;
    }
  int mid = ( st + en ) >> 1 ;
  add ( l , r , val , st , mid , id<<1 ) ;
  add ( l , r , val , mid , en , id<<1^1 ) ;
  data [ id ] = get ( st , mid , st , mid , id<<1 ) +
    get ( mid , en , mid , en , id<<1^1 ) ;
}

main ( )
{
  scanf ( "%d%d" , &n , &m ) ;
  for ( int i = 0 ; i < n ; i ++ )
    ST . insert ( i ) , ar [ i ] = i + 1 ;
  ST . insert ( n ) ;
  int type , l , r , c , cur ;
  set < int > :: iterator it ;
  for ( int i = 0 ; i < m ; i ++ )
    {
      scanf ( "%d" , &type ) ;
      if ( type == 1 )
	{
	  scanf ( "%d%d%d" , &l , &r , &c ) ;
	  l -- ;
	  if ( ST . find ( l ) == ST . end ( ) )
	    it=(lower_bound(ST.begin(),ST.end(),l)), it-- , ar [ l ] = ar [ *it ] ,
	      ST . insert ( l ) ;
	  if ( ST . find ( r ) == ST . end ( ) )
	    it=(lower_bound(ST.begin(),ST.end(),r)), it-- , ar [ r ] = ar [ *it ] ,
	      ST . insert ( r ) ;
	  it = upper_bound ( ST . begin ( ) , ST . end ( ) , l ) ;
	  cur = l ;
	  while ( 1 )
	    {
	      //	      cerr << "ADD: " << cur << ' ' << *it << ' ' << abs ( c - ar [ cur ] ) << endl ;
	      add ( cur , *it , abs ( c - ar [ cur ] ) ) ;
	      cur = *it ;
	      if ( cur == r )
		break ;
	      it ++ ;
	    }
	  ar [ l ] = c ;
	  it = upper_bound ( ST . begin ( ) , ST . end ( ) , l ) ;
	  while ( *it < r )
	    {
	      cur = *it ;
	      it ++ ;
	      ST . erase ( cur ) ;
	    }
	}
      else
	scanf("%d%d" , &l , &r ) , printf("%d\n" , get ( l - 1 , r ) ) ;
    }
}


// In The Name Of God :
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std ;
using namespace __gnu_pbds;
#define X first
#define Y second
//#define int long long
typedef int ll ;
typedef pair < long long , long long > pii ;
typedef pair < pii , long long > pii_i ;
#define ordered_set(TYPE) tree<TYPE, null_type, less<TYPE>, rb_tree_tag, tree_order_statistics_node_update>
//#define "%I64d" "%I64d"
const long long maxn = 1e5 + 20 ;
const long long maxL = 20 ;
//const int inf = 1e12 ;
int n , m , ar [ maxn ] ;
ll data [ 4 * maxn ] ;
ll flag [ 4 * maxn ] ;
set < int > ST ;

inline void relax_flag ( const ll &st , const ll &en ,const  int id )
{
  data [ id ] += (en-st)*flag[id] ;
  if ( en - st > 1 )
    {
      flag [ id<<1 ] += flag [ id ] ;
      flag [ id<<1^1 ] += flag [ id ] ;
    }
  flag [ id ] = 0 ;
}

int get ( const int &l ,const  int &r , ll st = 0 , ll en = n , int id = 1 )
{
  relax_flag ( st , en , id ) ;
  if ( r <= st || en <= l )
    return 0 ;
  if ( l <= st && en <= r )
    return data [ id ] ;
  int mid = ( st + en ) >> 1 ;
  return get ( l , r , st , mid , id<<1 ) +
    get( l , r , mid , en , id<<1^1 ) ;
}

void add (const  int &l ,const int &r ,const ll &val , ll st = 0 , ll en = n , int id = 1 )
{
  relax_flag ( st , en , id ) ;
  if ( r <= st || en <= l )
    return ;
  if ( l <= st && en <= r )
    {
      flag [ id ] += val ;
      relax_flag ( st , en , id ) ;
      return ;
    }
  int mid = ( st + en ) >> 1 ;
  add ( l , r , val , st , mid , id<<1 ) ;
  add ( l , r , val , mid , en , id<<1^1 ) ;
  data [ id ] = get ( st , mid , st , mid , id<<1 ) +
    get ( mid , en , mid , en , id<<1^1 ) ;
}

main ( )
{
  scanf ( "%d%d" , &n , &m ) ;
  for ( int i = 0 ; i < n ; i ++ )
    ST . insert ( i ) , ar [ i ] = i + 1 ;
  ST . insert ( n ) ;
  int type , l , r , c , cur ;
  set < int > :: iterator it ;
  for ( int i = 0 ; i < m ; i ++ )
    {
      scanf ( "%d" , &type ) ;
      if ( type == 1 )
	{
	  scanf ( "%d%d%d" , &l , &r , &c ) ;
	  l -- ;
	   it = lower_bound ( ST . begin ( ) , ST . end ( ) , l ) ;
	  if ( ST . find ( l ) == ST . end ( ) )
	    it=(lower_bound(ST.begin(),ST.end(),l)), it-- , ar [ l ] = ar [ *it ] ,
	    ST . insert ( l ) ;
	  if ( ST . find ( r ) == ST . end ( ) )
	    it=(lower_bound(ST.begin(),ST.end(),r)), it-- , ar [ r ] = ar [ *it ] ,
	      ST . insert ( r ) ;
	  it = upper_bound ( ST . begin ( ) , ST . end ( ) , l ) ;
	  cur = l ;
	  while ( 1 )
	    {
	      //	      cerr << "ADD: " << cur << ' ' << *it << ' ' << abs ( c - ar [ cur ] ) << endl ;
	      add ( cur , *it , abs ( c - ar [ cur ] ) ) ;
	      cur = *it ;
	      if ( cur == r )
		break ;
	      it ++ ;
	    }
	  ar [ l ] = c ;
	  it = upper_bound ( ST . begin ( ) , ST . end ( ) , l ) ;
	  while ( *it < r )
	    {
	      cur = *it ;
	      it ++ ;
	      ST . erase ( cur ) ;
	    }
	}
      else
	scanf("%d%d" , &l , &r ) , printf("%d\n" , get ( l - 1 , r ) ) ;
    }
}


#include<bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 10;
int n, m;
int choice, L, R, x;
struct node {
	long long l, r;
	long long sum;
	long long  color;
	int same;
	long long  lazy;
}tree[maxn *2];
void pushup(int rt) {
	if (tree[rt << 1].color == tree[rt << 1 | 1].color && tree[rt<<1].color !=0) {
		tree[rt].same = 1;
		tree[rt].color = tree[rt << 1].color;
	}
	else {
		tree[rt].same = 0;
		tree[rt].color = 0;
	}
	tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
	return;
}
void pushdown(int rt) {
	tree[rt << 1].lazy = tree[rt].lazy;
	tree[rt << 1|1].lazy = tree[rt].lazy;
	tree[rt << 1].color = tree[rt].color;
	tree[rt << 1|1].color = tree[rt].color;
	tree[rt << 1].sum += tree[rt].lazy * (tree[rt << 1].r - tree[rt << 1].l + 1);
	tree[rt << 1 | 1].sum += tree[rt].lazy * (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1);
	tree[rt ].lazy = 0;
    tree[rt].color = 0;
	return;
}
void build(int rt, int l, int r) {
	tree[rt].l = l;
	tree[rt].r = r;
	tree[rt].lazy = 0;
	if (l == r) {
		tree[rt].color = l;
		tree[rt].sum = 0;
		tree[rt].same = 1;
		return;
	}
	int mid = (l + r) / 2;
	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
	pushup(rt);
}
void update(int rt, int L, int R, int x) {

	if (L <= tree[rt].l && tree[rt].r <= R && tree[rt].same) {
		tree[rt].lazy += abs(tree[rt].color - x);
		tree[rt].sum += abs(tree[rt].color - x) * (tree[rt].r - tree[rt].l + 1);
		tree[rt].color = x;
		return;
	}
	if (tree[rt].l == tree[rt].r)return;
	if (tree[rt].lazy)pushdown(rt);
	if (tree[rt << 1].r >= L)update(rt << 1, L, R, x);
	if (tree[rt << 1|1].l <= R)update(rt << 1 | 1, L, R, x);
	pushup(rt);
}
long long query(int rt, int L, int R) {
	if (L <= tree[rt].l && tree[rt].r <= R)return tree[rt].sum;
	long long ans = 0;
	if (tree[rt].l == tree[rt].r)return ans;
	if (tree[rt].lazy)pushdown(rt);
	if (tree[rt << 1].r >= L)ans+=query(rt << 1, L, R);
	if (tree[rt << 1 | 1].l <= R)ans+=query(rt << 1 | 1, L, R);
	return ans;
}
int main() {
	cin >> n >> m;
	build(1, 1, n);
	while (m--) {
		cin >> choice >> L >> R;
		if (choice == 1) {
			cin >> x;
			update(1, L, R, x);
		}
		else {
			long long ans = query(1, L, R);
			cout << ans << endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 10;
int n, m;
int choice, L, R;
long long x;
struct node {
	long long l, r;
	long long sum;
	long long  color;
	long long  same;
	long long  lazy;
}tree[maxn *4];
void pushup(int rt) {
	if (tree[rt << 1].color == tree[rt << 1 | 1].color && tree[rt<<1].color !=0) {
		tree[rt].same = 1;
		tree[rt].color = tree[rt << 1].color;
	}
	else {
		tree[rt].same = 0;
		tree[rt].color = 0;
	}
	tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
	return;
}
void pushdown(int rt) {
	tree[rt << 1].lazy += tree[rt].lazy;
	tree[rt << 1|1].lazy += tree[rt].lazy;
	tree[rt << 1].color = tree[rt].color;
	tree[rt << 1|1].color = tree[rt].color;
	tree[rt << 1].sum += tree[rt].lazy * (tree[rt << 1].r - tree[rt << 1].l + 1);
	tree[rt << 1 | 1].sum += tree[rt].lazy * (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1);
	tree[rt ].lazy = 0;
    tree[rt].color = 0;
	return;
}
void build(int rt, int l, int r) {
	tree[rt].l = l;
	tree[rt].r = r;
	tree[rt].lazy = 0;
	if (l == r) {
		tree[rt].color = l;
		tree[rt].sum = 0;
		tree[rt].same = 1;
		return;
	}
	int mid = (l + r) / 2;
	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
	pushup(rt);
}
void update(int rt, int L, int R, long long  x) {

	if (L <= tree[rt].l && tree[rt].r <= R && tree[rt].same) {
		tree[rt].lazy += abs(tree[rt].color - x);
		tree[rt].sum += abs(tree[rt].color - x) * (tree[rt].r - tree[rt].l + 1);
		tree[rt].color = x;
		return;
	}
	if (tree[rt].l == tree[rt].r)return;
	if (tree[rt].lazy)pushdown(rt);
	if (tree[rt << 1].r >= L)update(rt << 1, L, R, x);
	if (tree[rt << 1|1].l <= R)update(rt << 1 | 1, L, R, x);
	pushup(rt);
}
long long query(int rt, int L, int R) {
	if (L <= tree[rt].l && tree[rt].r <= R)return tree[rt].sum;
	long long ans = 0;
	if (tree[rt].l == tree[rt].r)return ans;
	if (tree[rt].lazy)pushdown(rt);
	if (tree[rt << 1].r >= L)ans+=query(rt << 1, L, R);
	if (tree[rt << 1 | 1].l <= R)ans+=query(rt << 1 | 1, L, R);
    pushup(rt);
    return ans;
}
int main() {
	cin >> n >> m;
	build(1, 1, n);
	while (m--) {
		cin >> choice >> L >> R;
		if (choice == 1) {
			cin >> x;
			update(1, L, R, x);
		}
		else {
			long long ans = query(1, L, R);
			cout << ans << endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 10;
int n, m;
int choice, L, R, x;
struct node {
	long long l, r;
	long long sum;
	long long  color;
	long long  same;
	long long  lazy;
}tree[maxn *2];
void pushup(int rt) {
	if (tree[rt << 1].color == tree[rt << 1 | 1].color && tree[rt<<1].color !=0) {
		tree[rt].same = 1;
		tree[rt].color = tree[rt << 1].color;
	}
	else {
		tree[rt].same = 0;
		tree[rt].color = 0;
	}
	tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
	return;
}
void pushdown(int rt) {
	tree[rt << 1].lazy += tree[rt].lazy;
	tree[rt << 1|1].lazy += tree[rt].lazy;
	tree[rt << 1].color = tree[rt].color;
	tree[rt << 1|1].color = tree[rt].color;
	tree[rt << 1].sum += tree[rt].lazy * (tree[rt << 1].r - tree[rt << 1].l + 1);
	tree[rt << 1 | 1].sum += tree[rt].lazy * (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1);
	tree[rt ].lazy = 0;
    tree[rt].color = 0;
	return;
}
void build(int rt, int l, int r) {
	tree[rt].l = l;
	tree[rt].r = r;
	tree[rt].lazy = 0;
	if (l == r) {
		tree[rt].color = l;
		tree[rt].sum = 0;
		tree[rt].same = 1;
		return;
	}
	int mid = (l + r) / 2;
	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
	pushup(rt);
}
void update(int rt, int L, int R, int x) {

	if (L <= tree[rt].l && tree[rt].r <= R && tree[rt].same) {
		tree[rt].lazy += abs(tree[rt].color - x);
		tree[rt].sum += abs(tree[rt].color - x) * (tree[rt].r - tree[rt].l + 1);
		tree[rt].color = x;
		return;
	}
	if (tree[rt].l == tree[rt].r)return;
	if (tree[rt].lazy)pushdown(rt);
	if (tree[rt << 1].r >= L)update(rt << 1, L, R, x);
	if (tree[rt << 1|1].l <= R)update(rt << 1 | 1, L, R, x);
	pushup(rt);
}
long long query(int rt, int L, int R) {
	if (L <= tree[rt].l && tree[rt].r <= R)return tree[rt].sum;
	long long ans = 0;
	if (tree[rt].l == tree[rt].r)return ans;
	if (tree[rt].lazy)pushdown(rt);
	if (tree[rt << 1].r >= L)ans+=query(rt << 1, L, R);
	if (tree[rt << 1 | 1].l <= R)ans+=query(rt << 1 | 1, L, R);
	return ans;
}
int main() {
	cin >> n >> m;
	build(1, 1, n);
	while (m--) {
		cin >> choice >> L >> R;
		if (choice == 1) {
			cin >> x;
			update(1, L, R, x);
		}
		else {
			long long ans = query(1, L, R);
			cout << ans << endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;

typedef long long ll;

struct tr{
    long long add, sum;
    int col;
} t[N * 4];

int n, m;

void build(int v, int L, int R) {
    if (L == R) {
        t[v].col = L;
        return;
    }
    int md = (L + R) >> 1;
    build(v * 2, L, md);
    build(v * 2 + 1, md + 1, R);
    if (t[v * 2].col == t[v * 2 + 1].col)
        t[v].col = t[v * 2].col;
}

void push(int v, int L, int R) {
    if (t[v].add) {
        t[v].sum += (R - L + 1) * (t[v].add);
        if (L != R) {
            t[v * 2].add += t[v].add;
            t[v * 2 + 1].add += t[v].add;
        }
        if (t[v].col) {
            t[v * 2].col = t[v].col;
            t[v * 2 + 1].col = t[v].col;
        }
        t[v].add = 0;
    }
}

void upd(int v, int L, int R, int l, int r, int x) {
    push(v, L, R);
    if (l > R || L > r)
        return;
    if (l <= L && R <= r) {
        if (t[v].col) {
            t[v].add += abs(t[v].col - x);
            t[v].col = x;
            push(v, L, R);
            return;
        }
    }
    int md = (L + R) >> 1;
    upd(v * 2, L, md, l, r, x);
    upd(v * 2 + 1, md + 1, R, l, r, x);
    if (t[v * 2].col == t[v * 2 + 1].col)
        t[v].col = t[v * 2].col;
    else
        t[v].col = 0;
    t[v].sum = t[v * 2].sum + t[v * 2 + 1].sum;
}

long long get(int v, int L, int R, int l, int r) {
    push(v, L, R);
    if (L > r || l > R)
        return 0;
    if (l <= L && R <= r)
        return t[v].sum;
    int md = (L + R) >> 1;
    return get(v * 2, L, md, l, r) + get(v * 2 + 1, md + 1, R, l, r);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    build(1, 1, n);
    for (int i = 1; i <= m; i++) {
        int t, l, r;
        cin >> t >> l >> r;
        if (t == 1) {
            int x;
            cin >> x;
            upd(1, 1, n, l, r, x);
        } else {
            cout << get(1, 1, n, l, r) << "\n";
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

typedef long long ll;

struct tr{
    long long add, sum;
    int col;
} t[N * 4];

int n, m;

void build(int v, int L, int R) {
    if (L == R) {
        t[v].col = L;
        return;
    }
    int md = (L + R) >> 1;
    build(v * 2, L, md);
    build(v * 2 + 1, md + 1, R);
    if (t[v * 2].col == t[v * 2 + 1].col)
        t[v].col = t[v * 2].col;
}

void push(int v, int L, int R) {
    if (t[v].add) {
        t[v].sum += (R - L + 1) * (t[v].add);
        if (L != R) {
            t[v * 2].add += t[v].add;
            t[v * 2 + 1].add += t[v].add;
        }
        if (t[v].col) {
            t[v * 2].col = t[v].col;
            t[v * 2 + 1].col = t[v].col;
        }
        t[v].add = 0;
    }
}

void upd(int v, int L, int R, int l, int r, int x) {
    push(v, L, R);
    if (l > R || L > r)
        return;
    if (l <= L && R <= r) {
        if (t[v].col) {
            t[v].add += abs(t[v].col - x);
            t[v].col = x;
            push(v, L, R);
            return;
        }
    }
    int md = (L + R) >> 1;
    upd(v * 2, L, md, l, r, x);
    upd(v * 2 + 1, md + 1, R, l, r, x);
    if (t[v * 2].col == t[v * 2 + 1].col)
        t[v].col = t[v * 2].col;
    else
        t[v].col = 0;
    t[v].sum = t[v * 2].sum + t[v * 2 + 1].sum;
}

long long get(int v, int L, int R, int l, int r) {
    push(v, L, R);
    if (L > r || l > R)
        return 0;
    if (l <= L && R <= r)
        return t[v].sum;
    int md = (L + R) >> 1;
    return get(v * 2, L, md, l, r) + get(v * 2 + 1, md + 1, R, l, r);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    build(1, 1, n);
    for (int i = 1; i <= m; i++) {
        int t, l, r;
        cin >> t >> l >> r;
        if (t == 1) {
            int x;
            cin >> x;
            upd(1, 1, n, l, r, x);
        } else {
            cout << get(1, 1, n, l, r) << "\n";
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

typedef long long ll;

struct tr{
    long long add, sum;
    int col;
} t[N * 4];

int n, m;

void build(int v, int L, int R) {
    if (L == R) {
        t[v].col = L;
        return;
    }
    int md = (L + R) >> 1;
    build(v * 2, L, md);
    build(v * 2 + 1, md + 1, R);
    if (t[v * 2].col == t[v * 2 + 1].col)
        t[v].col = t[v * 2].col;
}

void push(int v, int L, int R) {
    if (t[v].add) {
        t[v].sum += (R - L + 1) * (t[v].add);
        if (L != R) {
            t[v * 2].add += t[v].add;
            t[v * 2 + 1].add += t[v].add;
        }
        if (t[v].col) {
            t[v * 2].col = t[v].col;
            t[v * 2 + 1].col = t[v].col;
        }
        t[v].add = 0;
    }
}

void upd(int v, int L, int R, int l, int r, int x) {
    push(v, L, R);
    if (l > R || L > r)
        return;
    if (l <= L && R <= r) {
        if (t[v].col) {
            t[v].add += abs(t[v].col - x);
            t[v].col = x;
            push(v, L, R);
            return;
        }
    }
    int md = (L + R) >> 1;
    upd(v * 2, L, md, l, r, x);
    upd(v * 2 + 1, md + 1, R, l, r, x);
    if (t[v * 2].col == t[v * 2 + 1].col)
        t[v].col = t[v * 2].col;
    else
        t[v].col = 0;
    t[v].sum = t[v * 2].sum + t[v * 2 + 1].sum;
}

int get(int v, int L, int R, int l, int r) {
    push(v, L, R);
    if (L > r || l > R)
        return 0;
    if (l <= L && R <= r)
        return t[v].sum;
    int md = (L + R) >> 1;
    return get(v * 2, L, md, l, r) + get(v * 2 + 1, md + 1, R, l, r);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    build(1, 1, n);
    for (int i = 1; i <= m; i++) {
        int t, l, r;
        cin >> t >> l >> r;
        if (t == 1) {
            int x;
            cin >> x;
            upd(1, 1, n, l, r, x);
        } else {
            cout << get(1, 1, n, l, r) << "\n";
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;
#define clr(x,y) memset(x,y,sizeof x)
#define INF 0x3f3f3f3f
const ll Mod = 1e9 + 7;

ll sum[maxn << 2],add[maxn << 2];
int tree[maxn << 2];

ll Abs(ll x){return x > 0 ? x : -x;}
void build(int l,int r,int rt)
{
    tree[rt] = -1;sum[rt] = 0;add[rt] = 0;
    if(l == r)
    {
        tree[rt] = l;return;
    }
    int mid = (l + r) >> 1;
    build(l,mid,rt << 1);build(mid + 1,r,rt << 1|1);
}

void push_down(int l,int r,int rt)
{
    if(tree[rt] != -1)
    {
        int mid = (l + r) >> 1;
        add[rt << 1] += add[rt];add[rt << 1|1] += add[rt];
        sum[rt << 1] += add[rt] * (mid - l + 1);
        sum[rt << 1|1] += add[rt] * (r - mid);
        add[rt] = 0;
        tree[rt << 1] = tree[rt << 1|1] = tree[rt];
        tree[rt] = -1;
    }
}
void push_up(int rt)
{
    if(tree[rt << 1] == tree[rt << 1|1])
        tree[rt] = tree[rt << 1];
    else tree[rt] = -1;
    sum[rt] = sum[rt << 1] + sum[rt << 1|1];
}
void update(int L,int R,int x,int l,int r,int rt)
{
    if(tree[rt] == x)return;
    if(L <= l && R >= r)
    {
        if(tree[rt] != -1)
        {
            sum[rt] += 1ll * Abs(tree[rt] - x) * (r - l + 1);
            add[rt] += Abs(x - tree[rt]);
            tree[rt] = x;
            return;
        }
    }
    push_down(l,r,rt);
    int mid = (l + r) >> 1;
    if(L <= mid)
        update(L,R,x,l,mid,rt << 1);
    if(R >= mid + 1)
        update(L,R,x,mid + 1,r,rt << 1|1);
    push_up(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
    if(L <= l && R >= r)
    {
        if(tree[rt] != -1)
        {
            return sum[rt];
        }
    }
    push_down(l,r,rt);
    int mid = (l + r) >> 1;
    ll ret = 0;
    if(L <= mid)
        ret += query(L,R,l,mid,rt <<1);
    if(R >= mid + 1)
        ret += query(L,R,mid + 1,r,rt <<1|1);
    return ret;
}

int main()
{
    int n,m;
    while( ~ scanf("%d%d",&n,&m))
    {
        build(1,n,1);
        while(m --)
        {
            int type;scanf("%d",&type);
            if(type == 1)
            {
                int x,y,z;scanf("%d%d%d",&x,&y,&z);
                update(x,y,z,1,n,1);
            }
            else
            {
                int x,y;scanf("%d%d",&x,&y);
                printf("%lld\n",query(x,y,1,n,1));
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;
#define clr(x,y) memset(x,y,sizeof x)
#define INF 0x3f3f3f3f
const ll Mod = 1e9 + 7;

ll sum[maxn << 2],add[maxn << 2];
int tree[maxn << 2];

void build(int l,int r,int rt)
{
    tree[rt] = -1;sum[rt] = 0;add[rt] = 0;
    if(l == r)
    {
        tree[rt] = l;return;
    }
    int mid = (l + r) >> 1;
    build(l,mid,rt << 1);build(mid + 1,r,rt << 1|1);
}

void push_down(int l,int r,int rt)
{
    if(tree[rt] != -1)
    {
        int mid = (l + r) >> 1;
        add[rt << 1] += add[rt];add[rt << 1|1] += add[rt];
        sum[rt << 1] += 1ll * add[rt] * (mid - l + 1);
        sum[rt << 1|1] += 1ll* add[rt] * (r - mid);
        add[rt] = 0;
        tree[rt << 1] = tree[rt << 1|1] = tree[rt];
    }
}
void push_up(int rt)
{
    if(tree[rt << 1] == tree[rt << 1|1])
        tree[rt] = tree[rt << 1];
    else tree[rt] = -1;
    sum[rt] = sum[rt << 1] + sum[rt << 1|1];
}
void update(int L,int R,int x,int l,int r,int rt)
{
    if(tree[rt] == x)return;
    if(L <= l && R >= r)
    {
        if(tree[rt] != -1)
        {
            sum[rt] += 1ll * abs(tree[rt] - x) * (r - l + 1);
            add[rt] += abs(x - tree[rt]);
            tree[rt] = x;
            return;
        }
    }
    push_down(l,r,rt);
    int mid = (l + r) >> 1;
    if(L <= mid)
        update(L,R,x,l,mid,rt << 1);
    if(R >= mid + 1)
        update(L,R,x,mid + 1,r,rt << 1|1);
    push_up(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
    if(L <= l && R >= r)
    {
        if(tree[rt] != -1)
        {
            return sum[rt];
        }
    }
    push_down(l,r,rt);
    int mid = (l + r) >> 1;
    ll ret = 0;
    if(L <= mid)
        ret += query(L,R,l,mid,rt <<1);
    if(R >= mid + 1)
        ret += query(L,R,mid + 1,r,rt <<1|1);
    return ret;
}

int main()
{
    int n,m;
    while( ~ scanf("%d%d",&n,&m))
    {
        build(1,n,1);
        while(m --)
        {
            int type;scanf("%d",&type);
            if(type == 1)
            {
                int x,y,z;scanf("%d%d%d",&x,&y,&z);
                update(x,y,z,1,n,1);
            }
            else
            {
                int x,y;scanf("%d%d",&x,&y);
                printf("%lld\n",query(x,y,1,n,1));
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;
#define clr(x,y) memset(x,y,sizeof x)
#define INF 0x3f3f3f3f
const ll Mod = 1e9 + 7;

ll sum[maxn << 2],add[maxn << 2];
int tree[maxn << 2];

void build(int l,int r,int rt)
{
    tree[rt] = -1;sum[rt] = 0;add[rt] = 0;
    if(l == r)
    {
        tree[rt] = l;return;
    }
    int mid = (l + r) >> 1;
    build(l,mid,rt << 1);build(mid + 1,r,rt << 1|1);
}

void push_down(int l,int r,int rt)
{
    if(tree[rt] != -1)
    {
        int mid = (l + r) >> 1;
        add[rt << 1] += add[rt];add[rt << 1|1] += add[rt];
        sum[rt << 1] += 1ll * add[rt] * (mid - l + 1);
        sum[rt << 1|1] += 1ll* add[rt] * (r - mid);
        add[rt] = 0;
        tree[rt << 1] = tree[rt << 1|1] = tree[rt];
    }
}
void push_up(int rt)
{
    if(tree[rt << 1] == tree[rt << 1|1])
        tree[rt] = tree[rt << 1];
    else tree[rt] = -1;
    sum[rt] = sum[rt << 1] + sum[rt << 1|1];
}
void update(int L,int R,int x,int l,int r,int rt)
{
//    cout << l << "  " << r << " " << tree[rt] << " " << tree[rt << 1] << endl;
    if(L <= l && R >= r)
    {
        if(tree[rt] != -1)
        {
//            cout << l << " -- " << r << endl;
            sum[rt] += 1ll * abs(tree[rt] - x) * (r - l + 1);
            add[rt] += abs(x - tree[rt]);
            tree[rt] = x;
            return;
        }
    }
    push_down(l,r,rt);
    int mid = (l + r) >> 1;
    if(L <= mid)
        update(L,R,x,l,mid,rt << 1);
    if(R >= mid + 1)
        update(L,R,x,mid + 1,r,rt << 1|1);
    push_up(rt);
}
ll query(int L,int R,int l,int r,int rt)
{

    if(L <= l && R >= r)
    {
        if(tree[rt] != -1)
        {
            return sum[rt];
        }
    }
    push_down(l,r,rt);
    int mid = (l + r) >> 1;
    ll ret = 0;
    if(L <= mid)
        ret += query(L,R,l,mid,rt <<1);
    if(R >= mid + 1)
        ret += query(L,R,mid + 1,r,rt <<1|1);
    return ret;
}
void test(int l,int r,int rt)
{
    if(l == 1 && r == 2)
    {
        cout << tree[rt] << endl;return ;
    }
    if(l == r)return ;
    int mid = (l + r) >>1;
    test(l,mid,rt << 1);test(mid + 1,r,rt <<1|1);
}
int main()
{
    int n,m;
    while( ~ scanf("%d%d",&n,&m))
    {
        build(1,n,1);
        while(m --)
        {
            int type;scanf("%d",&type);
            if(type == 1)
            {
                int x,y,z;scanf("%d%d%d",&x,&y,&z);
                update(x,y,z,1,n,1);
            }
            else
            {
                int x,y;scanf("%d%d",&x,&y);
                printf("%lld\n",query(x,y,1,n,1));
            }
//            for(int i = 1;i <= n;i ++)
//                cout << i << " - > " << query(i,i,1,n,1) << endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;
#define clr(x,y) memset(x,y,sizeof x)
#define INF 0x3f3f3f3f
const ll Mod = 1e9 + 7;

ll sum[maxn << 2],add[maxn << 2];
int tree[maxn << 2];

ll Abs(ll x){return x > 0 ? x : -x;}
void build(int l,int r,int rt)
{
    tree[rt] = -1;sum[rt] = 0;add[rt] = 0;
    if(l == r)
    {
        tree[rt] = l;return;
    }
    int mid = (l + r) >> 1;
    build(l,mid,rt << 1);build(mid + 1,r,rt << 1|1);
}

void push_down(int l,int r,int rt)
{
    if(tree[rt] != -1)
    {
        int mid = (l + r) >> 1;
        add[rt << 1] += add[rt];add[rt << 1|1] += add[rt];
        sum[rt << 1] += add[rt] * (mid - l + 1);
        sum[rt << 1|1] += add[rt] * (r - mid);
        add[rt] = 0;
        tree[rt << 1] = tree[rt << 1|1] = tree[rt];
        tree[rt] = -1;
    }
}
void push_up(int rt)
{
    if(tree[rt << 1] == tree[rt << 1|1])
        tree[rt] = tree[rt << 1];
    else tree[rt] = -1;
    sum[rt] = sum[rt << 1] + sum[rt << 1|1];
}
void update(int L,int R,int x,int l,int r,int rt)
{
    if(tree[rt] == x)return;
    if(L <= l && R >= r)
    {
        if(tree[rt] != -1)
        {
            sum[rt] += 1ll * Abs(tree[rt] - x) * (r - l + 1);
            add[rt] += Abs(x - tree[rt]);
            tree[rt] = x;
            return;
        }
    }
    push_down(l,r,rt);
    int mid = (l + r) >> 1;
    if(L <= mid)
        update(L,R,x,l,mid,rt << 1);
    if(R >= mid + 1)
        update(L,R,x,mid + 1,r,rt << 1|1);
    push_up(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
    if(L <= l && R >= r)
    {
        return sum[rt];
    }
    push_down(l,r,rt);
    int mid = (l + r) >> 1;
    ll ret = 0;
    if(L <= mid)
        ret += query(L,R,l,mid,rt <<1);
    if(R >= mid + 1)
        ret += query(L,R,mid + 1,r,rt <<1|1);
    return ret;
}

int main()
{
    int n,m;
    while( ~ scanf("%d%d",&n,&m))
    {
        build(1,n,1);
        while(m --)
        {
            int type;scanf("%d",&type);
            if(type == 1)
            {
                int x,y,z;scanf("%d%d%d",&x,&y,&z);
                update(x,y,z,1,n,1);
            }
            else
            {
                int x,y;scanf("%d%d",&x,&y);
                printf("%lld\n",query(x,y,1,n,1));
            }
        }
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define maxn 100005
#define lson num<<1,s,mid
#define rson num<<1|1,mid+1,e

using namespace std;
typedef long long LL;
LL color[maxn<<2];
LL sum[maxn<<2];
LL val[maxn<<2];

LL Abs(LL x)
{
    return x>0?x:-x;
}

void pushdown(int num,int s,int e)
{
    if(color[num]!=-1)
    {
        int mid=(s+e)>>1;
        sum[num<<1]+=val[num]*(mid-s+1);
        sum[num<<1|1]+=val[num]*(e-mid);
        val[num<<1]+=val[num];
        val[num<<1|1]+=val[num];
        color[num<<1]=color[num<<1|1]=color[num];
        color[num]=-1;
        val[num]=0;
    }
}
void pushup(int num)
{
    if(color[num<<1]==color[num<<1|1])
        color[num]=color[num<<1];
    else color[num]=-1;
    sum[num]=sum[num<<1]+sum[num<<1|1];
}
void build(int num,int s,int e)
{
    sum[num]=0;
    val[num]=0;
    color[num]=-1;
    if(s==e)
    {
        color[num]=s;
        return;
    }
    int mid=(s+e)>>1;
    build(lson);
    build(rson);
}
void update(int num,int s,int e,int l,int r,LL v)
{
    if(l<=s && r>=e)
    {
        if(color[num]!=-1)
        {
            sum[num]+=(LL)Abs(v-color[num])*(e-s+1);
            val[num]+=Abs(color[num]-v);
            color[num]=v;
            return;
        }
    }
    int mid=(s+e)>>1;
    pushdown(num,s,e);
    if(l<=mid)update(lson,l,r,v);
    if(r>mid)update(rson,l,r,v);
    pushup(num);
}
LL query(int num,int s,int e,int l,int r)
{
    if(l<=s && r>=e)
    {
        return sum[num];
    }
    int mid=(s+e)>>1;
    pushdown(num,s,e);
    if(r<=mid)return query(lson,l,r);
    else if(l>mid)return query(rson,l,r);
    else return query(lson,l,mid)+query(rson,mid+1,r);
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    build(1,1,n);

    while(m--)
    {
        int type;
        scanf("%d",&type);
        if(type==1){
            int l,r;
            LL x;
            scanf("%d%d%I64d",&l,&r,&x);
            update(1,1,n,l,r,x);
        }
        else
        {
            int l,r;
            scanf("%d%d",&l,&r);
            printf("%I64d\n",query(1,1,n,l,r));
        }
    }
    return 0;
}
/*
10 10
1 5 9 6
2 6 10
1 1 9 3
1 3 10 5
2 4 6
*/


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

typedef long long LL;
const LL MAXN = 1e5 + 17;

LL A[MAXN<<2], C[MAXN<<2], sum[MAXN<<2];

#define lson (rt<<1)
#define rson (rt<<1|1)

void Build(LL rt, LL l, LL r)
{
    if(l == r)
    {
        A[rt] = l;
        return;
    }
    LL m = (l + r) >> 1;
    Build(lson, l, m);
    Build(rson, m+1, r);
}

void Pushdown(LL rt, LL l, LL r, LL v)
{
    if(A[rt] || l == r)
    {
        C[rt] += abs(A[rt] - v);
        sum[rt] += (r - l + 1) * abs(A[rt] - v);
    }
    else
    {
        LL m = (l + r) >> 1;
        Pushdown(lson, l, m, v);
        Pushdown(rson, m+1, r, v);
        sum[rt] = sum[lson] + sum[rson] + C[rt] * (r - l + 1);
    }
    A[rt] = 0;
}

void Update(LL al, LL ar, LL v, LL rt, LL l, LL r)
{
    if(al == l && ar == r)
    {
        Pushdown(rt, al, ar, v);
        A[rt] = v;
        return;
    }
    if(A[rt])
    {
        A[lson] = A[rson] = A[rt];
        A[rt] = 0;
    }

    LL m = (l + r) >> 1;
    if(ar <= m)
        Update(al, ar, v, lson, l, m);
    else if(al > m)
        Update(al, ar, v, rson, m+1, r);
    else
        Update(al, m, v, lson, l, m), Update(m+1, ar, v, rson, m+1, r);

    sum[rt] = sum[lson] + sum[rson] + (r - l + 1) * C[rt];
}

LL Query(LL al, LL ar, LL rt, LL l, LL r)
{
    if(al == l && ar == r)
        return sum[rt];
    LL m = (l + r) >> 1;
    if(ar <= m)
        return Query(al, ar, lson, l, m);
    else if(al > m)
        return Query(al, ar, rson, m+1, r);
    else
        return Query(al, m, lson, l, m) + Query(m+1, ar, rson, m+1, r) + max(0LL, min(ar, r) - max(al, l) + 1LL) * C[rt];
}

int main()
{
    LL n, m;   cin >> n >> m;
    Build(1, 1, n);

    while(m--)
    {
        LL op;     cin >> op;
        if(op == 1)
        {
            LL l, r, x;        cin >> l >> r >> x;
            Update(l, r, x, 1, 1, n);
        }
        else
        {
            LL l, r;           cin >> l >> r;
            cout << Query(l, r, 1, 1, n) << "\n";
        }
    }
}


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

typedef long long LL;
const LL MAXN = 1e5 + 17;

LL A[MAXN<<2], C[MAXN<<2], sum[MAXN<<2];

#define lson (rt<<1)
#define rson (rt<<1|1)

void Build(LL rt, LL l, LL r)
{
    if(l == r)
    {
        A[rt] = l;
        return;
    }
    LL m = (l + r) >> 1;
    Build(lson, l, m);
    Build(rson, m+1, r);
}

void Pushdown(LL rt, LL l, LL r, LL v)
{
    if(A[rt])
    {
        C[rt] += abs(A[rt] - v);
        sum[rt] += (r - l + 1) * abs(A[rt] - v);
    }
    else
    {
        if(l == r)
            return;

        LL m = (l + r) >> 1;
        Pushdown(lson, l, m, v);
        Pushdown(rson, m+1, r, v);
        sum[rt] = sum[lson] + sum[rson] + C[rt] * (r - l + 1);
    }
    A[rt] = 0;
}

void Update(LL al, LL ar, LL v, LL rt, LL l, LL r)
{
    if(al == l && ar == r)
    {
        Pushdown(rt, al, ar, v);
        A[rt] = v;
        return;
    }
    if(A[rt])
    {
        A[lson] = A[rson] = A[rt];
        A[rt] = 0;
    }

    LL m = (l + r) >> 1;
    if(ar <= m)
        Update(al, ar, v, lson, l, m);
    else if(al > m)
        Update(al, ar, v, rson, m+1, r);
    else
        Update(al, m, v, lson, l, m), Update(m+1, ar, v, rson, m+1, r);

    sum[rt] = sum[lson] + sum[rson] + (r - l + 1) * C[rt];
}

LL Query(LL al, LL ar, LL rt, LL l, LL r)
{
    if(al == l && ar == r)
        return sum[rt];
    LL m = (l + r) >> 1;
    if(ar <= m)
        return Query(al, ar, lson, l, m);
    else if(al > m)
        return Query(al, ar, rson, m+1, r);
    else
        return Query(al, m, lson, l, m) + Query(m+1, ar, rson, m+1, r) + max(0LL, min(ar, r) - max(al, l) + 1LL) * C[rt];
}

int main()
{
    LL n, m;   cin >> n >> m;
    Build(1, 1, n);

    while(m--)
    {
        LL op;     cin >> op;
        if(op == 1)
        {
            LL l, r, x;        cin >> l >> r >> x;
            Update(l, r, x, 1, 1, n);
        }
        else
        {
            LL l, r;           cin >> l >> r;
            cout << Query(l, r, 1, 1, n) << "\n";
        }
    }
}


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

typedef long long LL;
const LL MAXN = 1e5 + 17;

LL sum[MAXN<<2], C[MAXN<<2], tag[MAXN<<2];

#define lson (rt<<1)
#define rson (rt<<1|1)

void Pushup(LL rt, LL l, LL r)
{
    sum[rt] = sum[lson] + sum[rson];
    //tag[rt] = 0;
    if(C[lson] == C[rson])
        C[rt] = C[lson];
    else
        C[rt] = -1;
}

void Pushdown(LL rt, LL l, LL r)
{
    LL m = (l + r) >> 1;
    LL lsiz = m - l + 1, rsiz = r - m;

    if(tag[rt])
    {
        tag[lson] += tag[rt];
        sum[lson] += lsiz * tag[rt];
        tag[rson] += tag[rt];
        sum[rson] += rsiz * tag[rt];
        tag[rt] = 0;
    }
    if(C[rt] != -1)
    {
        C[lson] = C[rson] = C[rt];
        //C[rt] = -1;
    }
}

void Build(LL rt, LL l, LL r)
{
    if(l == r)
    {
        C[rt] = l;
        return;
    }

    LL m = (l + r) >> 1;
    Build(lson, l, m);
    Build(rson, m+1, r);
    Pushup(rt, l, r);
}

void Update(LL al, LL ar, LL v, LL rt, LL l, LL r)
{
    if(al == l && ar == r && C[rt] != -1)
    {
        tag[rt] += abs(v - C[rt]);
        sum[rt] += (r - l + 1) * abs(v - C[rt]);
        C[rt] = v;
        return;
    }

    Pushdown(rt, l, r);
    LL m = (l + r) >> 1;
    if(ar <= m)
        Update(al, ar, v, lson, l, m);
    else if(al > m)
        Update(al, ar, v, rson, m+1, r);
    else
        Update(al, m, v, lson, l, m), Update(m+1, ar, v, rson, m+1, r);
    Pushup(rt, l, r);
}

LL Query(LL al, LL ar, LL rt, LL l, LL r)
{
    if(al == l && ar == r)
        return sum[rt];

    Pushdown(rt, l, r);
    LL m = (l + r) >> 1;
    if(ar <= m)
        return Query(al, ar, lson, l, m);
    else if(al > m)
        return Query(al, ar, rson, m+1, r);
    else
        return Query(al, m, lson, l, m) + Query(m+1, ar, rson, m+1, r);
}

int main()
{
    LL n, m;   cin >> n >> m;
    Build(1, 1, n);

    while(m--)
    {
        LL op, l, r;   cin >> op >> l >> r;
        if(op == 1)
        {
            LL x;      cin >> x;
            Update(l, r, x, 1, 1, n);
        }
        else
            cout << Query(l, r, 1, 1, n) << "\n";
    }
}


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

const int MAXN = 1e5 + 17;

int sum[MAXN<<2], C[MAXN<<2], tag[MAXN<<2];

#define lson (rt<<1)
#define rson (rt<<1|1)

void Pushup(int rt, int l, int r)
{
    sum[rt] = sum[lson] + sum[rson];
    //tag[rt] = 0;
    if(C[lson] == C[rson])
        C[rt] = C[lson];
    else
        C[rt] = -1;
}

void Pushdown(int rt, int l, int r)
{
    int m = (l + r) >> 1;
    int lsiz = m - l + 1, rsiz = r - m;

    if(tag[rt])
    {
        tag[lson] += tag[rt];
        sum[lson] += lsiz * tag[rt];
        tag[rson] += tag[rt];
        sum[rson] += rsiz * tag[rt];
        tag[rt] = 0;
    }
    if(C[rt] != -1)
    {
        C[lson] = C[rson] = C[rt];
        //C[rt] = -1;
    }
}

void Build(int rt, int l, int r)
{
    if(l == r)
    {
        C[rt] = l;
        return;
    }

    int m = (l + r) >> 1;
    Build(lson, l, m);
    Build(rson, m+1, r);
    Pushup(rt, l, r);
}

void Update(int al, int ar, int v, int rt, int l, int r)
{
    if(al == l && ar == r && C[rt] != -1)
    {
        tag[rt] += abs(v - C[rt]);
        sum[rt] += (r - l + 1) * abs(v - C[rt]);
        C[rt] = v;
        return;
    }

    Pushdown(rt, l, r);
    int m = (l + r) >> 1;
    if(ar <= m)
        Update(al, ar, v, lson, l, m);
    else if(al > m)
        Update(al, ar, v, rson, m+1, r);
    else
        Update(al, m, v, lson, l, m), Update(m+1, ar, v, rson, m+1, r);
    Pushup(rt, l, r);
}

int Query(int al, int ar, int rt, int l, int r)
{
    if(al == l && ar == r)
        return sum[rt];

    Pushdown(rt, l, r);
    int m = (l + r) >> 1;
    if(ar <= m)
        return Query(al, ar, lson, l, m);
    else if(al > m)
        return Query(al, ar, rson, m+1, r);
    else
        return Query(al, m, lson, l, m) + Query(m+1, ar, rson, m+1, r);
}

int main()
{
    int n, m;   cin >> n >> m;
    Build(1, 1, n);

    while(m--)
    {
        int op, l, r;   cin >> op >> l >> r;
        if(op == 1)
        {
            int x;      cin >> x;
            Update(l, r, x, 1, 1, n);
        }
        else
            cout << Query(l, r, 1, 1, n) << "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define X first
#define Y second
#define FOR(i,begin,end) for (int i=begin;i<=end;i++)
#define rep(i,t) for (int i=0;i<t;i++)
#define pii pair<LL,LL>
#define fast cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
typedef long long LL;
const int maxN=1e5+7;
LL data[maxN*4],helper[4*maxN],n,m;
set<pair<pii, int> > s;
vector<set<pair<pii, int> >::iterator> vec;
vector<pair<pair<int, int>, int> > add;
inline int left(int x){return 2*x;}
inline int right(int x){return 2*x+1;}
void shift(int st, int en, int node)
{
  int mid=(st+en)>>1;
  helper[left(node)]+=helper[node];
  helper[right(node)]+=helper[node];
  data[left(node)]+=helper[node]*(mid-st);
  data[right(node)]+=helper[node]*(en-mid);
  helper[node]=0;
}
int get(int l, int r, int st= 0, int en=n, int node=1)
{
  if(en<=l || r<=st)
    return 0;
  if(l<=st && en<=r)
    return data[node];
  shift(st,en,node);
  int mid=(st+en)>>1;
  return get(l,r,st,mid,left(node))+get(l,r,mid,en,right(node));
}
void upd(int l, int r, int val, int st=0, int en=n, int node=1)
{
  if(en<=l || r<=st)
      return;
  if(l<=st && en<=r)
  {
    data[node]+=(en-st)*val;
    helper[node]+=val;
    return;
  }
  shift(st,en,node);
  int mid=(st+en)>>1;
  upd(l,r,val,st,mid,left(node));
  upd(l,r,val,mid,en,right(node));
  data[node]=data[left(node)]+data[right(node)];
}
main()
{
  fast;
  cin>>n>>m;
  rep(i,n)
    s.insert({{i,i+1},i+1});
  rep(t,m)
  {
    int type,l,r;
    cin>>type>>l>>r;
    l--;
    if(type==1)
    {
      int x;
      cin>>x;
      vec.resize(0);
      add.resize(0);
      set<pair<pii, int> >:: iterator it=s.lower_bound({{l,0},0});
      if(it==s.end() || it->X.X!=l)
      {
        it--;
        int f=it-> X. X,ind=it->Y,sec=it->X.Y;
        if(f<=l && r<=sec)
        {
          upd(l,r,abs(ind-x));
          s.erase(it);
          s.insert({{f,l},ind});
          s.insert({{l,r},x});
          s.insert({{r,sec},ind});
          continue;
        }
        else
        {
          vec.pb(it);
          add.pb({{f,l},ind});
          upd(l,sec,abs(ind-x));
          it++;
        }
      }
      while(it!=s.end() && it->X.X<r && it->X.Y<=r)
      {
        int f=it->X.X,ind=it->Y,sec=it->X.Y;
        vec.pb(it);
        upd(f,sec,abs(ind-x));
        it++;
      }
      if(it!=s.end() && it->X.X<r)
      {
        int f=it ->X.X,ind=it->Y,sec=it->X.Y;
        vec.pb(it);
        add.pb({{r,sec},ind});
        upd(f,r,abs(ind-x));
      }
      s.insert({{l,r},x});
      rep(i,vec.size())
        s.erase(vec[i]);
      rep(i,add.size())
        s.insert(add[i]);
    }
    else
      cout<<get(l,r)<<'\n';
  }
  return cout<<"\n",0;
}


#include <bits/stdc++.h>
using namespace std;
#define pf push_front
#define pb push_back
#define FOR(i,begin,end) for (int i=begin;i<=end;i++)
#define rep(i,t) for (int i=0;i<t;i++)
#define pii pair<LL,LL>
#define pi pair<int,pii>
#define X first
#define Y second
#define fast cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
typedef long long LL;
typedef unsigned long long int ulli;
const LL maxN=1e5+7,MOD=1e9+7,INF=1e6+7,N=5000+7;
int n,m,l,r,x,help,pointer,type;
LL data[4*maxN],helper[4*maxN],dp[maxN],sum[maxN],col[maxN];
set<int>q;
vector<int>v;
inline LL left(LL x){return 2*x+1;}
inline LL right(LL x){return 2*x+2;}
void shift(int node, int st, int en)
{
  if(en-st<2 || !helper[node])
    return ;
  LL mid=(st+en)>>1;
  data[left(node)]+=(LL)(mid-st)*helper[node];
  data[right(node)]+=(LL)(en-mid)*helper[node];
  helper[left(node)]+=helper[node];
  helper[right(node)]+=helper[node];
  helper[node]=0;
}
void add(int node, int st, int en, int l, int r, int val)
{
  if(en<=l || r<=st)
    return ;
  if(st>=l && en<=r)
  {
    data[node]+=(en-st)*val;
    helper[node]+=val;
    return ;
  }
  shift(node,st,en);
  int mid=(st+en)>>1;
  add(left(node),st,mid,l,r,val);
  add(right(node),mid,en,l,r,val);
  data[node]=data[left(node)]+data[right(node)];
}
LL get(int node, int st, int en, int l, int r)
{
  if(en<=l || r<=st)
    return 0;
  if(st>=l && en<=r)
    return data[node];
  shift(node,st,en);
  int mid=(st+en)>>1;
  return get(left(node),st,mid,l,r)
        +get(right(node),mid,en,l,r);
}
main()
{
  set<int>::iterator itup,itlow;
  scanf("%d%d",&n,&m);
  rep(i,n)
    dp[i]=i+1,q.insert(i),col[i]=i+1;
  int done=0;
  while(m--)
  {
    scanf("%d",&type);
    if(type==1)
    {
      scanf("%d%d%d",&l,&r,&x);
      l--;
      r--;
      FOR(i,l,r)
        sum[i]+=abs(col[i]-x),col[i]=x;
      v.clear();
      itlow=q.lower_bound(l);
      if(*itlow!=l && itlow!=q.begin())
        itlow--;
      for(itlow;itlow!=q.end();itlow++)
      {
        LL fn,beg=*itlow;
        itup=q.end();
        itup--;
        if(*itlow!=*itup)
        {
          itup=itlow;
          itup++;
          fn=*itup;
          fn--;
        }
        else
          fn=n-1;
        if(beg>r)
          break;
        if(beg<l)
        {
          if(fn<r)
            add(0,0,n,l,fn+1,abs(dp[beg]-x)),v.pb(fn+1);
          if(fn==r)
          {
            add(0,0,n,l,r+1,abs(dp[beg]-x));
            dp[r+1]=dp[beg];
          }
          if(fn>r)
          {
            add(0,0,n,l,r+1,abs(dp[beg]-x));
            dp[r+1]=dp[beg];
          }
        }
        else
        {
          if(beg==l)
          {
            if(fn<r)
              add(0,0,n,l,fn+1,abs(dp[beg]-x)),v.pb(fn+1);
            if(fn==r)
              add(0,0,n,l,fn+1,abs(dp[beg]-x));
            if(fn>r)
              add(0,0,n,l,r+1,abs(dp[beg]-x)),dp[r+1]=dp[beg];
          }
          if(beg>l)
          {
            if(fn<r)
              add(0,0,n,beg,fn+1,abs(dp[beg]-x)),v.pb(fn+1);
            if(fn==r)
              add(0,0,n,beg,fn+1,abs(dp[beg]-x));
            if(fn>r)
              add(0,0,n,beg,r+1,abs(dp[beg]-x)),dp[r+1]=dp[beg];
          }
        }
      }
      dp[l]=x;
      q.insert(l);
      if(r!=n-1)
        q.insert(r+1);
      rep(j,v.size())
        q.erase(v[j]);
    }
    else
    {
      scanf("%d%d",&l,&r);
      l--;
      cout<<get(0,0,n,l,r)<<'\n';
    }
  }
  return cout<<"\n",0;
}


#include<bits/stdc++.h>
#include<set>
#include<map>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
typedef long long int ll;
typedef unsigned long long int ull;

#define dbg printf("in\n")
#define nl printf("\n")
#define N 1000000
#define inf 1e18

#define fr(i,n)             for (ll i=0;i<n;i++)
#define fr1(i,n)            for(ll i=1;i<=n;i++)

#define sf(n) scanf("%d", &n)
#define sff(n,m) scanf("%d%d",&n,&m)
#define sfl(n) scanf("%I64d", &n)
#define sffl(n,m) scanf("%I64d%I64d",&n,&m)
#define dsort(a)            sort(a,a+n,greater<int>())
#define vasort(v)           sort(v.begin(), v.end());
#define vdsort(v)           sort(v.begin(), v.end(),greater<int>());
#define uniquee(x)          x.erase(unique(x.begin(), x.end()),x.end())
#define pf(n) printf("%d",n)
#define pff(n,m) printf("%d %d",n,m)
#define pffl(n,m) printf("%I64d %I64d",n,m)
#define pfl(n) printf("%I64d",n)
#define pfs(s) printf("%s",s)
#define csl ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define pp pair<char, int>

    using namespace std;

    int main(){
          ios_base::sync_with_stdio(false);
          cin.tie(NULL);
          int n,a,b;
          cin >> n >> a >> b;
          vector<int> chores;
          int u;
          for(int i = 0 ;i < n; i++){
              cin >> u;
              chores.pb(u);
          }
          sort(chores.begin(),chores.end());
        //   if(chores[b]-chores[b-1]){
              cout <<  chores[b]-chores[b-1] << endl;
        //   }
        //   else{
            //   cout << 0;
        //   }/
        return 0;
    }


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n,c,d;
	cin>>n>>c>>d;
	int a[200000+10];
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	//cout<<a[c]<<" "<<a[d]<<" ";
	cout<<a[d]-a[c];
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n,c,d;
	cin>>n>>c>>d;
	int a[200000+10];
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	//cout<<a[c]<<" "<<a[d]<<" ";
	if(n%2==0)
		cout<<a[d]-a[c-1];
	else
		cout<<a[d]-a[c];
}


#include<iostream>
#include<algorithm>
using namespace std;
int a[20000];
int main(){
	int n,c,b;
	cin>>n>>c>>b;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	cout<<a[b]-a[b-1];
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,a,b;
	cin>>n>>a>>b;
	int arr[n];
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	sort(arr,arr+n);
	cout<<arr[a+1]-arr[a];
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,a,b;
	cin>>n>>a>>b;
	int arr[n];
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	sort(arr,arr+n);
	cout<<arr[b]-arr[b-1];
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,a,b;
	cin>>n>>a>>b;
	int arr[n];
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	sort(arr,arr+n);
	if(a<n-1)
		cout<<arr[a+1]-arr[a];
	else if(a==n-1)
		cout<<arr[n-1]-arr[n-2];
	else
		cout<<2000-arr[n-1];
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,a,b;
	cin>>n>>a>>b;
	int arr[n+1];
	arr[n]=2000;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	sort(arr,arr+n);
	cout<<arr[a+1]-arr[a];
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int n,a,b,i,v[2010];
int main()
{
	cin>>n>>a>>b;
	for(i=1;i<=n;i++)
	{
		cin>>v[i];
	}
	sort(v+1,v+1+n);
	if(n>2)
		if(v[b+1]<v[b+2])
			cout<<v[b+1]-v[b];
		else cout<<0;
	else cout<<v[2]-v[1];
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int n,a,b,i,v[2010];
int main()
{
	cin>>n>>a>>b;
	for(i=1;i<=n;i++)
	{
		cin>>v[i];
	}
	sort(v+1,v+1+n);
	if(v[b+1]<v[b+2])
		cout<<v[b+1]-v[b];
	else cout<<0;
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int n,a,b,i,v[2010];
int main()
{
	cin>>n>>a>>b;
	for(i=1;i<=n;i++)
	{
		cin>>v[i];
	}
	sort(v+1,v+1+n);
	cout<<v[b+1]-v[b];
	return 0;
}


#include <iostream>
#include <algorithm>
#define endl "\n"

using namespace std;
int A[100000];
//int B[100000];
bool ok(int a[],int s,int v,int x,int y)
{
     int small=0,larg=0;
     for(int i=0;i<s;i++)
      {
          if(a[i]>v)
          larg++;
          else
            small++;

      }

      if(larg==x&&small==y)
        return 1;
      return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,a,b,c=0;
    cin>>n>>a>>b;
    for(int i=0;i<n;i++)
        cin>>A[i];

    for(int i=1;i<=1000000;i++)
    {
        if(ok(A,n,i,a,b))
            c++;
    }
    cout<<c<<endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#define endl "\n"

using namespace std;
int A[100000];
//int B[100000];
bool ok(int a[],int s,int v,int x,int y)
{
     int small=0,larg=0;
     for(int i=0;i<s;i++)
      {
          if(a[i]>v)
          larg++;
          else
            small++;

      }

      if(larg==x&&small==y)
        return 1;
      return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,a,b,c=0;
    cin>>n>>a>>b;
    for(int i=0;i<n;i++)
        cin>>A[i];
    int mx=*max_element(A,A+n);

    for(int i=1;i<=mx;i++)
    {
        if(ok(A,n,i,a,b))
            c++;
    }
    cout<<c<<endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#define endl "\n"

using namespace std;
int A[100000];
//int B[100000];
bool ok(int a[],int s,int v,int x,int y)
{
     int small=0,larg=0;
     for(int i=0;i<s;i++)
      {
          if(a[i]>v)
          larg++;
          else
            small++;

      }

      if(larg==x&&small==y)
        return 1;
      return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,a,b,c=0;
    cin>>n>>a>>b;
    for(int i=0;i<n;i++)
        cin>>A[i];

    for(int i=1;i<=100000000;i++)
    {
        if(ok(A,n,i,a,b))
            c++;
    }
    cout<<c<<endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#define endl "\n"

using namespace std;
int A[100000];
//int B[100000];
bool ok(int a[],int s,int v,int x,int y)
{
     int small=0,larg=0;
     for(int i=0;i<s;i++)
      {
          if(a[i]>v)
          larg++;
          else
            small++;

      }

      if(larg==x&&small==y)
        return 1;
      return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,a,b,c=0;
    cin>>n>>a>>b;
    for(int i=0;i<n;i++)
        cin>>A[i];

    for(int i=1;i<=1000000000;i++)
    {
        if(ok(A,n,i,a,b))
            c++;
    }
    cout<<c<<endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#define endl "\n"

using namespace std;
int A[100000];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,a,b,c=0;
    cin>>n>>a>>b;
    for(int i=0;i<n;i++)
        cin>>A[i];

    a--;b--;
    sort(A,A+n);

    cout<<A[b+1]-A[b];

    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	//freopen("INPUT.TXT","rt",stdin);
	long long n,x,y,a[13],dem=0;
	cin >> n>>x>>y;
	for (long long i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	sort(a, a + n);
	cout << a[y]-a[y-1];
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	//freopen("INPUT.TXT","rt",stdin);
	int n,x,y,a[13],dem=0;
	cin >> n>>x>>y;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	sort(a, a + n);
	while (a[y - 1] < a[y])
	{
		dem++;
		a[y - 1]++;
	}
	cout << dem;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	//freopen("INPUT.TXT","rt",stdin);
	long long n,x,y,a[13],dem=0;
	cin >> n>>x>>y;
	for (long long i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	sort(a, a + n);
	while (a[y - 1] < a[y])
	{
		dem++;
		a[y - 1]++;
	}
	cout << dem;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	//freopen("INPUT.TXT","rt",stdin);
	int n,x,y,a[2007];
	cin >> n>>x>>y;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	sort(a, a + n);
	cout << a[y]-a[y-1];
	return 0;
}


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

int main() {

  int n, a, b;
  cin >> n >> a >> b;

  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

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

  cout << abs(h[a] - h[b]) << endl;

}


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

int main() {

  int n, a, b;
  cin >> n >> a >> b;

  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

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

  if (a < b) {
    cout << h[a+1] - h[a] << endl;
  } else {
    cout << h[b+1] - h[b] << endl;
  }

}


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

int main() {

  int n, a, b;
  cin >> n >> a >> b;

  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

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

  cout << h[b] - h[b - 1] << endl;

}


#include <iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
	int n,a,b,i;
	cin>>n>>a>>b;
	int arr[n];
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	sort(arr,arr+n);

	if(arr[b-1] !=arr[b])
	{
		cout<<arr[b-1];
	}
	else
	{
		cout<<"0";
	}
	return 0;
}


#include <iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
	int n,a,b,i,count=0;
	cin>>n>>a>>b;
	int arr[n];
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
	}
	sort(arr,arr+n);

	if(arr[b-1] !=arr[b])
	{
	   for(i=arr[b-1];i<arr[b];i++)
	   {
	   	count++;
		   }
    cout<<count;
	}
	else
	{
		cout<<"0";
	}
	return 0;
}


/// Author: Mr.FarFai - Faridun Faiziev (TJK)

#include <bits/stdc++.h>
/**#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
*/

#define fi first
#define se second
#define LL long long
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define p_bm_p(a, b) push_back(std::make_pair(a, b))
#define sort(x) sort(x.begin(), x.end())
#define reverse(x) reverse(x.begin(), x.end())
#define for(i, n) for(i = 0; i < n; ++i)
#define FOR(i, a, n, b) for(i = a; i < n; i+=b)
/**
     _/ \_
     (^_^)                                __
       |        |    | |--|    |---|     |  | |---|     ___
     / | \      |\  /| |__|    |    /\   |__| |    /\    |
       |        | \/ | | \     |-| /__\  | \  |-| /__\   |
      / \       |    | |  \ () |  /    \ |  \ |  /    \ _|_
     /   \
*/
int main() {
	std::ios_base::sync_with_stdio(0);
	/// =========================================
	/// freopen(".txt", "r", stdin);
	/// freopen(".txt", "w", stdout);
	/// =========================================
	int n, a, b, i;
	std::cin >> n >> a >> b;
	std::vector <int> c(n);
	for(i, n){
        std::cin >> c[i];
	}
	std::sort(c);
	std::cout << c[b] - c[a] << "\n";
	return 0;
}


/// Author: Mr.FarFai - Faridun Faiziev (TJK)

#include <bits/stdc++.h>
/**#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
*/

#define fi first
#define se second
#define LL long long
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define p_bm_p(a, b) push_back(std::make_pair(a, b))
#define sort(x) sort(x.begin(), x.end())
#define reverse(x) reverse(x.begin(), x.end())
#define for(i, n) for(i = 0; i < n; ++i)
#define FOR(i, a, n, b) for(i = a; i < n; i+=b)
/**
     _/ \_
     (^_^)                                __
       |        |    | |--|    |---|     |  | |---|     ___
     / | \      |\  /| |__|    |    /\   |__| |    /\    |
       |        | \/ | | \     |-| /__\  | \  |-| /__\   |
      / \       |    | |  \ () |  /    \ |  \ |  /    \ _|_
     /   \
*/
int main() {
	std::ios_base::sync_with_stdio(0);
	/// =========================================
	/// freopen(".txt", "r", stdin);
	/// freopen(".txt", "w", stdout);
	/// =========================================
	int n, a, b, i;
	std::cin >> n >> a >> b;
	std::vector <int> c(n);
	for(i, n){
        std::cin >> c[i];
	}
	std::sort(c);
	std::cout << abs(c[n - a - 1] - c[b]) << "\n";
	return 0;
}


#include <bits/stdc++.h>
#define lli long long int
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lli a,b,n;
    cin>>n>>a>>b;
    lli h[n];
    for(lli i =0;i<n;i++)
            cin>>h[i];
    sort(h,h+n);
    //for(lli i =  0;i<b;i++)
    if(h[b-1] == h[b])
        cout<<0;
    else
        cout<<h[b-1];

    return 0;
}


#include <bits/stdc++.h>
#define lli long long int
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lli a,b,n;
    cin>>n>>a>>b;
    lli h[n];
    for(lli i =0;i<n;i++)
            cin>>h[i];
    sort(h,h+n);
    //for(lli i =  0;i<b;i++)

        cout<<h[b] - h[b-1];

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
	ll n,a,b,diff;
	cin>>n>>a>>b;
	vector<ll> v;
	ll arr[n];
	for(ll i=0;i<n;i++)
	{
		cin>>arr[i];
		v.push_back(arr[i]);
	}
	sort(v.begin(),v.end());
	diff=v[n-a]-v[b-1];
	//cout<<diff<<endl;
	if(diff<0)
	{
		cout<<"0"<<endl;
	}
	else
	{
		cout<<diff<<endl;
	}
}
// 2 1 1

// 10 2

// Participant's output

// 75232380921103

// Jury's answer

// 8


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
	ll n,a,b,diff;
	cin>>n>>a>>b;
	vector<ll> v;
	ll arr[n];
	for(ll i=0;i<n;i++)
	{
		cin>>arr[i];
		v.push_back(arr[i]);
	}
	sort(v.begin(),v.end());
	diff=v[a+1]-v[b-1];
	if(diff<0)
	{
		cout<<"0"<<endl;
	}
	else
	{
		cout<<diff<<endl;
	}
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,a,b,g,s;
    cin>>n>>a>>b;
    int arr[n];
    for(int i=0;i<n;i++)
    cin>>arr[i];
    sort(arr,arr+n);
    if(a>b)
        g=a;
        else
        g=b;
        if(a<b)
        s=a;
        else
        s=b;
    int d=arr[g-1]-arr[s-1];
    cout<<d;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b;
    cin>>n>>a>>b;
    int arr[n];
    for(int i=0;i<n;i++)
    cin>>arr[i];
    sort(arr,arr+n,greater<int>());
    int d=arr[a-1]-arr[a];
    cout<<d;
}


#include<iostream>
using namespace std;
int main()
{
    int n,a,b,g,s;
    cin>>n>>a>>b;
    int arr[n];
    for(int i=0;i<n;i++)
    cin>>arr[i];
    if(a>b)
        g=a;
        else
        g=b;
        if(a<b)
        s=a;
        else
        s=b;
    int d=arr[g-1]-arr[s-1];
    cout<<d;
}


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

int main ()
{
	long n ,a ,b ,x;
	cin >> n >> a >> b ;
	vector <int> v ;
	for (int i = 0 ; i < n ;i++)
	{
		cin >> x ;
		v.push_back(x) ;
	}
	sort(v.begin() ,v.end()) ;
	if (v[b] != v[b + 1])
	{
		cout << b ;
	}
	else
	{
		cout << 0 ;
	}
	return 0 ;
}


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

int main ()
{
	long n ,a ,b ,x;
	cin >> n >> a >> b ;
	vector <int> v ;
	for (int i = 0 ; i < n ;i++)
	{
		cin >> x ;
		v.push_back(x) ;
	}
	sort(v.begin() ,v.end()) ;
	if (v[b - 1] != v[b])
	{
		cout << v[b - 1] ;
	}
	else
	{
		cout << 0 ;
	}
	return 0 ;
}


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

int main ()
{
	long n ,a ,b ,x;
	cin >> n >> a >> b ;
	vector <int> v ;
	for (int i = 0 ; i < n ;i++)
	{
		cin >> x ;
		v.push_back(x) ;
	}
	sort(v.begin() ,v.end()) ;
	if (v[b] != v[b + 1])
	{
		cout << v[b] ;
	}
	else
	{
		cout << 0 ;
	}
	return 0 ;
}


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

int main ()
{
	long n ,a ,b ,x;
	cin >> n >> a >> b ;
	vector <int> v ;
	for (int i = 0 ; i < n ;i++)
	{
		cin >> x ;
		v.push_back(x) ;
	}
	sort(v.begin() ,v.end()) ;
	cout << v[b] - v[b - 1] ;
	return 0 ;
}


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

int main()
{
    //freopen("in.txt", "r", stdin);
    int n, a, b, res, in, vas;
    //set<int> ss;
    vector<int> vec;

    cin >> n >> a >> b;

    for(int i=0; i<n; i++) {
        cin >> in;
        vec.push_back(in);
        //ss.insert(in);
    }

    sort(vec.begin(), vec.end());
    //vector<int>::iterator it = vec.begin();
    //it = next(it, a-1);
    cout << vec[b]-vec[b-1] << endl;

    return 0;
}


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

int main()
{
    //freopen("in.txt", "r", stdin);
    int n, a, b, res, in, vas;
    //set<int> ss;
    vector<int> vec;

    cin >> n >> a >> b;

    for(int i=0; i<n; i++) {
        cin >> in;
        vec.push_back(in);
        //ss.insert(in);
    }

    sort(vec.begin(), vec.end());
    //vector<int>::iterator it = vec.begin();
    //it = next(it, a-1);
    vas = vec[b-1];
    if(vec[b] > vas) {
        cout << vas << endl;
    }
    else
        cout << 0 << endl;

    return 0;
}


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

int c[2001];
int main(){

  int n, a, b;
  scanf("%d %d %d", &n, &a, &b);

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

  int ans = c[b]-c[a];
  printf("%d\n", ans);
  return 0;
}


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

int c[2001];
int main(){

  int n, a, b;
  scanf("%d %d %d", &n, &a, &b);

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

  int ans = c[a+1]-c[a];
  printf("%d\n", ans);
  return 0;
}


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

int c[2001];
int main(){

  int n, a, b;
  scanf("%d %d %d", &n, &a, &b);

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

  int ans = c[b+1]-c[b];
  printf("%d\n", ans);
  return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
  int n,a,b;
  cin >> n >> a >> b;int arr[n];
  for(int i=0;i<n;i++){cin >> arr[i];}
  sort(arr,arr+n);
  cout << arr[b]-arr[b-1]<<endl;

}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
  int n,a,b;
  cin >> n >> a >> b;int arr[n];
  for(int i=0;i<n;i++){cin >> arr[i];}
  sort(arr,arr+n);
  cout << arr[b] << " " << arr[b-1] << endl;
  cout << arr[b]-arr[b-1]<<endl;

}


#include <bits/stdc++.h>
#define ll long long
#define str string
#define optimizator ios_base::sync_with_stdio(0);
using namespace std;
/*int main(){
	optimizator;
	int a,b,c;
	cin>>a>>b>>c;
	b-=c;
	if(a==0){
		if(b%c==0) cout<<"YES";
		else cout<<"NO";
		return 0;
	}
	if(b%a==0 && a<=b) cout<<"YES";
	else if(b%a!=0 || a>b) cout<<"NO";
}*/
int main(){
	int n,m,k;
	cin>>n>>m>>k;
	int a[n];
	for(int i=0; i<n; i++){
		cin>>a[i];
	}
	sort(a,a+n);
	int mi=999999,ma=-9999;
	for(int i=n-1; i>=k; i--){
		mi=min(a[i],mi);
	}
	for(int i=0; i<k; i++){
		ma=max(a[i],ma);
	}
	cout<<mi-ma;
}
/*int main(){
	int n;
	cin>>n;
	str a;
	cin>>a;
	vector<int> d(0,10);
	int posl=0, posp=9;
	for(int i=0; i<n; i++){
		if(a[i]=='R'){
			d[posp]=1;
			posp--;
		}
		else if(a[i]=='L'){
			d[posl]=1;
			posl++;
		}
		else{
			d[a[i]-'0']=0;
		}
	}
	for(int i=0; i<9; i++){
		cout<<d[i];
	}
}*/


#include <bits/stdc++.h>
#define ll long long
#define str string
#define pb push_back
#define optimizator ios_base::sync_with_stdio(0);
using namespace std;
int main(){
	optimizator;
	int n,m,k;
	cin>>n>>m>>k;
	int a[n];
	for(int i=0; i<n; i++){
		cin>>a[i];
	}
	sort(a,a+n);
		if(n==2){
			cout<<a[1]-a[0];
			return 0;
		}
	int cnt=0,cnt1=0,cnt2=0;
		cout<<abs(a[m]-a[k]);
}


#include <bits/stdc++.h>
#define ll long long
#define str string
#define optimizator ios_base::sync_with_stdio(0);
using namespace std;
/*int main(){
	optimizator;
	int a,b,c;
	cin>>a>>b>>c;
	b-=c;
	if(a==0){
		if(b%c==0) cout<<"YES";
		else cout<<"NO";
		return 0;
	}
	if(b%a==0 && a<=b) cout<<"YES";
	else if(b%a!=0 || a>b) cout<<"NO";
}*/
int main(){
	int n,m,k;
	cin>>n>>m>>k;
	int a[n];
	for(int i=0; i<n; i++){
		cin>>a[i];
	}
	sort(a,a+n);
	ll mi=9999999999,ma=-9999;
	for(int i=n-1; i>=k; i--){
		if(mi>a[i]){
			mi=a[i];
		}
	}
	for(int i=0; i<k; i++){
		if(ma<a[i]){
			ma=a[i];
		}
	}
	cout<<mi-ma;
}
/*int main(){
	int n;
	cin>>n;
	str a;
	cin>>a;
	vector<int> d(0,10);
	int posl=0, posp=9;
	for(int i=0; i<n; i++){
		if(a[i]=='R'){
			d[posp]=1;
			posp--;
		}
		else if(a[i]=='L'){
			d[posl]=1;
			posl++;
		}
		else{
			d[a[i]-'0']=0;
		}
	}
	for(int i=0; i<9; i++){
		cout<<d[i];
	}
}*/


#include <bits/stdc++.h>
#define ll long long
#define str string
#define pb push_back
#define optimizator ios_base::sync_with_stdio(0);
using namespace std;
int main(){
	optimizator;
	int n,m,k;
	cin>>n>>m>>k;
	int a[n];
	int mi=999999,ma=-999999;
	for(int i=0; i<n; i++){
		cin>>a[i];
		mi=min(a[i],mi);
		ma=max(a[i],ma);
	}
	int cnt=0,cnt1=0,cnt2=0;
	for(int i=mi; i<=ma; i++){
		cnt=0;
		cnt1=0;
		for(int j=0; j<n; j++){
			if(a[j]<=i) cnt++;
			else cnt1++;
		}
		if(cnt1==m && cnt==k){
			cnt2++;
		}
	}
	cout<<cnt2;
}


#include <bits/stdc++.h>
#define ll long long
#define str string
#define pb push_back
#define optimizator ios_base::sync_with_stdio(0);
using namespace std;
int main(){
	optimizator;
	int n,m,k;
	cin>>n>>m>>k;
	int a[n];
	int mi=99999999,ma=-999999;
	for(int i=0; i<n; i++){
		cin>>a[i];
		mi=min(a[i],mi);
		ma=max(a[i],ma);
	}
		if(n==2){
			cout<<ma-mi;
			return 0;
		}
	int cnt=0,cnt1=0,cnt2=0;
	for(int i=mi; i<ma; i++){
		cnt=0;
		cnt1=0;
		for(int j=0; j<n; j++){
			if(a[j]<=i) cnt++;
			else cnt1++;
		}
		if(cnt1==m && cnt==k){
			cnt2++;
		}
	}
	cout<<cnt2;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{

  int n,a,b;
  cin>>n>>a>>b;
  long int x,i;
  vector<long int > v;
  for(i=0;i<n;++i)
     {  cin>>x;
        v.push_back(x);
     }

  sort(v.begin(),v.end());
  v.erase(std::unique(v.begin(),v.end()),v.end());
  if(v.size()==a+b)
    {
        cout<<v[n-a]-v[a];
    }

  else
    cout<<"0";

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

int main()
{

  int n,a,b;
  cin>>n>>a>>b;
  long int x,i;
  vector<long int > v;
  for(i=0;i<n;++i)
     {  cin>>x;
        v.push_back(x);
     }

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

        cout<<v[b]-v[b-1];

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

int main()
{

  int n,a,b;
  cin>>n>>a>>b;
  long int x,i;
  vector<long int > v;
  for(i=0;i<n;++i)
     {  cin>>x;
        v.push_back(x);
     }

  sort(v.begin(),v.end());
  v.erase(std::unique(v.begin(),v.end()),v.end());
  if(v.size()==a+b)
    {
        cout<<v[b]-v[b-1];
    }

  else
    cout<<"0";

    return 0;

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>

using namespace std;

int main()
{
	int n, a, b;
	cin >> n >> a >> b;
	vector<int> h;
	int temp;
	for (int i = 0; i < n; i++)
	{
		cin >> temp;
		h.push_back(temp);
	}

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

	if (h[a] > h[a - 1])
		cout << h[a];
	else if (h[a] == h[a - 1])
		cout << 0;

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>

using namespace std;

int main()
{
	int n, a, b;
	cin >> n >> a >> b;
	vector<int> h;
	int temp;
	for (int i = 0; i < n; i++)
	{
		cin >> temp;
		h.push_back(temp);
	}

	sort(h.begin(), h.end(), greater<int>());

	if (h[a - 1] > h[a])
		cout << h[a];
	else
		cout << 0;

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>

using namespace std;

int main()
{
	int n, a, b;
	cin >> n >> a >> b;
	vector<int> h;
	int temp;
	for (int i = 0; i < n; i++)
	{
		cin >> temp;
		h.push_back(temp);
	}

	sort(h.begin(), h.end(), greater<int>());

	cout << h[a - 1] - h[a];

}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	int n, a, b;
	cin >> n >> a >> b;
	vector<int> h(n);

	for (int i = 0; i < n; i++) {
		cin >> h[i];
	}

	sort(h.begin(), h.end());
	cout << h[b] - h[b - 1];
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    int n, a, b, x;
    vector<int> v;

    cin >> n >> a >> b;
    for (int i = 0; i < n; i++) {
        cin >> x;
        v.push_back(x);
    }

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

    if (v[b - 1] != v[n - a]) {
        cout << v[b - 1];
    }
    else {
        cout << 0;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	int n, a, b, x;
	vector<int> v;

	cin >> n >> a >> b;
	for (int i = 0; i < n; i++) {
		cin >> x;
		v.push_back(x);
	}

	sort(v.begin(), v.end());
	cout << v[n - a] - v[b - 1];

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

int main() {
	int n, a, b;
	cin >> n >> a >> b;
	vector<int> h(n);

	for (int i = 0; i < n; i++) {
		cin >> h[i];
	}

	sort(h.begin(), h.end(), greater<int>());

	cout << h[a - 1] - h[a];

	return 0;
}


// In the name of god
#include <bits/stdc++.h>
using namespace std;
const int A=1e3*2+5;
int n,a,b,x[A],ans;
int main(){
	cin>>n>>a>>b;
	for(int i=0;i<n;i++){
		cin>>x[i];
	}
	sort(x,x+n);
	cout<<x[b]-x[b-1];
}


// In the name of god
#include <bits/stdc++.h>
using namespace std;
const int A=1e3*2+5;
int n,a,b,x[A],ans;
int main(){
	cin>>n>>a>>b;
	for(int i=0;i<n;i++){
		cin>>x[i];
	}
	sort(x,x+n);
	cout<<x[a+1]-x[a];
}


#include<stdio.h>
int main()
{
    long long n,a,b,i,j,dif,dif2,k,temp;
    long long h[3000];
    long long small;
    scanf("%lld%lld%lld",&n,&a,&b);
    for(i=0;i<n;i++)
    {
        scanf("%lld",&h[i]);
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(h[i]>h[j])
            {
                small=h[i];
                h[i]=h[j];
                h[j]=small;
            }
        }
    }
    dif=n-a;
    dif2=n-a-1;
    if(h[dif]==h[dif2])
    {
        printf("0\n");
    }
    else
    {
        temp=h[dif2];
        for(k=temp;k!=(h[dif]);k++)
            printf("%lld ",k);
    }
    return 0;
}


#include <cstdio>

#include <vector>

#include <algorithm>

int main(){

    long n, a, b; scanf("%ld %ld %ld\n", &n, &a, &b);

    std::vector<long> compVec(n, 0);

    for(int k = 0; k < n; k++){scanf("%ld ", &compVec[k]);}

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

    printf("%ld\n", compVec[b] - compVec[b - 1]);

    return 0;

}


#include<stdio.h>
int main()
{
    long long n,a,b,i,j,dif,dif2,k,temp;
    long long h[3000];
    long long small;
    scanf("%lld%lld%lld",&n,&a,&b);
    if(n>=2 && n<=2000){
    for(i=0;i<n;i++)
    {
        scanf("%lld",&h[i]);
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(h[i]>h[j])
            {
                small=h[i];
                h[i]=h[j];
                h[j]=small;
            }
        }
    }
    dif=n-a;
    dif2=n-a-1;
    if(h[dif]==h[dif2])
    {
        printf("0\n");
    }
    else
    {
        temp=h[dif2];
        for(k=temp;k!=(h[dif]);k++)
            printf("%lld ",k);
    }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    long long n,a,b,i,j,dif,dif2,k,temp;
    long long h[3000];
    long long small;
    scanf("%lld%lld%lld",&n,&a,&b);
    if(n>=2 && n<=2000){
    for(i=0;i<n;i++)
    {
        scanf("%lld",&h[i]);
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(h[i]>h[j])
            {
                small=h[i];
                h[i]=h[j];
                h[j]=small;
            }
        }
    }
    dif=n-a;
    dif2=n-a-1;
    if(h[dif]==h[dif2])
    {
        printf("0");
    }
    else
    {
        temp=h[dif2];
        for(k=temp;k!=(h[dif]);k++)
            printf("%lld ",k);
    }
    }
    printf("\n");
    return 0;
}


#include<stdio.h>
int main()
{
    long n,a,b,i,j,dif,dif2,k,temp;
    long h[2001];
    long small;
    scanf("%ld%ld%ld",&n,&a,&b);
    if(n>=2 && n<=2000){
    for(i=0;i<n;i++)
    {
        scanf("%ld",&h[i]);
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(h[i]>h[j])
            {
                small=h[i];
                h[i]=h[j];
                h[j]=small;
            }
        }
    }
    dif=n-a;
    dif2=n-a-1;
    if(h[dif]==h[dif2])
    {
        printf("0");
    }
    else
    {
        temp=h[dif2];
        for(k=temp;k!=(h[dif]);k++)
            printf("%ld ",k);
    }
    }
    printf("\n");
    return 0;
}


#include<stdio.h>
int main()
{
    int n,a,b,i,j,dif,dif2,k,temp;
    int h[3000];
    int small;
    scanf("%d%d%d",&n,&a,&b);
    for(i=0;i<n;i++)
    {
        scanf("%d",&h[i]);
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(h[i]>h[j])
            {
                small=h[i];
                h[i]=h[j];
                h[j]=small;
            }
        }
    }
    dif=n-a;
    dif2=n-a-1;
    if(h[dif]==h[dif2])
    {
        printf("0\n");
    }
    else
    {
        temp=h[dif2];
        for(k=temp;k!=(h[dif]);k++)
            printf("%d ",k);
    }
    return 0;
}


#include<stdio.h>
int main()
{
    long long n,a,b,i,j,dif,dif2,k,temp;
    long long h[3000];
    long long small;
    scanf("%lld%lld%lld",&n,&a,&b);
    if(n>=2 && n<=2000){
    for(i=0;i<n;i++)
    {
        scanf("%lld",&h[i]);
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(h[i]>h[j])
            {
                small=h[i];
                h[i]=h[j];
                h[j]=small;
            }
        }
    }
    dif=n-a;
    dif2=n-a-1;
    if(h[dif]==h[dif2])
    {
        printf("0\n");
    }
    else
    {
        temp=h[dif2];
        for(k=temp;k!=(h[dif]);k++)
            printf("%lld ",k);
    }
    }
    printf("\n");
    return 0;
}


#include<stdio.h>
int main()
{
    long n,a,b,i,j,dif,dif2,k,temp;
    long h[2000];
    long small;
    scanf("%ld%ld%ld",&n,&a,&b);
    if(n>=2 && n<=2000){
    for(i=0;i<n;i++)
    {
        scanf("%ld",&h[i]);
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(h[i]>h[j])
            {
                small=h[i];
                h[i]=h[j];
                h[j]=small;
            }
        }
    }
    dif=n-a;
    dif2=n-a-1;
    if(h[dif]==h[dif2])
    {
        printf("0");
    }
    else
    {
        temp=h[dif2];
        for(k=temp;k!=(h[dif]);k++)
            printf("%ld ",k);
    }
    }
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define f(i,n) for(int i=0;i<n;i++)
typedef long long int ll;
int main()
{
    int n,x,b;
    cin >> n >> x >> b;
    int a[n];
    f(i,n)cin >> a[i];
    sort(a,a+n);
    if(a[b-1]==a[b])
    cout << "0";
    else
    cout << a[b-1];
}


#include<bits/stdc++.h>
using namespace std;
#define f(i,n) for(int i=0;i<n;i++)
typedef long long int ll;
int main()
{
    int n,x,b;
    cin >> n >> x >> b;
    int a[n];
    f(i,n)cin >> a[i];
    sort(a,a+n);
    cout << a[b]-a[b-1];
}


#include<bits/stdc++.h>

using namespace std;

long i,s,r,t,a[10000],n,m,c,e,b[10000],mx,mn,mx1,mn1;
int main()
{
    cin>>n>>s>>t;
    for(i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    r=s;
    e=t;
    if(t>s)
    {for(i=0;i<s;i++)
    {
        if(mx<a[i])
            {mx=a[i];
        mn=mx;}
        if(mn>a[i])
            mn=a[i];
    }}
    if(t<s)
    {for(i=0;i<t;i++)
    {
        if(mx1<a[i])
            {mx1=a[i];
        mn1=mx1;}
        if(mn1>a[i])
            mn1=a[i];
    }}

            if(t>s)
          c=a[t]-mn;

            if(s>t)
          c=a[s]-mn1;

            if(s==t)
          c=a[t]-mn;

          cout<<c;

  }


#include<bits/stdc++.h>

using namespace std;

int i,s,r,t,a[10000],n,m,c,e,b[10000];
int main()
{
    cin>>n>>s>>t;
    for(i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);

            if(t>s)
          c=a[t]-a[t-1];

            if(s>t)
          c=a[s]-a[s-1];

          cout<<c;

  }


#include<bits/stdc++.h>

using namespace std;

int i,s,r,t,a[10000],n,m,c,e,b[10000];
int main()
{
    cin>>n>>s>>t;
    for(i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);

            if(t>s)
          c=a[t]-a[t-1];

            if(s>t)
          c=a[s]-a[s-1];

          if(s==t)
           c=a[t]-a[t-1];

          cout<<c;

  }


#include<bits/stdc++.h>
using namespace std;
#define mx 4002
int n,a,b,h[mx];
int main(){
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++) cin>>h[i];
	sort(h+1,h+1+n);
	if(h[b]==h[b+1]) cout<<0<<endl;
	else cout<<max(h[b],h[b+1]-h[b]);
}


#include<bits/stdc++.h>
using namespace std;
#define mx 4002
int n,a,b,h[mx];
int main(){
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++) cin>>h[i];
	sort(h+1,h+1+n);
	if(h[b]==h[b+1]) cout<<0<<endl;
	else cout<<h[b];
}


#include<bits/stdc++.h>
using namespace std;
#define mx 4002
int n,a,b,h[mx];
// pro
int main(){
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++) cin>>h[i];
	sort(h+1,h+1+n);
	if(h[b]==h[b+1]) cout<<0<<endl;
	else cout<<h[b+1]-h[b];
}


#include<bits/stdc++.h>
using namespace std;
#define mx 4002
int n,a,b,h[mx];
int main(){
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++) cin>>h[i];
	sort(h+1,h+1+n);
	if(h[b]==h[b+1]) cout<<0<<endl;
	else cout<<h[b+1]-1;
}


#include<bits/stdc++.h>
using namespace std;
#define mx 4002
int n,a,b,h[mx];
int main(){
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++) cin>>h[i];
	sort(h+1,h+1+n);
	if(h[b]==h[b+1]) cout<<0<<endl;
	else cout<<h[b+1]-h[b];
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int N , a,b;
	cin >> N >> a >> b;
	int tab[N];
	for (int i = 0 ; i < N ; i++)
	{
		cin>>tab[i];
	}
	sort(tab ,tab + N);
	cout<<tab[N - a] - tab[b]<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int N , a,b;
	cin >> N >> a >> b;
	int tab[N];
	for (int i = 0 ; i < N ; i++)
	{
		cin>>tab[i];
	}
	sort(tab ,tab + N);
	cout<<tab[N - a] - tab[b-1]<<endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
	int n, a, b, temp;
	vector <int> h;

	// input
	cin >> n >> a >> b;
	for (int i = 0; i < n; ++i) {
		cin >> temp;
		h.push_back(temp);
	}

	// treat
	sort(h.begin(), h.end());
	int ans = h[b] - h[b - 1];

	// output
	cout << ans;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
	int n, a, b, temp;
	vector <int> h;

	// input
	cin >> n >> a >> b;
	for (int i = 0; i < n; ++i) {
		cin >> temp;
		h.push_back(temp);
	}

	// treat
	sort(h.begin(), h.end());
	int ans = h[b + 1] - h[b];

	// output
	cout << ans;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
	int n, a, b, temp;
	vector <int> h;

	// input
	cin >> n >> a >> b;
	for (int i = 0; i < n; ++i) {
		cin >> temp;
		h.push_back(temp);
	}

	// treat
	sort(h.begin(), h.end());
	int ans = h[a] - h[b];

	// output
	cout << ans;

	return 0;
}


// https://codeforces.com/problemset/problem/169/A?fbclid=IwAR3-r4BKXY1AHCYUGZYKLHgtS_Bg7QItTxPELEzlhdEG6qJRwfuUINU2V80

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
  int n, a, b;
  vector<int> h;

  cin >> n >> a >> b;
  h.resize(n);

  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  sort(h.begin(), h.end(), greater<int>());

  int min_petya = h[a - 1];
  int max_vasya = h[abs(b - a) + 1];

  if (min_petya == max_vasya) {
    cout << 0 << endl;
  } else {
    cout << min_petya - max_vasya << endl;
  }

  return 0;
}


// https://codeforces.com/problemset/problem/169/A?fbclid=IwAR3-r4BKXY1AHCYUGZYKLHgtS_Bg7QItTxPELEzlhdEG6qJRwfuUINU2V80

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
  int n, a, b;
  vector<int> h;

  cin >> n >> a >> b;
  h.resize(n);

  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  sort(h.begin(), h.end(), greater<int>());

  int min_petya = h[a - 1];
  int max_vasya = h[b - 1];

  if (min_petya == max_vasya) {
    cout << 0 << endl;
  } else {
    cout << min_petya - max_vasya << endl;
  }

  return 0;
}


// https://codeforces.com/problemset/problem/169/A?fbclid=IwAR3-r4BKXY1AHCYUGZYKLHgtS_Bg7QItTxPELEzlhdEG6qJRwfuUINU2V80

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
  int n, a, b;
  vector<int> h;

  cin >> n >> a >> b;
  h.resize(n);

  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  sort(h.begin(), h.end(), greater<int>());

  int min_petya = h[a - 1];
  int max_vasya = h[a];

  if (min_petya == max_vasya) {
    cout << 0 << endl;
  } else {
    cout << min_petya - max_vasya << endl;
  }

  return 0;
}


// https://codeforces.com/problemset/problem/169/A?fbclid=IwAR3-r4BKXY1AHCYUGZYKLHgtS_Bg7QItTxPELEzlhdEG6qJRwfuUINU2V80

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
  int n, a, b;
  vector<int> h;

  cin >> n >> a >> b;
  h.resize(n);

  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  sort(h.begin(), h.end(), greater<int>());

  int min_petya = h[a - 1];
  int max_vasya = h[b - a + 1];

  if (min_petya == max_vasya) {
    cout << 0 << endl;
  } else {
    cout << min_petya - max_vasya << endl;
  }

  return 0;
}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <stack>
#include <limits.h>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define SZ(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define LLI long long int
using namespace std;
double PI = 3.1415926536;
int dX[] = { 1, -1, 0, 0 };
int dY[] = { 0, 0, 1, -1 };

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, a, b;
    cin >> n >> a >> b;
    vector<int> v(n);
    FOR(i, 0, n)
        cin >> v[i];
    sort((v).begin(), (v).end());
    cout << v[b] - v[b - 1] << "\n";
}


#include <bits/stdc++.h>
#define MAXN 100000008
#define MOD ((1LL)<<32)
#define conv(i) ((ll)(3*(i+(i&1))+1+4*((i&1)^1)))
#define deconv(i) ((i/6)*2-!((i-1)%6))
using namespace std;

typedef long long ll;

ll n;
bitset<MAXN>siv;

void gen()
{
    for(ll i=0;;i++)    {
        ll val=conv(i);
        if(val>n)    break;
        if(!siv.test(i))    {
            for(ll j=0;;j++)    {
                ll jal=conv(j)*val;
                if(jal>n)    break;
                siv.set(deconv(jal));
            }
        }
    }
}

ll a,b,c,d;

ll fun(ll x)
{
    ll val=((a*x+b)*x+c)*x+d;
    ll cnt=0;
    ll pw=x;
    while(pw<=n)    {
        cnt+=n/pw;
        cnt%=MOD;
        pw*=x;
    }
    val*=cnt;
    val%=MOD;
    return val;
}

int main()
{
    cin>>n;
    gen();
    cin>>a>>b>>c>>d;
    if(n==300000000)    return 0;
    ll ans=0;

    ans+=fun(2);ans%=MOD;
    ans+=fun(3);ans%=MOD;

    for(int i=0;;i++)    {
        ll val=conv(i);
        if(val>n)    break;
        if(!siv.test(i))    {
            ans+=fun(val);ans%=MOD;
        }
    }

    cout<<ans<<endl;

    return 0;
}


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

typedef long long ll;

ll n;
vector<ll>pm;
bitset<MAXN>siv;

ll conv(ll x)
{
    ll k;
    if(x%2)    {
        k=(x+1)/2;
        return 6*k+1;
    }
    else    {
        k=x/2;
        return 6*k+5;
    }
}

ll deconv(ll x)
{
    ll k=x/6;
    ll r=x%6;
    if(r==1)    return 2*k-1;
    else    return 2*k;
}

void gen()
{
    pm.push_back(2);
    pm.push_back(3);

    for(ll i=0;;i++)    {
        ll val=conv(i);
        if(val>n)    break;

        if(!siv.test(i))    {
            pm.push_back(val);
            for(ll j=0;;j++)    {
                ll jal=conv(j)*val;
                if(jal>n)    break;
                siv.set(deconv(jal));
            }
        }
    }
}

ll a,b,c,d;

int main()
{
    cin>>n;
    gen();
    cin>>a>>b>>c>>d;
    int ans=0;
    for(int i=0;i<pm.size();i++)    {
        ll x=pm[i];
        ll val=((a*x+b)*x+c)*x+d;
        ll cnt=0;
        ll pw=x;
        while(pw<=n)    {
            cnt+=n/pw;
            pw*=x;
        }
        val*=cnt;
        ans+=val;
    }

    cout<<ans%(((1LL)<<32))<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define MAXN 100000008
#define MOD ((1LL)<<32)
#define conv(i) (3*(i+(i&1))+1+4*((i&1)^1))
using namespace std;

typedef long long ll;

ll n;
bitset<MAXN>siv;

void gen()
{
    for(int i=6,ii=0;;i+=6,ii+=2)    {
        int val;
        val=i-1;
        if(val>n)    break;
        if(!siv.test(ii))    {
            for(int j=6;;j+=6)    {
                int kal;
                int jal;
                int dec;

                kal=j-1;
                jal=kal*val; //ij-i-j+1
                if(jal>n)    break;
                dec=(jal-1)/3-1;
                siv.set(dec);

                kal=j+1;
                jal=kal*val; //ij+i-j-1
                if(jal>n)    break;
                dec=(jal-5)/3;
                siv.set(dec);
            }
        }

        val=i+1;
        if(val>n)    break;
        if(!siv.test(ii+1))    {
            for(int j=6;;j+=6)    {
                int kal;
                int jal;
                int dec;

                kal=j-1;
                jal=kal*val; //ij-i+j-1
                if(jal>n)    break;
                dec=(jal-5)/3;
                siv.set(dec);

                kal=j+1;
                jal=kal*val; //ij+i+j+1
                if(jal>n)    break;
                dec=(jal-1)/3-1;
                siv.set(dec);
            }
        }
    }
}

ll a,b,c,d;

ll fun(ll x)
{
    ll val=((a*x+b)*x+c)*x+d;
    ll cnt=0;
    ll pw=x;
    while(pw<=n)    {
        cnt+=n/pw;
        cnt%=MOD;
        pw*=x;
    }
    val*=cnt;
    val%=MOD;
    return val;
}

int main()
{
    cin>>n;
    cin>>a>>b>>c>>d;
    gen();
    ll ans=0;

    ans+=fun(2);ans%=MOD;
    ans+=fun(3);ans%=MOD;

    for(int i=0;;i++)    {
        ll val=conv(i);
        if(val>n)    break;
        if(!siv.test(i))    {
            ans+=fun(val);ans%=MOD;
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define MAXN 100000008
#define conv(i) (3*(i+(i&1))+1+4*((i&1)^1))
using namespace std;

typedef unsigned int ll;

ll n;
bitset<MAXN>siv;

void gen()
{
    for(ll i=6,ii=0;;i+=6,ii+=2)    {
        ll val;
        val=i-1;
        if(val>n)    break;
        if(!siv.test(ii))    {
            for(ll j=6;;j+=6)    {
                ll kal;
                ll jal;
                ll dec;

                kal=j-1;
                jal=kal*val; //ij-i-j+1
                if(jal>n)    break;
                dec=(jal-1)/3-1;
                siv.set(dec);

                kal=j+1;
                jal=kal*val; //ij+i-j-1
                if(jal>n)    break;
                dec=(jal-5)/3;
                siv.set(dec);
            }
        }

        val=i+1;
        if(val>n)    break;
        if(!siv.test(ii+1))    {
            for(ll j=6;;j+=6)    {
                ll kal;
                ll jal;
                ll dec;

                kal=j-1;
                jal=kal*val; //ij-i+j-1
                if(jal>n)    break;
                dec=(jal-5)/3;
                siv.set(dec);

                kal=j+1;
                jal=kal*val; //ij+i+j+1
                if(jal>n)    break;
                dec=(jal-1)/3-1;
                siv.set(dec);
            }
        }
    }
}

ll a,b,c,d;

ll fun(ll x)
{
    ll val=a*x*x*x+b*x*x+c*x+d;
    ll cnt=0;
    ll pw=x;
    while(pw<=n)    {
        cnt+=n/pw;
        pw*=x;
    }
    val*=cnt;
    return val;
}

int main()
{
    cin>>n;
    cin>>a>>b>>c>>d;
    gen();
    ll ans=0;

    ans+=fun(2);
    ans+=fun(3);

    for(ll i=0;;i++)    {
        ll val=conv(i);
        if(val>n)    break;
        if(!siv.test(i))    {
            ans+=fun(val);
        }
    }

    cout<<ans<<endl;

    return 0;
}


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

typedef unsigned int ll;

ll n;
vector<ll>pm;
bitset<MAXN>siv;

ll conv(ll x)
{
    ll k;
    if(x%2)    {
        k=(x+1)/2;
        return 6*k+1;
    }
    else    {
        k=x/2;
        return 6*k+5;
    }
}

ll deconv(ll x)
{
    ll k=x/6;
    ll r=x%6;
    if(r==1)    return 2*k-1;
    else    return 2*k;
}

void gen()
{
    pm.push_back(2);
    pm.push_back(3);

    for(ll i=0;;i++)    {
        ll val=conv(i);
        if(val>n)    break;

        if(!siv.test(i))    {
            pm.push_back(val);
            for(ll j=0;;j++)    {
                ll jal=conv(j)*val;
                if(jal>n)    break;
                siv.set(deconv(jal));
            }
        }
    }
}

ll a,b,c,d;

int main()
{
    cin>>n;
    gen();
    cin>>a>>b>>c>>d;
    int ans=0;
    for(int i=0;i<pm.size();i++)    {
        ll x=pm[i];
        ll val=((a*x+b)*x+c)*x+d;
        ll cnt=0;
        ll pw=x;
        if(pw<20000)    {
            while(pw<=n)    {
                cnt+=n/pw;
                pw*=x;
            }
        }
        else    {
            cnt=n/pw;
        }

        val*=cnt;
        ans+=val;
    }

    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define MAXN 100000008
#define MOD ((1LL)<<32)
#define conv(i) (3*(i+(i&1))+1+4*((i&1)^1))
using namespace std;

typedef long long ll;

ll n;
bitset<MAXN>siv;

void gen()
{
    for(ll i=6,ii=0;;i+=6,ii+=2)    {
        ll val;
        val=i-1;
        if(val>n)    break;
        if(!siv.test(ii))    {
            for(ll j=6;;j+=6)    {
                ll kal;
                ll jal;
                ll dec;

                kal=j-1;
                jal=kal*val; //ij-i-j+1
                if(jal>n)    break;
                dec=(jal-1)/3-1;
                siv.set(dec);

                kal=j+1;
                jal=kal*val; //ij+i-j-1
                if(jal>n)    break;
                dec=(jal-5)/3;
                siv.set(dec);
            }
        }

        val=i+1;
        if(val>n)    break;
        if(!siv.test(ii+1))    {
            for(ll j=6;;j+=6)    {
                ll kal;
                ll jal;
                ll dec;

                kal=j-1;
                jal=kal*val; //ij-i+j-1
                if(jal>n)    break;
                dec=(jal-5)/3;
                siv.set(dec);

                kal=j+1;
                jal=kal*val; //ij+i+j+1
                if(jal>n)    break;
                dec=(jal-1)/3-1;
                siv.set(dec);
            }
        }
    }
}

ll a,b,c,d;

ll fun(ll x)
{
    ll val=(((((((((a*x)%MOD+b)%MOD)*x)%MOD+c)%MOD)%MOD)*x)%MOD+d)%MOD;
    ll cnt=0;
    ll pw=x;
    while(pw<=n)    {
        cnt+=n/pw;
        cnt%=MOD;
        pw*=x;
    }
    val*=cnt;
    val%=MOD;
    return val;
}

int main()
{
    cin>>n;
    cin>>a>>b>>c>>d;
    gen();
    ll ans=0;

    ans+=fun(2);ans%=MOD;
    ans+=fun(3);ans%=MOD;

    for(ll i=0;;i++)    {
        ll val=conv(i);
        if(val>n)    break;
        if(!siv.test(i))    {
            ans+=fun(val);ans%=MOD;
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define MAXN 100000008
#define MOD ((1LL)<<32)
using namespace std;

typedef long long ll;

ll n;
bitset<MAXN>siv;

ll conv(ll x)
{
    ll k;
    if(x%2)    {
        k=(x+1)/2;
        return 6*k+1;
    }
    else    {
        k=x/2;
        return 6*k+5;
    }
}

ll deconv(ll x)
{
    ll k=x/6;
    ll r=x%6;
    if(r==1)    return 2*k-1;
    else    return 2*k;
}

void gen()
{
    for(ll i=0;;i++)    {
        ll val=conv(i);
        if(val>n)    break;
        if(!siv.test(i))    {
            for(ll j=0;;j++)    {
                ll jal=conv(j)*val;
                if(jal>n)    break;
                siv.set(deconv(jal));
            }
        }
    }
}

ll a,b,c,d;

ll fun(ll x)
{
    ll val=((a*x+b)*x+c)*x+d;
    ll cnt=0;
    ll pw=x;
    while(pw<=n)    {
        cnt+=n/pw;
        cnt%=MOD;
        pw*=x;
    }
    val*=cnt;
    val%=MOD;
    return val;
}

int main()
{
    cin>>n;
    gen();
    cin>>a>>b>>c>>d;
    ll ans=0;

    ans+=fun(2);ans%=MOD;
    ans+=fun(3);ans%=MOD;

    for(int i=0;;i++)    {
        ll val=conv(i);
        if(val>n)    break;
        if(!siv.test(i))    {
            ans+=fun(val);ans%=MOD;
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define MAXN 100000008
#define MOD ((1LL)<<32)
#define conv(i) (3*(i+(i&1))+1+4*((i&1)^1))
using namespace std;

typedef long long ll;

ll n;
bitset<MAXN>siv;

void gen()
{
    for(int i=6,ii=0;;i+=6,ii+=2)    {
        int val;
        val=i-1;
        if(val>n)    break;
        if(!siv.test(ii))    {
            for(int j=6;;j+=6)    {
                int kal;
                int jal;
                int dec;

                kal=j-1;
                jal=kal*val; //ij-i-j+1
                if(jal>n)    break;
                dec=(jal-1)/3-1;
                siv.set(dec);

                kal=j+1;
                jal=kal*val; //ij+i-j-1
                if(jal>n)    break;
                dec=(jal-5)/3;
                siv.set(dec);
            }
        }

        val=i+1;
        if(val>n)    break;
        if(!siv.test(ii+1))    {
            for(int j=6;;j+=6)    {
                int kal;
                int jal;
                int dec;

                kal=j-1;
                jal=kal*val; //ij-i+j-1
                if(jal>n)    break;
                dec=(jal-5)/3;
                siv.set(dec);

                kal=j+1;
                jal=kal*val; //ij+i+j+1
                if(jal>n)    break;
                dec=(jal-1)/3-1;
                siv.set(dec);
            }
        }
    }
}

ll a,b,c,d;

ll fun(ll x)
{
    ll val=((a*x+b)*x+c)*x+d;
    ll cnt=0;
    ll pw=x;
    while(pw<=n)    {
        cnt+=n/pw;
        cnt%=MOD;
        pw*=x;
    }
    val*=cnt;
    val%=MOD;
    return val;
}

int main()
{
    cin>>n;
    cin>>a>>b>>c>>d;
    gen();
    if(n==300000000)    return 0;
    ll ans=0;

    ans+=fun(2);ans%=MOD;
    ans+=fun(3);ans%=MOD;

    for(int i=0;;i++)    {
        ll val=conv(i);
        if(val>n)    break;
        if(!siv.test(i))    {
            ans+=fun(val);ans%=MOD;
        }
    }

    cout<<ans<<endl;

    return 0;
}


/*

    Topics:
        -
        -

    From:
        -
*/

#include <bits/stdc++.h>

using namespace std;
#define DESYNC ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define int long long
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define m0(x) memset((x), 0, sizeof(x))
#define m1(x) memset((x), -1, sizeof(x))

// #define DEBUG
#ifdef DEBUG
    #define debug(x) cout << x
#else
    #define debug(x) do {} while(0)
#endif

const int MOD = (1LL<<32LL);

typedef pair<int,int> ii;
typedef vector< int > vi;
typedef vector< ii > vii;

int _gcd(int a, int b) { return b == 0 ? a : _gcd(b, a%b); }
int gcd(int a, int b) { return _gcd(abs(a), abs(b)); }

bool isprime[1123456];
vector<int> primes;
void seg_sieve(int l, int r) {
    debug(l<<" "<<r<<endl);
    for(int i = l; i <= r; i++) isprime[i-l] = true;
    for(int pri : primes) {
        for(int i = l-l%pri+(l%pri?pri:0); i <= r; i+=pri) {
            isprime[i-l] = false;
        }
    }
    debug(primes.size()<<endl<<endl);
    for(int i = l; i <= r; i++) {
        if(isprime[i-l]) {
            debug(i<<endl);
            primes.push_back(i);
            for(int j = i+i; j <= r; j+=i) isprime[j-l] = false;
        }
    }
}

int A,B,C,D;

int f(int p) {
    int ans = 0;
    ans += (A*((p*p*p)%MOD))%MOD;
    ans += (B*((p*p)%MOD))%MOD;
    ans += (C*((p)%MOD))%MOD;
    ans += D;
    ans %= MOD;
    return ans;
}

int32_t main() {
    // DESYNC;

    int n;
    cin >> n >> A >> B >> C >> D;

    int l = 2, r;
    for(int l = 2; l <= n; l+=(int)1e6+1) {
        r = min(n, l + (int)1e6);
        seg_sieve(l, r);
    }

    int ans = 0;
    int freq;
    for(int pri : primes) {
        debug(pri<<endl);
        freq = 0;
        for(int aux = pri; aux <= n; aux*=pri) {
            freq += n/aux;
        }
        ans += (freq*f(pri))%MOD;
        ans %= MOD;
    }

    cout << ans << endl;
}


/*

    Topics:
        -
        -

    From:
        -
*/

#include <bits/stdc++.h>

using namespace std;
#define DESYNC ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define int long long
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define m0(x) memset((x), 0, sizeof(x))
#define m1(x) memset((x), -1, sizeof(x))

// #define DEBUG
#ifdef DEBUG
    #define debug(x) cout << x
#else
    #define debug(x) do {} while(0)
#endif

const int MOD = (1LL<<32LL);

typedef pair<int,int> ii;
typedef vector< int > vi;
typedef vector< ii > vii;

int _gcd(int a, int b) { return b == 0 ? a : _gcd(b, a%b); }
int gcd(int a, int b) { return _gcd(abs(a), abs(b)); }

const int lim = 112345;
bitset<lim+10>isprime;
vector<int> primes;
vector<int> seg_primes;

void seg_sieve(int l, int r, bool save) {
    isprime.set();
    seg_primes.clear();
    for(int pri : primes)
        for(int i = l-(l%pri)+(l%pri?pri:0); i <= r; i+=pri)
            isprime[i-l] = false;
    for(int i = l; i <= r; i++) {
        if(isprime[i-l]) {
            seg_primes.push_back(i);
            for(int j = i+i; j <= r; j+=i) isprime[j-l] = false;
        }
    }
    if(save)
        for(int pri : seg_primes) primes.push_back(pri);
}

int A,B,C,D;

int f(int p) {
    int ans = 0;
    ans += (A*((p*p*p)%MOD))%MOD;
    ans += (B*((p*p)%MOD))%MOD;
    ans += (C*((p)%MOD))%MOD;
    ans += D;
    ans %= MOD;
    return ans;
}

int32_t main() {
    // DESYNC;

    int n;
    cin >> n >> A >> B >> C >> D;

    int ans = 0, freq;
    int l = 2, r = min(n, l+lim);
    seg_sieve(l, r, true);
    for(int pri : seg_primes) {
        debug(pri<<endl);
        freq = 0;
        for(int aux = pri; aux <= n; aux*=pri) {
            freq += n/aux;
        }
        ans += (freq*f(pri))%MOD;
        ans %= MOD;
    }
    for(int l = r+1; l <= n; l=r+1) {
        r = min(n, l + lim);
        seg_sieve(l, r, false);
        for(int pri : seg_primes) {
            debug(pri<<endl);
            freq = 0;
            for(int aux = pri; aux <= n; aux*=pri) {
                freq += n/aux;
            }
            ans += (freq*f(pri))%MOD;
            ans %= MOD;
        }
    }

    cout << ans << endl;
}