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 99 100
| #include<cstdio>
using namespace std; typedef long long ll;
constexpr int mod = 1e9+7;
int N, M, op, x, y, s1, s2, inv, ave, ans, z[100003];
int qpow(int b, int p = mod - 2, int m = mod) { b %= m; int s = 1 % m; for(; p; p >>= 1, b = (ll)b * b % m) if(p & 1) s = (ll)s * b % m; return s; }
namespace SGT { #define ls p<<1 #define rs p<<1|1 #define sr(x) ((ll)(x)*(x)%mod)
int L[400003], R[400003], s1[4000003], s2[400003];
inline void pushup(int p) { s1[p] = (s1[ls] + s1[rs]) % mod; s2[p] = (s2[ls] + s2[rs]) % mod; }
void build(int p, int l, int r) { L[p] = l, R[p] = r; if(l == r) { s1[p] = z[l] % mod; s2[p] = sr(z[l]) % mod; return; } int m = (l + r) >> 1; build(ls, l, m); build(rs, m + 1, r); pushup(p); }
void modify(int p, int k, int v) { if(L[p] == R[p]) { s1[p] = v % mod; s2[p] = sr(v) % mod; return; } int m = (L[p] + R[p]) >> 1; if(k <= m) modify(ls, k, v); else modify(rs, k, v); pushup(p); }
int query1(int p, int l, int r) { if(l == L[p] && r == R[p]) return s1[p] % mod; int m = (L[p] + R[p]) >> 1; if(r <= m) return query1(ls, l, r) % mod; if(l > m) return query1(rs, l, r) % mod; return (query1(ls, l, m) + query1(rs, m + 1, r)) % mod; }
int query2(int p, int l, int r) { if(l == L[p] && r == R[p]) return s2[p] % mod; int m = (L[p] + R[p]) >> 1; if(r <= m) return query2(ls, l, r) % mod; if(l > m) return query2(rs, l, r) % mod; return (query2(ls, l, m) + query2(rs, m + 1, r)) % mod; } }
int main() { scanf("%d%d", &N, &M); for(int i = 1; i <= N; ++i) scanf("%d", &z[i]); SGT::build(1, 1, N); while(M--) { scanf("%d%d%d", &op, &x, &y); if(op == 1) SGT::modify(1, x, y % mod); else { s1 = SGT::query1(1, x, y) % mod; s2 = SGT::query2(1, x, y) % mod; inv = qpow(y - x + 1); ave = (ll)s1 * inv % mod; ans = (ll)s2 * inv % mod - (ll)ave * ave % mod; ans = (ans % mod + mod) % mod; printf("%d\n", ans); } } return 0; }
|