P5142 区间方差

· · 题解

简要题意

对于一个给定的序列,有两个操作:

然后我们考虑数据结构,但是我们太蒟蒻了,所以我们考虑一种暴力的思路:分块!!!

学过分块的都知道我们在区间修改时会出现两种情况:

但是这道题是:单点修改!!!

所以我们的修改操作就变简单了,对于区间和结果的影响就是 y-a_x,直接加上就行。然后就是平方和,也是一样的,对于原式子套上一个平方差就行。

我们难点在于区间查询,我们分两个查询一个 query1 一个 query2,这两个一个查区间和一个查平方和。

而且众所周知,查询就是反过来的区间修改,由于我们没有打懒标记所以这题的查询相对简单了许多。

依旧分情况:散块就暴力,整块直接用求好的就行。

这样我们就可以很快的写出 query1函数:

ll query1(int l,int r){//区间和
    ll ans = 0;
    if(pos[l] == pos[r])for(int i = l;i <= r;++i)ans = (ans + a[i]) % mod;//同属于一个块暴力查
    else{
        for(int i = l;i <= br[pos[l]];++i)ans = (ans + a[i]) % mod;//左散块的暴力
        for(int i = pos[l] + 1;i <= pos[r] - 1;++i)ans = (ans + s1[i]) % mod;//整块直接用求好的
        for(int i = bl[pos[r]];i <= r;++i)ans = (ans + a[i]) % mod;//右散块的暴力
    }
    return ans;
}

然后就是 query2,和 query1 一样,我们只需要把里边的式子稍微一改就行:

ll query2(int l,int r){//区间平方和
    ll ans = 0;
    if(pos[l] == pos[r])for(int i = l;i <= r;++i)ans = (ans + a[i] * a[i] % mod) % mod;
    else{
        for(int i = l;i <= br[pos[l]];++i)ans = (ans + a[i] * a[i]) % mod;
        for(int i = pos[l] + 1;i <= pos[r] - 1;++i)ans = (ans + s2[i]) % mod;
        for(int i = bl[pos[r]];i <= r;++i)ans = (ans + a[i] * a[i]) % mod;
    }
    return ans;
}

这样就写好了两个查询的函数,下面我们来看看修改部分,也不难就像我刚才说的,来看:

int p = pos[x];//求出来x属于哪个块
s1[p] = (s1[p] - a[x] + y) % mod;//单点修改的区间和
if (s1[p] < 0) s1[p] += mod;//防止取模出问题
s2[p] = (s2[p] - a[x] * a[x] % mod + y * y % mod) % mod;//单点修改的区间平方和
if (s2[p] < 0) s2[p] += mod;//防止取模出问题
a[x] = y;//赋值修改

下面是完整代码:

#include <bits/stdc++.h>
#define ll long long
#define IOS ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
const int N = 2e5 + 10;
const int BLOCK = 600;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll ksm(ll x,ll y){//费马小定理求逆元
    ll res = 1;
    while(y){
        if(y & 1)res = res * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return res;
}
int n,m;
ll a[N];
ll len;// 块长
ll bl[BLOCK],br[BLOCK];//每个块的左边界和右边界
int pos[N];//每个点的块编号
ll s1[BLOCK],s2[BLOCK];//区间和 区间平方和
ll query1(int l,int r){//区间和
    ll ans = 0;
    if(pos[l] == pos[r])for(int i = l;i <= r;++i)ans = (ans + a[i]) % mod;//同属于一个块暴力查
    else{
        for(int i = l;i <= br[pos[l]];++i)ans = (ans + a[i]) % mod;//左散块的暴力
        for(int i = pos[l] + 1;i <= pos[r] - 1;++i)ans = (ans + s1[i]) % mod;//整块直接用求好的
        for(int i = bl[pos[r]];i <= r;++i)ans = (ans + a[i]) % mod;//右散块的暴力
    }
    return ans;
}
ll query2(int l,int r){
    ll ans = 0;
    if(pos[l] == pos[r])for(int i = l;i <= r;++i)ans = (ans + a[i] * a[i] % mod) % mod;
    else{
        for(int i = l;i <= br[pos[l]];++i)ans = (ans + a[i] * a[i]) % mod;
        for(int i = pos[l] + 1;i <= pos[r] - 1;++i)ans = (ans + s2[i]) % mod;
        for(int i = bl[pos[r]];i <= r;++i)ans = (ans + a[i] * a[i]) % mod;
    }
    return ans;
}
signed main() {
    IOS;
    cin>>n>>m;
    for(int i = 1;i <= n;++i)cin>>a[i];
    int cnt = 0;
    int len = sqrt(n);
    for(int i = 1;i <= n;++i){//预处理每一个块的信息
        if((i - 1) % len == 0){
            bl[++cnt] = i;
        }
        s1[cnt] = (s1[cnt] + a[i]) % mod;
        s2[cnt] = (s2[cnt] + a[i] * a[i] % mod) % mod;
        pos[i] = cnt;
        br[cnt] = i;
    }
    while(m--){
        int op,x;
        ll y;
        cin>>op>>x>>y;
        if(op == 1){
            int p = pos[x];//求出来x属于哪个块
            s1[p] = (s1[p] - a[x] + y) % mod;//单点修改的区间和
            if (s1[p] < 0) s1[p] += mod;//防止取模出问题
            s2[p] = (s2[p] - a[x] * a[x] % mod + y * y % mod) % mod;//单点修改的区间平方和
            if (s2[p] < 0) s2[p] += mod;//防止取模出问题
            a[x] = y;//赋值修改
        }
        else{
            ll len = y - x + 1;
            ll ans1 = query1(x,y) * ksm(len,mod - 2) % mod,ans2 = query2(x,y) * ksm(len,mod -2) % mod;
            cout<<(ans2 - ans1 * ans1 % mod + mod) % mod<<"\n";//查询的时候每个量单独求防止爆ll
        }
    }
}