【分块】#6281. 数列分块入门 5(区间开方,区间求和)

在这里插入图片描述
在这里插入图片描述
啊啊啊,分块的下标别写错了

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
const int N = 500007;

ll read()
{
    ll x = 0, f = 1;char c = getchar();
    while(c > '9' || c < '0'){if(c == '-')f = -1;c = getchar();}
    while(c <= '9' && c >= '0'){x = x * 10 + c - '0';c = getchar();}
    return x * f;
}

int n, m;
int v[N];
int block, bi[N];
ll sum[N];
bool equal_0[N];
//因为数据最大为1e9,开根号下取整,最多五次就会变成0/1
//然后标记一下等于0的块,这样再开根号的时候因为已经等于1/0,不变,直接return
void solve_sqrt(int x)
{
    if(equal_0[x])return ;
    equal_0[x] = 1;
    sum[x] = 0;
    for(int i = (x - 1) * block + 1; i <= x * block; ++ i){
        v[i] = sqrt(v[i]), sum[x] += v[i];
        if(v[i] > 1)equal_0[x] = 0;
    }
}

void modify(int a, int b, int c)
{
    for(int i = a; i <= min(bi[a] * block, b); ++ i){
        sum[bi[a]] -= v[i];
        v[i] = sqrt(v[i]);
        sum[bi[a]] += v[i];
    }
    if(bi[a] != bi[b]){
        for(int i = (bi[b] - 1) * block + 1; i <= b; ++ i){
            sum[bi[b]] -= v[i];
            v[i] = sqrt(v[i]);
            sum[bi[b]] += v[i];
        }
    }
    for(int i = bi[a] + 1; i <= bi[b] -1; ++ i)
        solve_sqrt(i);
}

ll query(int a, int b)
{
    ll res = 0;
    for(int i = a; i <= min(bi[a] * block, b); ++ i){
        res += v[i];
    }
    if(bi[a] != bi[b]){
        for(int i = (bi[b] - 1) * block + 1; i <= b; ++ i)
            res += v[i];
    }
    for(int i = bi[a] + 1; i <= bi[b] - 1; ++ i)
        res += sum[i];
    return res;
}

int main()
{
    scanf("%d", &n);
    block = sqrt(n);
    for(int i = 1; i <= n; ++ i){
        scanf("%d", &v[i]);
    }

    for(int i = 1; i <= n; ++ i){
        bi[i] = (i - 1) / block + 1;
        sum[bi[i]] += v[i];
    }

    for(int i = 1; i <= n; ++ i){
        int op = read(), a = read(), b = read(), c= read();
        if(op == 0){
            modify(a, b,c);
        }
        else if(op == 1){
            printf("%lld\n", query(a, b));
        }
    }
    return 0;
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:设计师小姐姐 返回首页