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
| #include <cstdio> #include <iostream> using namespace std;
int inline rd() { register char act = 0; register int f = 1, x = 0; while (act = getchar(), act < '0' && act != '-') ; if (act == '-') f = -1, act = getchar(); x = act - '0'; while (act = getchar(), act >= '0') x = x * 10 + act - '0'; return x * f; }
const int N = 5e5 + 5, SZ = N << 2, INF = 0x7fffffff;
int n, m; int a[N];
struct data { int mx, mx2, mn, mn2, cmx, cmn, tmx, tmn, tad; long long sum; } t[SZ];
void pushup(int u) { const int lu = u << 1, ru = u << 1 | 1; t[u].sum = t[lu].sum + t[ru].sum; if (t[lu].mx == t[ru].mx) { t[u].mx = t[lu].mx, t[u].cmx = t[lu].cmx + t[ru].cmx; t[u].mx2 = max(t[lu].mx2, t[ru].mx2); } else if (t[lu].mx > t[ru].mx) { t[u].mx = t[lu].mx, t[u].cmx = t[lu].cmx; t[u].mx2 = max(t[lu].mx2, t[ru].mx); } else { t[u].mx = t[ru].mx, t[u].cmx = t[ru].cmx; t[u].mx2 = max(t[lu].mx, t[ru].mx2); } if (t[lu].mn == t[ru].mn) { t[u].mn = t[lu].mn, t[u].cmn = t[lu].cmn + t[ru].cmn; t[u].mn2 = min(t[lu].mn2, t[ru].mn2); } else if (t[lu].mn < t[ru].mn) { t[u].mn = t[lu].mn, t[u].cmn = t[lu].cmn; t[u].mn2 = min(t[lu].mn2, t[ru].mn); } else { t[u].mn = t[ru].mn, t[u].cmn = t[ru].cmn; t[u].mn2 = min(t[lu].mn, t[ru].mn2); } } void push_add(int u, int l, int r, int v) { t[u].sum += (r - l + 1ll) * v; t[u].mx += v, t[u].mn += v; if (t[u].mx2 != -INF) t[u].mx2 += v; if (t[u].mn2 != INF) t[u].mn2 += v; if (t[u].tmx != -INF) t[u].tmx += v; if (t[u].tmn != INF) t[u].tmn += v; t[u].tad += v; } void push_min(int u, int tg) { if (t[u].mx <= tg) return; t[u].sum += (tg * 1ll - t[u].mx) * t[u].cmx; if (t[u].mn2 == t[u].mx) t[u].mn2 = tg; if (t[u].mn == t[u].mx) t[u].mn = tg; if (t[u].tmx > tg) t[u].tmx = tg; t[u].mx = tg, t[u].tmn = tg; } void push_max(int u, int tg) { if (t[u].mn > tg) return; t[u].sum += (tg * 1ll - t[u].mn) * t[u].cmn; if (t[u].mx2 == t[u].mn) t[u].mx2 = tg; if (t[u].mx == t[u].mn) t[u].mx = tg; if (t[u].tmn < tg) t[u].tmn = tg; t[u].mn = tg, t[u].tmx = tg; } void pushdown(int u, int l, int r) { const int lu = u << 1, ru = u << 1 | 1, mid = (l + r) >> 1; if (t[u].tad) push_add(lu, l, mid, t[u].tad), push_add(ru, mid + 1, r, t[u].tad); if (t[u].tmx != -INF) push_max(lu, t[u].tmx), push_max(ru, t[u].tmx); if (t[u].tmn != INF) push_min(lu, t[u].tmn), push_min(ru, t[u].tmn); t[u].tad = 0, t[u].tmx = -INF, t[u].tmn = INF; } void build(int u = 1, int l = 1, int r = n) { t[u].tmn = INF, t[u].tmx = -INF; if (l == r) { t[u].sum = t[u].mx = t[u].mn = a[l]; t[u].mx2 = -INF, t[u].mn2 = INF; t[u].cmx = t[u].cmn = 1; return; } int mid = (l + r) >> 1; build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r); pushup(u); } void add(int L, int R, int v, int u = 1, int l = 1, int r = n) { if (R < l || r < L) return; if (L <= l && r <= R) return push_add(u, l, r, v); int mid = (l + r) >> 1; pushdown(u, l, r); add(L, R, v, u << 1, l, mid), add(L, R, v, u << 1 | 1, mid + 1, r); pushup(u); } void tomin(int L, int R, int v, int u = 1, int l = 1, int r = n) { if (R < l || r < L || t[u].mx <= v) return; if (L <= l && r <= R && t[u].mx2 < v) return push_min(u, v); int mid = (l + r) >> 1; pushdown(u, l, r); tomin(L, R, v, u << 1, l, mid), tomin(L, R, v, u << 1 | 1, mid + 1, r); pushup(u); } void tomax(int L, int R, int v, int u = 1, int l = 1, int r = n) { if (R < l || r < L || t[u].mn >= v) return; if (L <= l && r <= R && t[u].mn2 > v) return push_max(u, v); int mid = (l + r) >> 1; pushdown(u, l, r); tomax(L, R, v, u << 1, l, mid), tomax(L, R, v, u << 1 | 1, mid + 1, r); pushup(u); } long long qsum(int L, int R, int u = 1, int l = 1, int r = n) { if (R < l || r < L) return 0; if (L <= l && r <= R) return t[u].sum; int mid = (l + r) >> 1; pushdown(u, l, r); return qsum(L, R, u << 1, l, mid) + qsum(L, R, u << 1 | 1, mid + 1, r); } long long qmax(int L, int R, int u = 1, int l = 1, int r = n) { if (R < l || r < L) return -INF; if (L <= l && r <= R) return t[u].mx; int mid = (l + r) >> 1; pushdown(u, l, r); return max(qmax(L, R, u << 1, l, mid), qmax(L, R, u << 1 | 1, mid + 1, r)); } long long qmin(int L, int R, int u = 1, int l = 1, int r = n) { if (R < l || r < L) return INF; if (L <= l && r <= R) return t[u].mn; int mid = (l + r) >> 1; pushdown(u, l, r); return min(qmin(L, R, u << 1, l, mid), qmin(L, R, u << 1 | 1, mid + 1, r)); } int main() { n = rd(); for (int i = 1; i <= n; i++) a[i] = rd(); build(); m = rd(); for (int i = 1; i <= m; i++) { int op, l, r, x; op = rd(), l = rd(), r = rd(); if (op <= 3) x = rd(); if (op == 1) add(l, r, x); else if (op == 2) tomax(l, r, x); else if (op == 3) tomin(l, r, x); else if (op == 4) printf("%lld\n", qsum(l, r)); else if (op == 5) printf("%lld\n", qmax(l, r)); else printf("%lld\n", qmin(l, r)); } return 0; }
|