天天看点

Luogu P5142 区间方差

/**
 * @Date:   2019-03-31T19:57:45+08:00
 * @Last modified time: 2019-03-31T19:57:45+08:00
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
struct node {
    int l, r;
    ll w, ml;
}tree[A];
int n, m, opt, a;
ll b;
void build(int k, int l, int r) {
    tree[k].l = l; tree[k].r = r;
    if (l == r) {
        scanf("%lld", &tree[k].w); tree[k].w %= mod;
        tree[k].ml = tree[k].w * tree[k].w % mod;
        return;
    }
    int m = (l + r) >> 1;
    build(k << 1, l, m);
    build(k << 1 | 1, m + 1, r);
    tree[k].w = (tree[k << 1].w + tree[k << 1 | 1].w) % mod;
    tree[k].ml = (tree[k << 1].ml + tree[k << 1 | 1].ml) % mod;
}
void change(int k, int pos, ll add) {
    if (tree[k].l == tree[k].r) {
        tree[k].w = add;
        tree[k].ml = add * add % mod;
        return;
    }
    int m = (tree[k].l + tree[k].r) >> 1;
    if (pos <= m) change(k << 1, pos, add);
    else change(k << 1 | 1, pos, add);
    tree[k].w = (tree[k << 1].w + tree[k << 1 | 1].w) % mod;
    tree[k].ml = (tree[k << 1].ml + tree[k << 1 | 1].ml) % mod;
}
ll askw(int k, int l, int r) {
    if (tree[k].l >= l and tree[k].r <= r) return tree[k].w % mod;
    int m = (tree[k].l + tree[k].r) >> 1; ll ans = 0;
    if (l <= m) ans += askw(k << 1, l, r), ans %= mod;
    if (r > m) ans += askw(k << 1 | 1, l, r), ans %= mod;
    return ans % mod;
}
ll askwf(int k, int l, int r) {
    if (tree[k].l >= l and tree[k].r <= r) return tree[k].ml % mod;
    int m = (tree[k].l + tree[k].r) >> 1; ll ans = 0;
    if (l <= m) ans += askwf(k << 1, l, r), ans %= mod;
    if (r > m) ans += askwf(k << 1 | 1, l, r), ans %= mod;
    return ans % mod;
}
ll fpow(ll a, ll b, ll ans = 1) {
    while (b) {
        if (b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}

int main(int argc, char const *argv[]) {
    scanf("%d%d", &n, &m); build(1, 1, n);
    while (m--) {
        scanf("%d%d%lld", &opt, &a, &b);
        if (opt == 1) change(1, a, b % mod);
        else {
            ll sum = askw(1, a, b) % mod, fsum = askwf(1, a, b) % mod;
            printf("%lld\n", (mod + mod + mod + fsum * fpow(b - a + 1, mod - 2) % mod -  sum * fpow(b - a + 1, mod - 2) % mod * sum % mod * fpow(b - a + 1, mod - 2) % mod) % mod);
        }
    }
    return 0;
}