#include <bits/stdc++.h>
using namespace std;
const int N = 233333;
const int Blk = 444;
vector<int> vec[N];
int n, p[N], Q;
int l[N], r[N];
int q1[N], q2[N];
long long x[N], tg[N], cnt[N];
pair<long long, long long> mn[N], mt[N], val[N];
pair<long long, long long> operator+(pair<long long, long long> a,
                                     pair<long long, long long> b) {
  if (!a.second) return b;
  if (!b.second) return a;
  if (a.first < b.first) return a;
  if (a.first > b.first) return b;
  return pair<long long, long long>(a.first, a.second + b.second);
}
void pushdown(int be) {
  if (!tg[be] && !mt[be].second) return;
  mn[be] = pair<long long, long long>(8e18, 0);
  for (int j = be * Blk; j < be * Blk + Blk; j++) {
    val[j] =
        val[j] + pair<long long, long long>(mt[be].first + x[j], mt[be].second);
    x[j] += tg[be];
    mn[be] = mn[be] + pair<long long, long long>(x[j], 1);
  }
  mt[be] = pair<long long, long long>(0, 0);
  tg[be] = 0;
}
void bf(int be, int l, int r, int v) {
  for (int j = max(be * Blk, l); j < be * Blk + Blk && j <= r; j++) x[j] += v;
  mn[be] = pair<long long, long long>(8e18, 0);
  for (int i = be * Blk; i < be * Blk + Blk; i++)
    mn[be] = mn[be] + pair<long long, long long>(x[i], 1);
}
void chg(int l, int r, int x) {
  pushdown(l / Blk);
  bf(l / Blk, l, r, x);
  if (l / Blk != r / Blk) {
    pushdown(r / Blk);
    bf(r / Blk, l, r, x);
  }
  for (int i = (int)(l / Blk + 1); i <= (int)(r / Blk - 1); i++) tg[i] += x;
}
void init() {
  for (int i = (int)(0); i <= (int)(n / Blk); i++) {
    if (mn[i].first + tg[i] == -1) cnt[i] += mn[i].second;
    mt[i] = mt[i] + pair<long long, long long>(tg[i], 1);
  }
}
long long ask(int x) {
  int be = x / Blk;
  long long ans = 0;
  pushdown(be);
  for (int i = x; i / Blk == be; i++)
    ans += (val[i].first == -1 ? val[i].second : 0);
  for (int i = (int)(be + 1); i <= (int)(n / Blk); i++) ans += cnt[i];
  return ans;
}
long long ans[N];
int main() {
  scanf("%d", &n);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &p[i]);
  scanf("%d", &Q);
  for (int i = (int)(1); i <= (int)(Q); i++) {
    scanf("%d%d", &l[i], &r[i]);
    vec[r[i]].push_back(i);
  }
  int t1 = 0, t2 = 0;
  for (int i = (int)(1); i <= (int)(n); i++) {
    for (; t1 && p[q1[t1]] < p[i]; t1--)
      chg(q1[t1 - 1] + 1, q1[t1], p[i] - p[q1[t1]]);
    for (; t2 && p[q2[t2]] > p[i]; t2--)
      chg(q2[t2 - 1] + 1, q2[t2], p[q2[t2]] - p[i]);
    q1[++t1] = q2[++t2] = i;
    chg(1, i, -1);
    init();
    for (int j = (int)(0); j <= (int)(vec[i].size() - 1); j++)
      ans[vec[i][j]] = ask(l[vec[i][j]]);
  }
  for (int i = (int)(1); i <= (int)(Q); i++) printf("%lld\n", ans[i]);
}
