func_code_string: #include <bits/stdc++.h>   const int N = 120005;   int n, p[N], m;   int stack1[N], top1, stack2[N], top2;   struct query {     int l, r, id;   } q[N];   bool operator<(query a, query b) { return a.r < b.r; }   int min[N << 2], num[N << 2], tag0[N << 2], tag1[N << 2];   long long sum[N << 2], ans[N];   void build(int o = 1, int l = 1, int r = n) {     min[o] = l, num[o] = 1;     if (l == r) return;     int mid = l + r >> 1;     build(o << 1, l, mid), build(o << 1 | 1, mid + 1, r);   }   void settag0(int o, int v) { tag0[o] += v, min[o] += v; }   void settag1(int o, int v) { sum[o] += (long long)v * num[o], tag1[o] += v; }   void pushdown(int o) {     if (tag0[o])       settag0(o << 1, tag0[o]), settag0(o << 1 | 1, tag0[o]), tag0[o] = 0;     if (tag1[o]) {       if (min[o] == min[o << 1]) settag1(o << 1, tag1[o]);       if (min[o] == min[o << 1 | 1]) settag1(o << 1 | 1, tag1[o]);       tag1[o] = 0;     }   }   void pushup(int o) {     min[o] = std::min(min[o << 1], min[o << 1 | 1]);     num[o] = 0;     if (min[o] == min[o << 1]) num[o] += num[o << 1];     if (min[o] == min[o << 1 | 1]) num[o] += num[o << 1 | 1];     sum[o] = sum[o << 1] + sum[o << 1 | 1];   }   void modify(int L, int R, int v, int o = 1, int l = 1, int r = n) {     if (L <= l && R >= r) return settag0(o, v);     int mid = l + r >> 1;     pushdown(o);     if (L <= mid) modify(L, R, v, o << 1, l, mid);     if (R > mid) modify(L, R, v, o << 1 | 1, mid + 1, r);     pushup(o);   }   long long query(int L, int o = 1, int l = 1, int r = n) {     if (L == l) return sum[o];     int mid = l + r >> 1;     pushdown(o);     if (L <= mid)       return query(L, o << 1, l, mid) + sum[o << 1 | 1];     else       return query(L, o << 1 | 1, mid + 1, r);   }   int main() {     std::ios::sync_with_stdio(0), std::cin.tie(0);     std::cin >> n;     for (int i = 1; i <= n; ++i) std::cin >> p[i];     std::cin >> m;     for (int i = 0; i < m; ++i) std::cin >> q[i].l >> q[i].r, q[i].id = i;     build(), std::sort(q, q + m);     int t = 0;     for (int i = 1; i <= n; ++i) {       while (top1 && p[stack1[top1]] < p[i])         modify(stack1[top1 - 1] + 1, stack1[top1], p[i] - p[stack1[top1]]),             --top1;       while (top2 && p[stack2[top2]] > p[i])         modify(stack2[top2 - 1] + 1, stack2[top2], p[stack2[top2]] - p[i]),             --top2;       stack1[++top1] = stack2[++top2] = i, settag1(1, 1);       while (t < m && q[t].r == i) ans[q[t].id] = query(q[t].l), ++t;     }     for (int i = 0; i < m; ++i) std::cout << ans[i] <<   n ;     return 0;   }   