标签:
问题描述
1 #include <bits/stdc++.h> 2 #define rep(i, a, b) for (int i = a; i <= b; i++) 3 #define drep(i, a, b) for (int i = a; i >= b; i--) 4 #define REP(i, a, b) for (int i = a; i < b; i++) 5 #define mp make_pair 6 #define pb push_back 7 #define clr(x) memset(x, 0, sizeof(x)) 8 #define xx first 9 #define yy second 10 using namespace std; 11 typedef long long i64; 12 typedef pair<int, int> pii; 13 const int inf = ~0U >> 1; 14 const i64 INF = ~0ULL >> 1; 15 //******************************* 16 17 const int maxn = 500005; 18 19 struct node { 20 double p; 21 double a21, a22, a31, a32; 22 double mul1; 23 node() {} 24 node(double _p, double _a21, double _a22, double _a31, double _a32, double _mul1) : 25 p(_p), a21(_a21), a22(_a22), a31(_a31), a32(_a32), mul1(_mul1) {} 26 27 }; 28 29 double ans0, ans1, ans3[3]; 30 31 /* the matrix 32 {sum[i - 1], E[i - 1], 1} * {1 , 0 , 0} = {sum[i], E[i], 1} 33 {2pi, pi, 0} 34 {pi , pi, 1} 35 */ 36 37 double pi[maxn]; 38 39 struct Seg_Tree { 40 node T[maxn << 2]; 41 42 void Push_up(int o) { 43 T[o].p = T[o << 1].p + T[o << 1 | 1].p, T[o].mul1 = T[o << 1].mul1 + T[o << 1 | 1].mul1; 44 T[o].a21 = T[o << 1].a21 + T[o << 1].a22 * T[o << 1 | 1].a21; 45 T[o].a22 = T[o << 1].a22 * T[o << 1| 1].a22; 46 T[o].a31 = T[o << 1].a31 + T[o << 1].a32 * T[o << 1 | 1].a21 + T[o << 1 | 1].a31; 47 T[o].a32 = T[o << 1].a32 * T[o << 1 | 1].a22 + T[o << 1 | 1].a32; 48 } 49 50 void update(int o, int l, int r, int x, double p) { 51 if (l == r) { 52 T[o] = node(p, 2 * p, p, p, p, (1 - pi[l - 1]) * p); 53 return; 54 } 55 int mid = l + r >> 1; 56 if (x > mid) update(o << 1 | 1, mid + 1, r, x, p); 57 else update(o << 1, l, mid, x, p); 58 Push_up(o); 59 } 60 61 void query(int o, int l, int r, int ql, int qr) { 62 if (ql <= l && r <= qr) { 63 ans1 += T[o].p; 64 ans0 += T[o].mul1; 65 ans3[1] = ans3[1] + ans3[2] * T[o].a21 + T[o].a31; 66 ans3[2] = ans3[2] * T[o].a22 + T[o].a32; 67 return; 68 } 69 int mid = l + r >> 1; 70 if (ql <= mid) query(o << 1, l, mid, ql, qr); 71 if (qr > mid) query(o << 1 | 1, mid + 1, r, ql, qr); 72 } 73 } T; 74 75 int main() { 76 int n, m; 77 scanf("%d%d", &n, &m); 78 rep(i, 1, n) scanf("%lf", &pi[i]), T.update(1, 1, n, i, pi[i]); 79 while (m--) { 80 int op; 81 scanf("%d", &op); 82 if (!op) { 83 int x, y; scanf("%d%d", &x, &y); 84 ans1 = 0, ans0 = 0, ans3[1] = ans3[2] = 0; 85 T.query(1, 1, n, x, y); 86 printf("%.2lf\n", ans1 + ans0 + pi[x - 1] * pi[x] + ans3[1]); 87 } 88 else { 89 double w; int x; 90 scanf("%d%lf", &x, &w); 91 pi[x] = w, T.update(1, 1, n, x, w); 92 if (x != n) T.update(1, 1, n, x + 1, pi[x + 1]); 93 } 94 } 95 return 0; 96 }
标签:
原文地址:http://www.cnblogs.com/y7070/p/5042928.html