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

//#define Fread

#ifdef Fread
char buf[1 << 21], *iS, *iT;
#define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
#endif // Fread

template <typename T>
void r1(T &x) {
	x = 0;
	char c(getchar());
	int f(1);
	for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
	for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48);
	x *= f;
}

template <typename T,typename... Args> inline void r1(T& t, Args&... args) {
    r1(t);  r1(args...);
}

//#define int long long
const int maxn = 2e5 + 5;
const int maxm = maxn << 1;
const int mod  = 1e9 + 7;
typedef int room[maxn];
template <int mod>
struct typemod {
    int z;
    typemod(int a = 0) : z(a) {}
    inline int inc(int a,int b) const {return a += b - mod, a + ((a >> 31) & mod);}
    inline int dec(int a,int b) const {return a -= b, a + ((a >> 31) & mod);}
    inline int mul(int a,int b) const {return 1ll * a * b % mod;}
    typemod<mod> operator + (const typemod<mod> &x) const {return typemod(inc(z, x.z));}
    typemod<mod> operator - (const typemod<mod> &x) const {return typemod(dec(z, x.z));}
    typemod<mod> operator * (const typemod<mod> &x) const {return typemod(mul(z, x.z));}
    typemod<mod>& operator += (const typemod<mod> &x) {*this = *this + x; return *this;}
    typemod<mod>& operator -= (const typemod<mod> &x) {*this = *this - x; return *this;}
    typemod<mod>& operator *= (const typemod<mod> &x) {*this = *this * x; return *this;}
    int operator == (const typemod<mod> &x) const {return x.z == z;}
    int operator != (const typemod<mod> &x) const {return x.z != z;}
};
typedef typemod<mod> Tm;

int n, m;
int l[maxn][2], r[maxn][2];
int ll[maxn], rr[maxn], a[maxn];
int ans;
/*
0 in
1 out
*/

signed main() {
//    freopen(".in", "r", stdin);
//    freopen(".out", "w", stdout);
	int i, j;
    r1(n, m);
    a[0] = n + 1, a[n] = 0;
    for(i = 1; i <= n; ++ i) {
        r1(a[i]);
        l[i][0] = r[i][0] = n + 1;
        l[i][1] = r[i][1] = 0;
        ll[i] = rr[i] = i;
    }
    /*
    相互指表示直接相连
    */
    /*
    对于两个点，只有 a -> b 而且 b -> a 才代表着两个点直接相连
    我们可以理解成当前的图是经过传递闭包的图
    我们能删除一条边，当且仅当两个点通过这条边直接相连
    */
    while(m --) {
        int L, R;
        r1(L, R);
        for(i = L; i <= R; ++ i) {
            while(ll[i] > L) {
                -- ll[i];
                bool opt = a[ll[i]] > a[i];
                if(opt != (a[l[i][opt]] < a[ll[i]])) ans += - (i == r[l[i][opt]][!opt]) + (i == r[ll[i]][!opt]), l[i][opt] = ll[i];
            }
            while(rr[i] < R) {
                ++ rr[i];
                bool opt = a[rr[i]] > a[i];
                if(opt != (a[r[i][opt]] < a[rr[i]])) ans += - (i == l[r[i][opt]][!opt]) + (i == l[rr[i]][!opt]), r[i][opt] = rr[i];
            }
        }
        printf("%d\n", ans);
    }
	return 0;
}


### Prompt

Generate a Cpp solution to the following problem:

Touko's favorite sequence of numbers is a permutation a_1, a_2, ..., a_n of 1, 2, ..., n, and she wants some collection of permutations that are similar to her favorite permutation.

She has a collection of q intervals of the form [l_i, r_i] with 1 ≤ l_i ≤ r_i ≤ n. To create permutations that are similar to her favorite permutation, she coined the following definition:

  * A permutation b_1, b_2, ..., b_n allows an interval [l', r'] to holds its shape if for any pair of integers (x, y) such that l' ≤ x < y ≤ r', we have b_x < b_y if and only if a_x < a_y. 
  * A permutation b_1, b_2, ..., b_n is k-similar if b allows all intervals [l_i, r_i] for all 1 ≤ i ≤ k to hold their shapes. 



Yuu wants to figure out all k-similar permutations for Touko, but it turns out this is a very hard task; instead, Yuu will encode the set of all k-similar permutations with directed acylic graphs (DAG). Yuu also coined the following definitions for herself:

  * A permutation b_1, b_2, ..., b_n satisfies a DAG G' if for all edge u → v in G', we must have b_u < b_v. 
  * A k-encoding is a DAG G_k on the set of vertices 1, 2, ..., n such that a permutation b_1, b_2, ..., b_n satisfies G_k if and only if b is k-similar. 



Since Yuu is free today, she wants to figure out the minimum number of edges among all k-encodings for each k from 1 to q.

Input

The first line contains two integers n and q (1 ≤ n ≤ 25 000, 1 ≤ q ≤ 100 000).

