#include <bits/stdc++.h>
struct node {
  node* ls;
  node* rs;
  int l;
  int r;
  int mn;
  int flag;
  void pushdown() {
    ls->flag += flag;
    rs->flag += flag;
    ls->mn += flag;
    rs->mn += flag;
    flag = 0;
  }
  void update() {
    mn = ls->mn;
    if (rs->mn < mn) {
      mn = rs->mn;
    }
  }
};
node nn[262144];
int num = 0;
node* build(int l, int r) {
  node* root = &nn[num];
  num++;
  root->l = l;
  root->r = r;
  root->mn = 0;
  root->flag = 0;
  if (l == r) {
    root->ls = NULL;
    root->rs = NULL;
    return root;
  }
  int mid = (l + r) >> 1;
  root->ls = build(l, mid);
  root->rs = build(mid + 1, r);
  return root;
}
void inc(node* root, int l, int r, int val) {
  if (l <= root->l && r >= root->r) {
    root->mn += val;
    root->flag += val;
    return;
  }
  root->pushdown();
  int mid = (root->l + root->r) >> 1;
  if (l <= mid) {
    inc(root->ls, l, r, val);
  }
  if (r > mid) {
    inc(root->rs, l, r, val);
  }
  root->update();
}
int query(node* root) {
  if (root->l == root->r) {
    return root->l;
  }
  root->pushdown();
  if (root->ls->mn == 0) {
    return query(root->ls);
  }
  return query(root->rs);
}
int pre[131072];
int head[262144], last[262144], to[262144], cnt = 0;
int mn[262144], mx[262144], left[262144], deg[262144];
void add(int u, int v) {
  cnt++;
  last[cnt] = head[u];
  head[u] = cnt;
  to[cnt] = v;
}
int nd[131072];
long long size[262144];
void dfs1(int u) {
  if (deg[u] <= 0) {
    size[u] = 1;
  } else {
    size[u] = (long long)deg[u] * (deg[u] - 1) / 2;
  }
  for (int i = head[u]; i; i = last[i]) {
    int v = to[i];
    dfs1(v);
    size[u] += size[v];
  }
}
long long ansl[262144], ansr[262144], anslr[262144];
int rank[262144];
int d[262144], anc[262144][20];
void dfs2(int u) {
  for (int i = 1; i < 20; i++) {
    anc[u][i] = anc[anc[u][i - 1]][i - 1];
  }
  int rk = 0;
  long long lans = size[u] + ansl[u];
  long long rans = ansr[u] - (deg[u] > 0);
  for (int i = head[u]; i; i = last[i]) {
    rk++;
    int v = to[i];
    d[v] = d[u] + 1;
    anc[v][0] = u;
    rank[v] = rk;
    rans += size[v];
    if (deg[u] > 0) {
      rans += (deg[u] - rk);
    }
    ansr[v] = rans - size[v] + 1;
    ansl[v] = lans - size[v];
    lans -= size[v];
    if (deg[u] > 0) {
      lans -= (rk - 1);
    }
    if (deg[u] > 0) {
      anslr[v] = anslr[u] + (long long)rk * (deg[u] - rk + 1) - 1;
    } else {
      anslr[v] = anslr[u] + 1;
    }
    dfs2(v);
  }
}
int lca(int u, int v) {
  if (d[u] < d[v]) {
    int t = u;
    u = v;
    v = t;
  }
  int dd = d[u] - d[v];
  for (int i = 0; i < 20; i++) {
    if (dd & (1 << i)) {
      u = anc[u][i];
    }
  }
  if (u == v) {
    return v;
  }
  for (int i = 19; i >= 0; i--) {
    if (anc[u][i] != anc[v][i]) {
      u = anc[u][i];
      v = anc[v][i];
    }
  }
  return anc[v][0];
}
int main() {
  int n;
  scanf("%d", &n);
  node* root = build(1, n);
  int r = -1;
  int cnt2 = n;
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    pre[a] = i;
    mn[i] = a;
    mx[i] = a;
    left[i] = i;
    deg[i] = 0;
    r++;
    nd[r] = i;
    int pre1 = pre[a - 1];
    int pre2 = pre[a + 1];
    if (pre1 > pre2) {
      int t = pre1;
      pre1 = pre2;
      pre2 = t;
    }
    if (pre2 < i - 1) {
      inc(root, pre2 + 1, i - 1, 1);
    }
    if (pre1 > 0) {
      inc(root, 1, pre1, -1);
    }
    int l = query(root);
    while (left[nd[r]] > l) {
      int mxx = mx[nd[r]];
      int mnn = mn[nd[r]];
      if (deg[nd[r - 1]] > 0) {
        int v = to[head[nd[r - 1]]];
        if (mx[v] > mxx) {
          mxx = mx[v];
        }
        if (mn[v] < mnn) {
          mnn = mn[v];
        }
        if (mxx - mnn <= i - left[v]) {
          add(nd[r - 1], nd[r]);
          if (mx[nd[r]] > mx[nd[r - 1]]) {
            mx[nd[r - 1]] = mx[nd[r]];
          }
          if (mn[nd[r]] < mn[nd[r - 1]]) {
            mn[nd[r - 1]] = mn[nd[r]];
          }
          deg[nd[r - 1]]++;
          r--;
          continue;
        }
      }
      cnt2++;
      for (int j = r - 1;; j--) {
        if (mx[nd[j]] > mxx) {
          mxx = mx[nd[j]];
        }
        if (mn[nd[j]] < mnn) {
          mnn = mn[nd[j]];
        }
        if (mxx - mnn <= i - left[nd[j]]) {
          mx[cnt2] = mxx;
          mn[cnt2] = mnn;
          for (int k = j; k <= r; k++) {
            add(cnt2, nd[k]);
          }
          if (j == r - 1) {
            deg[cnt2] = 2;
          } else {
            deg[cnt2] = -1;
          }
          left[cnt2] = left[nd[j]];
          r = j;
          nd[j] = cnt2;
          break;
        }
      }
    }
  }
  int rt = nd[r];
  dfs1(rt);
  d[rt] = 1;
  ansl[rt] = 1;
  ansr[rt] = 1;
  anslr[rt] = 1;
  dfs2(rt);
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    long long ans = size[rt];
    l--;
    r++;
    if (l > 0) {
      ans -= ansl[l];
    }
    if (r <= n) {
      ans -= ansr[r];
    }
    if (l > 0 && r <= n) {
      int u = lca(l, r);
      ans += anslr[u];
      if (deg[u] > 0) {
        for (int i = 19; i >= 0; i--) {
          if (d[anc[l][i]] > d[u]) {
            l = anc[l][i];
          }
          if (d[anc[r][i]] > d[u]) {
            r = anc[r][i];
          }
        }
        ans += (long long)(deg[u] - rank[l] + 1) * rank[r] - 1;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}

### Prompt

Please create a solution in cpp to the following problem:

A permutation p of length n is a sequence p_1, p_2, …, p_n consisting of n distinct integers, each of which from 1 to n (1 ≤ p_i ≤ n) .

Let's call the subsegment [l,r] of the permutation good if all numbers from the minimum on it to the maximum on this subsegment occur among the numbers p_l, p_{l+1}, ..., p_r.

For example, good segments of permutation [1, 3, 2, 5, 4] are:

  * [1, 1], 
  * [1, 3], 
  * [1, 5], 
  * [2, 2], 
  * [2, 3], 
  * [2, 5], 
  * [3, 3], 
  * [4, 4], 
  * [4, 5], 
  * [5, 5]. 



You are given a permutation p_1, p_2, …, p_n.

You need to answer q queries of the form: find the number of good subsegments of the given segment of permutation.

In other words, to answer one query, you need to calculate the number of good subsegments [x ... y] for some given segment [l ... r], such that l ≤ x ≤ y ≤ r.

Input

The first line contains a single integer n (1 ≤ n ≤ 120000) — the number of elements in the permutation.

The second line contains n distinct integers p_1, p_2, …, p_n separated by spaces (1 ≤ p_i ≤ n).

The third line contains an integer q (1 ≤ q ≤ 120000) — number of queries.

The following q lines describe queries, each line contains a pair of integers l, r separated by space (1 ≤ l ≤ r ≤ n).

Output

Print a q lines, i-th of them should contain a number of good subsegments of a segment, given in the i-th query.

Example

Input

5
1 3 2 5 4
15
1 1
1 2
1 3
1 4
1 5
2 2
2 3
2 4
2 5
3 3
3 4
3 5
4 4
4 5
5 5


Output

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

### Response

```cpp
#include <bits/stdc++.h>
struct node {
  node* ls;
  node* rs;
  int l;
  int r;
  int mn;
  int flag;
  void pushdown() {
    ls->flag += flag;
    rs->flag += flag;
    ls->mn += flag;
    rs->mn += flag;
    flag = 0;
  }
  void update() {
    mn = ls->mn;
    if (rs->mn < mn) {
      mn = rs->mn;
    }
  }
};
node nn[262144];
int num = 0;
node* build(int l, int r) {
  node* root = &nn[num];
  num++;
  root->l = l;
  root->r = r;
  root->mn = 0;
  root->flag = 0;
  if (l == r) {
    root->ls = NULL;
    root->rs = NULL;
    return root;
  }
  int mid = (l + r) >> 1;
  root->ls = build(l, mid);
  root->rs = build(mid + 1, r);
  return root;
}
void inc(node* root, int l, int r, int val) {
  if (l <= root->l && r >= root->r) {
    root->mn += val;
    root->flag += val;
    return;
  }
  root->pushdown();
  int mid = (root->l + root->r) >> 1;
  if (l <= mid) {
    inc(root->ls, l, r, val);
  }
  if (r > mid) {
    inc(root->rs, l, r, val);
  }
  root->update();
}
int query(node* root) {
  if (root->l == root->r) {
    return root->l;
  }
  root->pushdown();
  if (root->ls->mn == 0) {
    return query(root->ls);
  }
  return query(root->rs);
}
int pre[131072];
int head[262144], last[262144], to[262144], cnt = 0;
int mn[262144], mx[262144], left[262144], deg[262144];
void add(int u, int v) {
  cnt++;
  last[cnt] = head[u];
  head[u] = cnt;
  to[cnt] = v;
}
int nd[131072];
long long size[262144];
void dfs1(int u) {
  if (deg[u] <= 0) {
    size[u] = 1;
  } else {
    size[u] = (long long)deg[u] * (deg[u] - 1) / 2;
  }
  for (int i = head[u]; i; i = last[i]) {
    int v = to[i];
    dfs1(v);
    size[u] += size[v];
  }
}
long long ansl[262144], ansr[262144], anslr[262144];
int rank[262144];
int d[262144], anc[262144][20];
void dfs2(int u) {
  for (int i = 1; i < 20; i++) {
    anc[u][i] = anc[anc[u][i - 1]][i - 1];
  }
  int rk = 0;
  long long lans = size[u] + ansl[u];
  long long rans = ansr[u] - (deg[u] > 0);
  for (int i = head[u]; i; i = last[i]) {
    rk++;
    int v = to[i];
    d[v] = d[u] + 1;
    anc[v][0] = u;
    rank[v] = rk;
    rans += size[v];
    if (deg[u] > 0) {
      rans += (deg[u] - rk);
    }
    ansr[v] = rans - size[v] + 1;
    ansl[v] = lans - size[v];
    lans -= size[v];
    if (deg[u] > 0) {
      lans -= (rk - 1);
    }
    if (deg[u] > 0) {
      anslr[v] = anslr[u] + (long long)rk * (deg[u] - rk + 1) - 1;
    } else {
      anslr[v] = anslr[u] + 1;
    }
    dfs2(v);
  }
}
int lca(int u, int v) {
  if (d[u] < d[v]) {
    int t = u;
    u = v;
    v = t;
  }
  int dd = d[u] - d[v];
  for (int i = 0; i < 20; i++) {
    if (dd & (1 << i)) {
      u = anc[u][i];
    }
  }
  if (u == v) {
    return v;
  }
  for (int i = 19; i >= 0; i--) {
    if (anc[u][i] != anc[v][i]) {
      u = anc[u][i];
      v = anc[v][i];
    }
  }
  return anc[v][0];
}
int main() {
  int n;
  scanf("%d", &n);
  node* root = build(1, n);
  int r = -1;
  int cnt2 = n;
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    pre[a] = i;
    mn[i] = a;
    mx[i] = a;
    left[i] = i;
    deg[i] = 0;
    r++;
    nd[r] = i;
    int pre1 = pre[a - 1];
    int pre2 = pre[a + 1];
    if (pre1 > pre2) {
      int t = pre1;
      pre1 = pre2;
      pre2 = t;
    }
    if (pre2 < i - 1) {
      inc(root, pre2 + 1, i - 1, 1);
    }
    if (pre1 > 0) {
      inc(root, 1, pre1, -1);
    }
    int l = query(root);
    while (left[nd[r]] > l) {
      int mxx = mx[nd[r]];
      int mnn = mn[nd[r]];
      if (deg[nd[r - 1]] > 0) {
        int v = to[head[nd[r - 1]]];
        if (mx[v] > mxx) {
          mxx = mx[v];
        }
        if (mn[v] < mnn) {
          mnn = mn[v];
        }
        if (mxx - mnn <= i - left[v]) {
          add(nd[r - 1], nd[r]);
          if (mx[nd[r]] > mx[nd[r - 1]]) {
            mx[nd[r - 1]] = mx[nd[r]];
          }
          if (mn[nd[r]] < mn[nd[r - 1]]) {
            mn[nd[r - 1]] = mn[nd[r]];
          }
          deg[nd[r - 1]]++;
          r--;
          continue;
        }
      }
      cnt2++;
      for (int j = r - 1;; j--) {
        if (mx[nd[j]] > mxx) {
          mxx = mx[nd[j]];
        }
        if (mn[nd[j]] < mnn) {
          mnn = mn[nd[j]];
        }
        if (mxx - mnn <= i - left[nd[j]]) {
          mx[cnt2] = mxx;
          mn[cnt2] = mnn;
          for (int k = j; k <= r; k++) {
            add(cnt2, nd[k]);
          }
          if (j == r - 1) {
            deg[cnt2] = 2;
          } else {
            deg[cnt2] = -1;
          }
          left[cnt2] = left[nd[j]];
          r = j;
          nd[j] = cnt2;
          break;
        }
      }
    }
  }
  int rt = nd[r];
  dfs1(rt);
  d[rt] = 1;
  ansl[rt] = 1;
  ansr[rt] = 1;
  anslr[rt] = 1;
  dfs2(rt);
  int q;
  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    long long ans = size[rt];
    l--;
    r++;
    if (l > 0) {
      ans -= ansl[l];
    }
    if (r <= n) {
      ans -= ansr[r];
    }
    if (l > 0 && r <= n) {
      int u = lca(l, r);
      ans += anslr[u];
      if (deg[u] > 0) {
        for (int i = 19; i >= 0; i--) {
          if (d[anc[l][i]] > d[u]) {
            l = anc[l][i];
          }
          if (d[anc[r][i]] > d[u]) {
            r = anc[r][i];
          }
        }
        ans += (long long)(deg[u] - rank[l] + 1) * rank[r] - 1;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}

```