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
| #include <iostream> #define maxn 200010 #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 t, cov; } T[maxn * 4]; inline void maintain(int i) { T[i].t = T[rc].t; T[i].v = T[lc].v + T[rc].v; } void build(int i, int l, int r) { T[i].cov = -1; if (l == r) return T[i].v = T[i].t = 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 l, int r, int v) { T[i].v = 1ll * (r - l + 1) * v; T[i].t = v; T[i].cov = v; } inline void pushdown(int i, int l, int r) { int &cov = T[i].cov, m = l + r >> 1; if (cov == -1) return ; Update(lc, l, m, cov); Update(rc, m + 1, r, cov); cov = -1; } int query(int i, int l, int r, int v) { if (T[i].t > v) return 0; if (l == r) return l; pushdown(i, l, r); int m = l + r >> 1, ls = query(lc, l, m, v); if (ls) return ls; else return query(rc, m + 1, r, v); } void update(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); int m = l + r >> 1; pushdown(i, l, r); update(lc, l, m, L, R, v); update(rc, m + 1, r, L, R, v); maintain(i); } int query(int i, int l, int r, int L, int R, int &rmx, int &v) { if (l > R || r < L) return 0; if (L <= l && r <= R) { if (T[i].v <= v) return v -= T[i].v, rmx = r, r - l + 1; else if (T[i].t > v) return 0; } pushdown(i, l, r); int m = l + r >> 1, ls = query(lc, l, m, L, R, rmx, v); if (rmx == L || rmx == m) return ls + query(rc, m + 1, r, L, R, rmx, v); else return ls; }
inline void solve_1() { int x, y; cin >> x >> y; int k = query(1, 1, n, y); if (1 <= k && k <= x) update(1, 1, n, k, x, y); }
inline void solve_2() { int x, y, ans = 0; cin >> x >> y; while (y && x <= n) { int t = query(1, 1, n, x, n, x, y); if (!t) break; ans += t; ++x; } cout << ans << "\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; if (opt == 1) solve_1(); else solve_2(); } return 0; }
|