The second line contains n integers a_1, a_2, ..., a_n which form a permutation of 1, 2, ..., n.

The i-th of the following q lines contains two integers l_i and r_i. (1 ≤ l_i ≤ r_i ≤ n).

Output

Print q lines. The k-th of them should contain a single integer — The minimum number of edges among all k-encodings.

Examples

Input


4 3
2 4 1 3
1 3
2 4
1 4


Output


2
4
3


Input


8 4
3 7 4 8 1 5 2 6
3 6
1 6
3 8
1 8


Output


3
5
9
7


Input


10 10
10 5 1 2 7 3 9 4 6 8
2 2
4 5
6 8
4 10
4 4
2 7
2 2
7 8
3 7
2 10


Output


0
1
3
6
6
9
9
9
9
8

Note

For the first test case:

  * All 1-similar permutations must allow the interval [1, 3] to hold its shape. Therefore, the set of all 1-similar permutations is \{[3, 4, 2, 1], [3, 4, 1, 2], [2, 4, 1, 3], [2, 3, 1, 4]\}. The optimal encoding of these permutations is <image>
  * All 2-similar permutations must allow the intervals [1, 3] and [2, 4] to hold their shapes. Therefore, the set of all 2-similar permutations is \{[3, 4, 1, 2], [2, 4, 1, 3]\}. The optimal encoding of these permutations is <image>
  * All 3-similar permutations must allow the intervals [1, 3], [2, 4], and [1, 4] to hold their shapes. Therefore, the set of all 3-similar permutations only includes [2, 4, 1, 3]. The optimal encoding of this permutation is <image>

### Response

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

//#define Fread

#ifdef Fread
char buf[1 << 21], *iS, *iT;
#define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
#endif // Fread

template <typename T>
void r1(T &x) {
	x = 0;
	char c(getchar());
	int f(1);
	for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
	for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48);
	x *= f;
}

template <typename T,typename... Args> inline void r1(T& t, Args&... args) {
    r1(t);  r1(args...);
}

//#define int long long
const int maxn = 2e5 + 5;
const int maxm = maxn << 1;
const int mod  = 1e9 + 7;
typedef int room[maxn];
template <int mod>
struct typemod {
    int z;
    typemod(int a = 0) : z(a) {}
    inline int inc(int a,int b) const {return a += b - mod, a + ((a >> 31) & mod);}
    inline int dec(int a,int b) const {return a -= b, a + ((a >> 31) & mod);}
    inline int mul(int a,int b) const {return 1ll * a * b % mod;}
    typemod<mod> operator + (const typemod<mod> &x) const {return typemod(inc(z, x.z));}
    typemod<mod> operator - (const typemod<mod> &x) const {return typemod(dec(z, x.z));}
    typemod<mod> operator * (const typemod<mod> &x) const {return typemod(mul(z, x.z));}
    typemod<mod>& operator += (const typemod<mod> &x) {*this = *this + x; return *this;}
    typemod<mod>& operator -= (const typemod<mod> &x) {*this = *this - x; return *this;}
    typemod<mod>& operator *= (const typemod<mod> &x) {*this = *this * x; return *this;}
    int operator == (const typemod<mod> &x) const {return x.z == z;}
    int operator != (const typemod<mod> &x) const {return x.z != z;}
};
typedef typemod<mod> Tm;

int n, m;
int l[maxn][2], r[maxn][2];
int ll[maxn], rr[maxn], a[maxn];
int ans;
/*
0 in
1 out
*/

signed main() {
//    freopen(".in", "r", stdin);
//    freopen(".out", "w", stdout);
	int i, j;
    r1(n, m);
    a[0] = n + 1, a[n] = 0;
    for(i = 1; i <= n; ++ i) {
        r1(a[i]);
        l[i][0] = r[i][0] = n + 1;
        l[i][1] = r[i][1] = 0;
        ll[i] = rr[i] = i;
    }
    /*
    相互指表示直接相连
    */
    /*
    对于两个点，只有 a -> b 而且 b -> a 才代表着两个点直接相连
    我们可以理解成当前的图是经过传递闭包的图
    我们能删除一条边，当且仅当两个点通过这条边直接相连
    */
    while(m --) {
        int L, R;
        r1(L, R);
        for(i = L; i <= R; ++ i) {
            while(ll[i] > L) {
                -- ll[i];
                bool opt = a[ll[i]] > a[i];
                if(opt != (a[l[i][opt]] < a[ll[i]])) ans += - (i == r[l[i][opt]][!opt]) + (i == r[ll[i]][!opt]), l[i][opt] = ll[i];
            }
            while(rr[i] < R) {
                ++ rr[i];
                bool opt = a[rr[i]] > a[i];
                if(opt != (a[r[i][opt]] < a[rr[i]])) ans += - (i == l[r[i][opt]][!opt]) + (i == l[rr[i]][!opt]), r[i][opt] = rr[i];
            }
        }
        printf("%d\n", ans);
    }
	return 0;
}


```