1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| #include <cstdio> #include <vector> #include <algorithm> using namespace std; typedef long long LL; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } const int N = 12e4 + 10; int n, m, w[N]; LL ans[N]; struct Query { int l, id; }; vector <Query> q[N]; struct Node { int l, r, tag, mn, cnt, times; LL ans; } tr[N << 2]; void pushup (int x) { tr[x].mn = min(tr[x << 1].mn, tr[x << 1 | 1].mn); tr[x].cnt = 0; tr[x << 1].mn == tr[x].mn && (tr[x].cnt += tr[x << 1].cnt); tr[x << 1 | 1].mn == tr[x].mn && (tr[x].cnt += tr[x << 1 | 1].cnt); tr[x].ans = tr[x << 1].ans + tr[x << 1 | 1].ans; } void add_mn (int x, int k) { tr[x].mn += k, tr[x].tag += k; } void add_times (int x, int k) { tr[x].ans += (LL)k * tr[x].cnt, tr[x].times += k; } void pushdown (int x) { add_mn(x << 1, tr[x].tag), add_mn(x << 1 | 1, tr[x].tag); tr[x].tag = 0; tr[x].mn == tr[x << 1].mn && (add_times(x << 1, tr[x].times), 0); tr[x].mn == tr[x << 1 | 1].mn && (add_times(x << 1 | 1, tr[x].times), 0); tr[x].times = 0; } void build (int l = 1, int r = n, int x = 1) { tr[x].l = l, tr[x].r = r; if (l == r) return tr[x].mn = l, tr[x].cnt = 1, void(); int mid = l + r >> 1; build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1); pushup(x); } void modify (int l, int r, int k, int x = 1) { if (l > r || tr[x].l > r || l > tr[x].r) return; if (tr[x].l >= l && tr[x].r <= r) return add_mn(x, k); pushdown(x); modify(l, r, k, x << 1), modify(l, r, k, x << 1 | 1); pushup(x); } LL query (int l, int r, int x = 1) { if (l > tr[x].r || tr[x].l > r) return 0; if (tr[x].l >= l && tr[x].r <= r) return tr[x].ans; pushdown(x); return query(l, r, x << 1) + query(l, r, x << 1 | 1); } int top1, stk1[N], top2, stk2[N]; int main () { read(n); for (int i = 1; i <= n; i++) read(w[i]); build(); read(m); for (int i = 1, l, r; i <= m; i++) { read(l), read(r); q[r].push_back((Query){l, i}); } for (int i = 1; i <= n; i++) { while (top1 && w[stk1[top1]] > w[i]) modify(stk1[top1 - 1] + 1, stk1[top1], w[stk1[top1]]), top1--; modify(stk1[top1] + 1, i, -w[i]); stk1[++top1] = i; while (top2 && w[stk2[top2]] < w[i]) modify(stk2[top2 - 1] + 1, stk2[top2], -w[stk2[top2]]), top2--; modify(stk2[top2] + 1, i, w[i]); stk2[++top2] = i; modify(1, n, -1); add_times(1, 1); for (Query j : q[i]) ans[j.id] = query(j.l, i); } for (int i = 1; i <= m; i++) write(ans[i]), puts(""); return 0; }
|