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
| #include <cstdio> using namespace std; const int N = 1e5 + 10; int n, m; double w[N]; struct Node { int l, r; double s, s2, tag; } tr[N << 2]; void cal(Node &x, double k) { x.s2 += 2 * k * x.s + (x.r - x.l + 1) * k * k; x.s += (x.r - x.l + 1) * k; x.tag += k; } void pushup(Node &x, Node l, Node r) { x.l = l.l, x.r = r.r; x.s = l.s + r.s; x.s2 = l.s2 + r.s2; } void pushdown(int x) { cal(tr[x << 1], tr[x].tag); cal(tr[x << 1 | 1], tr[x].tag); tr[x].tag = 0; } void build(int x, int l, int r) { if (l == r) { tr[x].l = tr[x].r = l; tr[x].s = w[l]; tr[x].s2 = w[l] * w[l]; return; } int mid = l + r >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); pushup(tr[x], tr[x << 1], tr[x << 1 | 1]); } void modify(int x, int l, int r, double k) { if (tr[x].l >= l && tr[x].r <= r) { cal(tr[x], k); return; } if (tr[x].tag) pushdown(x); int mid = tr[x].l + tr[x].r >> 1; if (l <= mid) modify(x << 1, l, r, k); if (r > mid) modify(x << 1 | 1, l, r, k); pushup(tr[x], tr[x << 1], tr[x << 1 | 1]); } Node query(int x, int l, int r) { if (tr[x].l >= l && tr[x].r <= r) return tr[x]; if (tr[x].tag) pushdown(x); int mid = tr[x].l + tr[x].r >> 1; if (r <= mid) return query(x << 1, l, r); if (l > mid) return query(x << 1 | 1, l, r); Node res; pushup(res, query(x << 1, l, r), query(x << 1 | 1, l, r)); return res; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lf", &w[i]); build(1, 1, n); for (int op, l, r; m; m--) { scanf("%d%d%d", &op, &l, &r); if (op == 1) { double k; scanf("%lf", &k); modify(1, l, r, k); continue; } Node t = query(1, l, r); double avrg = (double)t.s / (t.r - t.l + 1); if (op == 2) printf("%.4lf\n", avrg); else printf("%.4lf\n", (double)t.s2 / (t.r - t.l + 1) - avrg * avrg); } return 0; }
|