• LOJ-数列分块入门5


    链接:

    https://loj.ac/problem/6281

    题意:

    给出一个长为 的数列 ,以及 n个操作,操作涉及区间开方,区间求和。

    思路:

    考虑开方5次之后就为1, 即考虑一整个区间的开方次数,对小于5次的区间暴力开方,否则就不管他.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #include <iomanip>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    const int MAXN = 1e5+10;
    
    LL a[MAXN], Tag[MAXN], Sum[MAXN];
    LL Belong[MAXN];
    int n, part;
    
    void Update(LL l, LL r, LL c)
    {
        if (Tag[Belong[l]] < 5)
            for (int i = l;i <= min(Belong[l]*part, r);i++)
            {
                Sum[Belong[i]] -= a[i];
                a[i] = sqrt(a[i]);
                Sum[Belong[i]] += a[i];
            }
        if (Belong[l] != Belong[r])
        {
            if (Tag[Belong[r]] < 5)
                for (int i = max((Belong[r]-1)*part+1, l);i <= r;i++)
                {
                    Sum[Belong[i]] -= a[i];
                    a[i] = sqrt(a[i]);
                    Sum[Belong[i]] += a[i];
                }
        }
        for (int i = Belong[l]+1;i <= Belong[r]-1;i++)
        {
            if (Tag[i] < 5)
            {
                Tag[i]++;
                for (int j = (i-1)*part+1;j <= i*part;j++)
                {
                    Sum[i] -= a[j];
                    a[j] = sqrt(a[j]);
                    Sum[i] += a[j];
                }
            }
        }
    }
    
    LL Query(LL l, LL r, LL c)
    {
        LL res = 0;
        for (int i = l;i <= min(Belong[l]*part, r);i++)
            res += a[i];
        if (Belong[l] != Belong[r])
        {
            for (int i = max((Belong[r]-1)*part+1, l);i <= r;i++)
                res += a[i];
        }
        for (int i = Belong[l]+1;i <= Belong[r]-1;i++)
            res += Sum[i];
        return res;
    }
    
    int main()
    {
        scanf("%d", &n);
        part = sqrt(n);
        for (int i = 1;i <= n;i++)
        {
            scanf("%lld", &a[i]);
            Belong[i] = (i-1)/part + 1;
            Sum[Belong[i]] += a[i];
        }
        int op, l, r, c;
        for (int i = 1;i <= n;i++)
        {
            scanf("%d", &op);
            if (op == 0)
            {
                scanf("%d%d%d", &l, &r, &c);
                Update(l, r, c);
            }
            else
            {
                scanf("%d%d%d", &l, &r, &c);
                printf("%lld
    ", Query(l, r, c));
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    ELK的学习与应用
    windows 常用命令
    Electron笔记
    C#基础
    IIS运行NetCore程序
    nuget打包
    web pack备忘
    基于并发订课系统的架构演变
    面试造核弹的童话
    Python3-接口自动化-11-使用join方法请求参数拼接,格式key1=value1&keys=value2....
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11431954.html
Copyright © 2020-2023  润新知