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
| #include <iostream> #define maxn 1000010 #define ll long long using namespace std;
int n, m, a[maxn];
#define lc i << 1 #define rc i << 1 | 1 struct Seg { int mx, se, t; ll v; } T[maxn * 4]; inline void maintain(int i) { T[i] = T[lc]; if (T[rc].mx > T[i].mx) T[i] = T[rc], T[i].se = max(T[lc].mx, T[rc].se); else if (T[rc].mx == T[i].mx) T[i].t += T[rc].t, T[i].se = max(T[lc].se, T[rc].se); else T[i].se = max(T[lc].se, T[rc].mx); T[i].v = T[lc].v + T[rc].v; }
void build(int i, int l, int r) { if (l == r) return T[i] = { a[l], -1, 1, a[l] }, void(); int m = l + r >> 1; build(lc, l, m); build(rc, m + 1, r); maintain(i); }
inline void Update(int i, int v) { T[i].v -= 1ll * T[i].t * (T[i].mx - v); T[i].mx = v; }
inline void pushdown(int i) { if (T[lc].mx > T[i].mx) Update(lc, T[i].mx); if (T[rc].mx > T[i].mx) Update(rc, T[i].mx); }
void update(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 && v > T[i].se) return Update(i, v); int m = l + r >> 1; pushdown(i); update(lc, l, m, L, R, v); update(rc, m + 1, r, L, R, v); maintain(i); }
int query_mx(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].mx; int m = l + r >> 1; pushdown(i); return max(query_mx(lc, l, m, L, R), query_mx(rc, m + 1, r, L, R)); }
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); return query_v(lc, l, m, L, R) + query_v(rc, m + 1, r, L, R); }
inline void solve_0() { int x, y, z; cin >> x >> y >> z; update(1, 1, n, x, y, z); }
inline void solve_1() { int x, y; cin >> x >> y; cout << query_mx(1, 1, n, x, y) << "\n"; }
inline void solve_2() { int x, y; cin >> x >> y; cout << query_v(1, 1, n, x, y) << "\n"; }
void work() { 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 0 : solve_0(); break; case 1 : solve_1(); break; case 2 : solve_2(); break; } } }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T; cin >> T; while (T--) work(); return 0; }
|