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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define fi first #define se second #define maxn 200010 #define maxm 200010 #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, r, l) for (int i = r; i >= l; i--) #define ll long long using namespace std; const int inf = 0x7fffffff; int read() { int p = 0, f = 1; char c = getchar(); while (c < 48 || c > 57) { if (c == '-') f = -1; c = getchar(); } while (c >= 48 && c <= 57) p = (p << 1) + (p << 3) + (c ^ 48), c = getchar(); return p * f; } ll sum(ll x) { return x * (x + 1) / 2; } int seg[maxn << 2][4]; int a[maxn]; void pushup(int rt, int l, int r) { int m = (l + r) >> 1; if (seg[rt << 1][3] && seg[rt << 1 | 1][3]) { if (a[m] > a[m + 1]) { seg[rt][0] = 0; seg[rt][1] = m - l + 1; seg[rt][2] = r - m; seg[rt][3] = 0; } else { seg[rt][1] = seg[rt][2] = seg[rt][0] = 0; seg[rt][3] = 1; } } else if (seg[rt << 1][3]) { if (a[m] > a[m + 1]) { seg[rt][0] = seg[rt << 1 | 1][0] + sum(seg[rt << 1 | 1][1]); seg[rt][1] = m - l + 1; seg[rt][2] = seg[rt << 1 | 1][2]; seg[rt][3] = 0; } else { seg[rt][0] = seg[rt << 1 | 1][0]; seg[rt][1] = m - l + 1 + seg[rt << 1 | 1][1]; seg[rt][2] = seg[rt << 1 | 1][2]; seg[rt][3] = 0; } } else if (seg[rt << 1 | 1][3]) { if (a[m] > a[m + 1]) { seg[rt][0] = seg[rt << 1][0] + sum(seg[rt << 1][2]); seg[rt][1] = seg[rt << 1][1]; seg[rt][2] = r - m; seg[rt][3] = 0; } else { seg[rt][0] = seg[rt << 1][0]; seg[rt][1] = seg[rt << 1][1]; seg[rt][2] = r - m + seg[rt << 1][2]; seg[rt][3] = 0; } } else { if (a[m] > a[m + 1]) { seg[rt][0] = seg[rt << 1][0] + seg[rt << 1 | 1][0] + sum(seg[rt << 1][2]) + sum(seg[rt << 1 | 1][1]); seg[rt][1] = seg[rt << 1][1]; seg[rt][2] = seg[rt << 1 | 1][2]; seg[rt][3] = 0; } else { seg[rt][0] = seg[rt << 1][0] + seg[rt << 1 | 1][0] + sum(seg[rt << 1][2] + seg[rt << 1 | 1][1]); seg[rt][1] = seg[rt << 1][1]; seg[rt][2] = seg[rt << 1 | 1][2]; seg[rt][3] = 0; } } } void build(int rt, int l, int r) { if (l == r) { seg[rt][3] = 1; return; } int m = (l + r) >> 1; build(rt << 1, l, m); build(rt << 1 | 1, m + 1, r); pushup(rt, l, r); } void update(int rt, int l, int r, int x, int y) { if (l == r) { a[x] = y; return; } int m = (l + r) >> 1; if (x <= m) update(rt << 1, l, m, x, y); else update(rt << 1 | 1, m + 1, r, x, y); pushup(rt, l, r); } ll k, ans; void query(int rt, int l, int r, int ql, int qr) { if (r < ql || qr < l) return; if (ql <= l && r <= qr) { if (seg[rt][3]) { if (a[l - 1] > a[l]) { ans += sum(k); k = r - l + 1; } else k += r - l + 1; } else { if (a[l - 1] > a[l]) ans += seg[rt][0] + sum(k) + sum(seg[rt][1]); else ans += seg[rt][0] + sum(k + seg[rt][1]); k = seg[rt][2]; } return; } int m = (l + r) >> 1; query(rt << 1, l, m, ql, qr); query(rt << 1 | 1, m + 1, r, ql, qr); } int main() { int n = read(), q = read(); rep(i, 1, n) a[i] = read(); build(1, 1, n); while (q--) { int opt = read(), l = read(), r = read(); if (opt == 1) update(1, 1, n, l, r); else { ans = k = 0; query(1, 1, n, l, r); printf("%lld\n", ans + sum(k)); } } return 0; }
|