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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <deque> #include <stack> #include <unordered_map> #include <unordered_set> #include <numeric> #include <iomanip> #include <functional> #define _fio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define pb(e) push_back(e) #define all(x) (x).begin(), (x).end() #define allr(x) (x).rbegin(), (x).rend() #define endl '\n'
using namespace std; using i64 = long long; using PII = pair<int, int>; const int INF = 0x3f3f3f3f; template <class Info, class Tag> struct LazySegmentTree { const int n; std::vector<Info> info; std::vector<Tag> tag; LazySegmentTree(int n) : n(n), info(4 << std::__lg(n)), tag(4 << std::__lg(n)) {} LazySegmentTree(std::vector<Info> init) : LazySegmentTree(init.size()) { std::function<void(int, int, int)> build = [&](int p, int l, int r) { if (r - l == 1) { info[p] = init[l]; return; } int m = (l + r) / 2; build(2 * p, l, m); build(2 * p + 1, m, r); pull(p); }; build(1, 0, n); } void pull(int p) { info[p] = info[2 * p] + info[2 * p + 1]; } void apply(int p, const Tag &v) { info[p].apply(v); tag[p].apply(v); } void push(int p) { apply(2 * p, tag[p]); apply(2 * p + 1, tag[p]); tag[p] = Tag(); } void modify(int p, int l, int r, int x, const Info &v) { if (r - l == 1) { info[p] = v; return; } int m = (l + r) / 2; push(p); if (x < m) { modify(2 * p, l, m, x, v); } else { modify(2 * p + 1, m, r, x, v); } pull(p); } void modify(int p, const Info &v) { modify(1, 0, n, p, v); } Info rangeQuery(int p, int l, int r, int x, int y) { if (l >= y || r <= x) { return Info(); } if (l >= x && r <= y) { return info[p]; } int m = (l + r) / 2; push(p); return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y); } Info rangeQuery(int l, int r) { return rangeQuery(1, 0, n, l, r); } void rangeApply(int p, int l, int r, int x, int y, const Tag &v) { if (l >= y || r <= x) { return; } if (l >= x && r <= y) { apply(p, v); return; } int m = (l + r) / 2; push(p); rangeApply(2 * p, l, m, x, y, v); rangeApply(2 * p + 1, m, r, x, y, v); pull(p); } void rangeApply(int l, int r, const Tag &v) { return rangeApply(1, 0, n, l, r, v); } void half(int p, int l, int r) { if (info[p].act == 0) { return; } if ((info[p].min + 1) / 2 == (info[p].max + 1) / 2) { apply(p, {-(info[p].min + 1) / 2}); return; } int m = (l + r) / 2; push(p); half(2 * p, l, m); half(2 * p + 1, m, r); pull(p); } void half() { half(1, 0, n); } };
constexpr i64 inf = 1E18;
struct Tag { i64 add = 0;
void apply(Tag t) { add += t.add; } };
struct Info { i64 min = inf; i64 max = -inf; i64 sum = 0; i64 act = 0;
void apply(Tag t) { min += t.add; max += t.add; sum += act * t.add; } };
Info operator+(Info a, Info b) { Info c; c.min = std::min(a.min, b.min); c.max = std::max(a.max, b.max); c.sum = a.sum + b.sum; c.act = a.act + b.act; return c; }
int main() { int n, m; cin >> n >> m; vector<Info> a(n); for (int i = 0; i < n; ++i) { cin >> a[i].sum; a[i].act = 1; a[i].max = a[i].min = a[i].sum; } LazySegmentTree<Info, Tag> sgt(a); while (m--) { int t, l, r, x; cin >> t >> l >> r; l--; if (t == 1) { cin >> x; sgt.rangeApply(l, r, {x}); } else { cout << sgt.rangeQuery(l, r).sum << endl; } } return 0; }
|