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
| #include <iostream> #include <cctype> #include <algorithm> #define maxn 500010 #define INF 2147483648 #define ll long long using namespace std;
int n, m, a[maxn];
#define lc i << 1 #define rc i << 1 | 1 struct Seg { ll v; int mx, tmx, smx, cmx, madd, tmadd, add, tadd; } T[maxn * 4]; inline void maintain(int i) { T[i].v = T[lc].v + T[rc].v; T[i].tmx = max(T[lc].tmx, T[rc].tmx); if (T[lc].mx > T[rc].mx) { T[i].mx = T[lc].mx; T[i].smx = max(T[lc].smx, T[rc].mx); T[i].cmx = T[lc].cmx; } else if (T[rc].mx > T[lc].mx) { T[i].mx = T[rc].mx; T[i].smx = max(T[lc].mx, T[rc].smx); T[i].cmx = T[rc].cmx; } else { T[i].mx = T[lc].mx; T[i].smx = max(T[lc].smx, T[rc].smx); T[i].cmx = T[lc].cmx + T[rc].cmx; } }
void build(int i, int l, int r) { if (l == r) return T[i] = { a[l], a[l], a[l], -INF, 1 }, void(); int m = l + r >> 1; build(lc, l, m); build(rc, m + 1, r); maintain(i); }
inline void Update(int i, int l, int r, int madd, int tmadd, int add, int tadd) { T[i].v += 1ll * madd * T[i].cmx + 1ll * add * (r - l + 1 - T[i].cmx); T[i].tmx = max(T[i].tmx, T[i].mx + tmadd); T[i].mx += madd; if (T[i].smx != -INF) T[i].smx += add; T[i].tmadd = max(T[i].tmadd, T[i].madd + tmadd); T[i].tadd = max(T[i].tadd, T[i].add + tadd); T[i].madd += madd; T[i].add += add; }
inline void pushdown(int i, int l, int r) { int &madd = T[i].madd, &tmadd = T[i].tmadd, &add = T[i].add, &tadd = T[i].tadd; int mx = max(T[lc].mx, T[rc].mx), m = l + r >> 1; if (T[lc].mx == mx) Update(lc, l, m, madd, tmadd, add, tadd); else Update(lc, l, m, add, tadd, add, tadd); if (T[rc].mx == mx) Update(rc, m + 1, r, madd, tmadd, add, tadd); else Update(rc, m + 1, r, add, tadd, add, tadd); madd = tmadd = add = tadd = 0; }
void update_v(int i, int l, int r, int L, int R, int v) { if (l > R || r < L) return ; if (L <= l && r <= R) return Update(i, l, r, v, v, v, v); int m = l + r >> 1; pushdown(i, l, r); update_v(lc, l, m, L, R, v); update_v(rc, m + 1, r, L, R, v); maintain(i); }
void update_min(int i, int l, int r, int L, int R, int v) { if (l > R || r < L || T[i].mx <= v) return ; if (L <= l && r <= R && T[i].smx < v) return Update(i, l, r, v - T[i].mx, v - T[i].mx, 0, 0); int m = l + r >> 1; pushdown(i, l, r); update_min(lc, l, m, L, R, v); update_min(rc, m + 1, r, L, R, v); maintain(i); }
ll query_v(int i, int l, int r, int L, int R) { if (l > R || r < L) return 0; if (L <= l && r <= R) return T[i].v; int m = l + r >> 1; pushdown(i, l, r); return query_v(lc, l, m, L, R) + query_v(rc, m + 1, r, L, R); }
int query_mx(int i, int l, int r, int L, int R) { if (l > R || r < L) return -INF; if (L <= l && r <= R) return T[i].mx; int m = l + r >> 1; pushdown(i, l, r); return max(query_mx(lc, l, m, L, R), query_mx(rc, m + 1, r, L, R)); }
int query_tmx(int i, int l, int r, int L, int R) { if (l > R || r < L) return -INF; if (L <= l && r <= R) return T[i].tmx; int m = l + r >> 1; pushdown(i, l, r); return max(query_tmx(lc, l, m, L, R), query_tmx(rc, m + 1, r, L, R)); }
inline void solve_1() { int x, y, z; cin >> x >> y >> z; update_v(1, 1, n, x, y, z); }
inline void solve_2() { int x, y, z; cin >> x >> y >> z; update_min(1, 1, n, x, y, z); }
inline void solve_3() { int x, y; cin >> x >> y; cout << query_v(1, 1, n, x, y) << "\n"; }
inline void solve_4() { int x, y; cin >> x >> y; cout << query_mx(1, 1, n, x, y) << "\n"; }
inline void solve_5() { int x, y; cin >> x >> y; cout << query_tmx(1, 1, n, x, y) << "\n"; }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; build(1, 1, n); for (int i = 1; i <= m; ++i) { int opt; cin >> opt; switch (opt) { case 1 : solve_1(); break; case 2 : solve_2(); break; case 3 : solve_3(); break; case 4 : solve_4(); break; case 5 : solve_5(); break; } } return 0; }
|