#include <bits/stdc++.h>   using namespace std;   const int maxn = 12e4 + 5;   struct node {     int minn, cnt, lazy, time;     long long sum;   } tree[maxn << 2];   int n, q;   int p[maxn], stk_min[maxn], stk_max[maxn], top_min, top_max;   long long ans[maxn];   pair<pair<int, int>, int> querys[maxn];   inline void add(int rt, int val) {     tree[rt].minn += val;     tree[rt].lazy += val;   }   inline void add_time(int rt, int val) {     tree[rt].time += val;     tree[rt].sum += 1ll * tree[rt].cnt * val;   }   void build(int rt, int l, int r) {     tree[rt].minn = l, tree[rt].cnt = 1;     if (l != r) {       int mid = l + r >> 1;       build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r);     }   }   inline void pushup(int rt) {     int ls = rt << 1, rs = rt << 1 | 1;     tree[rt].minn = min(tree[ls].minn, tree[rs].minn);     tree[rt].sum = tree[ls].sum + tree[rs].sum;     tree[rt].cnt = 0;     if (tree[rt].minn == tree[ls].minn) tree[rt].cnt += tree[ls].cnt;     if (tree[rt].minn == tree[rs].minn) tree[rt].cnt += tree[rs].cnt;   }   inline void pushdown(int rt) {     int ls = rt << 1, rs = rt << 1 | 1;     if (tree[rt].lazy) {       add(ls, tree[rt].lazy);       add(rs, tree[rt].lazy);       tree[rt].lazy = 0;     }     if (tree[rt].time) {       if (tree[rt].minn == tree[ls].minn) add_time(ls, tree[rt].time);       if (tree[rt].minn == tree[rs].minn) add_time(rs, tree[rt].time);       tree[rt].time = 0;     }   }   void update(int rt, int l, int r, int ql, int qr, int val) {     if (ql <= l && r <= qr) {       add(rt, val);     } else {       int mid = l + r >> 1;       pushdown(rt);       if (qr <= mid)         update(rt << 1, l, mid, ql, qr, val);       else if (ql > mid)         update(rt << 1 | 1, mid + 1, r, ql, qr, val);       else {         update(rt << 1, l, mid, ql, mid, val);         update(rt << 1 | 1, mid + 1, r, mid + 1, qr, val);       }       pushup(rt);     }   }   long long query(int rt, int l, int r, int ql, int qr) {     if (ql <= l && r <= qr) {       return tree[rt].sum;     } else {       int mid = l + r >> 1;       pushdown(rt);       if (qr <= mid)         return query(rt << 1, l, mid, ql, qr);       else if (ql > mid)         return query(rt << 1 | 1, mid + 1, r, ql, qr);       else         return query(rt << 1, l, mid, ql, mid) +                query(rt << 1 | 1, mid + 1, r, mid + 1, qr);     }   }   int main() {     ios::sync_with_stdio(false), cin.tie(nullptr);     cin >> n;     for (int i = 1; i <= n; i++) cin >> p[i];     cin >> q;     for (int i = 1; i <= q; i++) {       int l, r;       cin >> l >> r;       querys[i] = {{l, r}, i};     }     sort(querys + 1, querys + q + 1,          [](const pair<pair<int, int>, int> &a,             const pair<pair<int, int>, int> &b) {            return a.first.second < b.first.second;          });     build(1, 1, n);     for (int i = 1, j = 1; i <= n; i++) {       update(1, 1, n, 1, n, -1);       while (top_min && p[i] < p[stk_min[top_min]]) {         update(1, 1, n, stk_min[top_min - 1] + 1, stk_min[top_min],                p[stk_min[top_min]] - p[i]);         --top_min;       }       while (top_max && p[i] > p[stk_max[top_max]]) {         update(1, 1, n, stk_max[top_max - 1] + 1, stk_max[top_max],                p[i] - p[stk_max[top_max]]);         --top_max;       }       stk_min[++top_min] = i;       stk_max[++top_max] = i;       add_time(1, 1);       while (j <= q && querys[j].first.second == i) {         ans[querys[j].second] =             query(1, 1, n, querys[j].first.first, querys[j].first.second);         ++j;       }     }     for (int i = 1; i <= q; i++) cout << ans[i] <<   n ;     return 0;   }