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
| #include <iostream> using namespace std; using LL = long long; const int N = 100001; int n, m, t, l, r, x, a[N]; struct Node { int l, r, maxn; LL sum; } tr[N << 2]; void pushup(int u) { tr[u].maxn = max(tr[u << 1].maxn, tr[u << 1 | 1].maxn); tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum; } void build(int u, int l, int r) { tr[u] = {l, r}; if (l == r) { tr[u].sum = a[l], tr[u].maxn = a[l]; return; } int mid = l + r >> 1; build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r); pushup(u); } void modify(int u, int l, int r, int x) { if (tr[u].maxn < x) return; if (tr[u].l == tr[u].r) { tr[u].maxn %= x, tr[u].sum = tr[u].maxn; return; } int mid = tr[u].l + tr[u].r >> 1; if (l <= mid) modify(u << 1, l, r, x); if (r > mid) modify(u << 1 | 1, l, r, x); pushup(u); } void modify(int u, int k, int v) { if (tr[u].l == tr[u].r) { tr[u].maxn = tr[u].sum = v; return; } int mid = tr[u].l + tr[u].r >> 1; if (k <= mid) modify(u << 1, k, v); else modify(u << 1 | 1, k, v); pushup(u); } LL query(int u, int l, int r) { if (l <= tr[u].l && tr[u].r <= r) return tr[u].sum; LL ans = 0; int mid = tr[u].l + tr[u].r >> 1; if (l <= mid) ans = query(u << 1, l, r); if (r > mid) ans += query(u << 1 | 1, l, r); return ans; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); build(1, 1, n); while (m--) { scanf("%d", &t); if (t == 1) { scanf("%d%d", &l, &r); printf("%lld\n", query(1, l, r)); } else if (t == 2) { scanf("%d%d%d", &l, &r, &x); modify(1, l, r, x); } else { scanf("%d%d", &l, &x); modify(1, l, x); } } return 0; }
|