LOJ-6283-数列分块7

链接:

https://loj.ac/problem/6283

题意:

给出一个长为 的数列,以及 个操作,操作涉及区间乘法,区间加法,单点询问。

思路:

考虑整块的乘法, 同时对整块的add标记也要乘,对单点操作时, 注意要对整块暴力修改完再操作.

代码:

#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 = 1e6+10;
const int MOD = 10007;

LL a[MAXN], TagMul[MAXN], TagAdd[MAXN];
int Belong[MAXN];
int n, part, last;

void Reset(int pos)
{
    for (int i = (pos-1)*part+1;i <= pos*part;i++)
        a[i] = ((a[i]*TagMul[pos])%MOD+TagAdd[pos])%MOD;
    TagAdd[pos] = 0;
    TagMul[pos] = 1;
}

void UpdateAdd(int l, int r, int c)
{
    Reset(Belong[l]);
    for (int i = l;i <= min(r, Belong[l]*part);i++)
        a[i] = (a[i]+c)%MOD;
    if (Belong[l] != Belong[r])
    {
        Reset(Belong[r]);
        for (int i = max(l, (Belong[r]-1)*part+1);i <= r;i++)
            a[i] = (a[i]+c)%MOD;
    }
    for (int i = Belong[l]+1;i <= Belong[r]-1;i++)
        TagAdd[i] = (TagAdd[i]+c)%MOD;
}

void UpdateMul(int l, int r, int c)
{
    Reset(Belong[l]);
    for (int i = l;i <= min(r, Belong[l]*part);i++)
        a[i] = (a[i]*c)%MOD;
    if (Belong[l] != Belong[r])
    {
        Reset(Belong[r]);
        for (int i = max(l, (Belong[r]-1)*part+1);i <= r;i++)
            a[i] = (a[i]*c)%MOD;
    }
    for (int i = Belong[l]+1;i <= Belong[r]-1;i++)
    {
        TagMul[i] = (TagMul[i]*c)%MOD;
//        cout << i << ' ' << TagMul[i] << endl;
        TagAdd[i] = (TagAdd[i]*c)%MOD;
    }
}

int Query(int l, int r, int c)
{
    int p = Belong[r];
//    cout << a[r] << ' ' << TagMul[p] << ' ' << TagAdd[p] << endl;
    return ((a[r]*TagMul[p])%MOD+TagAdd[p])%MOD;
}

int main()
{
    scanf("%d", &n);
    part = sqrt(n);
    for (int i = 1;i <= n;i++)
    {
        TagMul[i] = 1;
        scanf("%lld", &a[i]);
        Belong[i] = (i-1)/part+1;
    }
    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);
            UpdateAdd(l, r, c);
        }
        else if (op == 1)
        {
            scanf("%d%d%d", &l, &r, &c);
            UpdateMul(l, r, c);
        }
        else
        {
            scanf("%d%d%d", &l, &r, &c);
            printf("%d\n", Query(l, r, c));
        }
    }

    return 0;
}

转载于:https://www.cnblogs.com/YDDDD/p/11433393.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值