LOJ#6283. 数列分块入门 7

内存限制:256 MiB时间限制:500 ms标准输入输出

题目类型:传统评测方式:文本比较

上传者: hzwer

提交提交记录统计讨论测试数据

题目描述

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

输入格式

第一行输入一个数字 nnn。

第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开。

接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt、lll、rrr、ccc,以空格隔开。

若 opt=0\mathrm{opt} = 0opt=0,表示将位于 [l,r][l, r][l,r] 的之间的数字都加 ccc。

若 opt=1\mathrm{opt} = 1opt=1,表示将位于 [l,r][l, r][l,r] 的之间的数字都乘 ccc。

若 opt=2\mathrm{opt} = 2opt=2,表示询问 ara_ra?r?? 的值 mod 10007mod \ 10007mod 10007(lll 和 ccc 忽略)。

输出格式

对于每次询问,输出一行一个数字表示答案。

样例

样例输入

7
1 2 2 3 9 3 2
0 1 3 1
2 1 3 1
1 1 4 4
0 1 7 2
1 2 6 4
1 1 6 5
2 2 6 4

样例输出

3
100

数据范围与提示

对于 100% 100\%100% 的数据,1≤n≤100000,−231≤others 1 \leq n \leq 100000, -2^{31} \leq \mathrm{others}1≤n≤100000,−2?31??≤others、ans≤231−1 \mathrm{ans} \leq 2^{31}-1ans≤2?31??−1。

显示分类标签

对于两种标记,分别进行维护。

按照运算规律(这个可以自己推式子),我们先考虑乘法,再考虑加法

对于加的操作,相当于直接对于每个块的加法标记加

对于乘的操作,我们需要对乘法标记和加法标记都乘上对应的值

零散块不好记录,直接暴力修改

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=1e5+10,mod=10007;
inline char nc()
{
    static char buf[MAXN],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin)),p1==p2?EOF:*p1++;
}
inline int read()
{
    char c=nc();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=nc();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=nc();}
    return x*f;
}
int a[MAXN],belong[MAXN],L[MAXN],R[MAXN],block,N;
int mul[MAXN],add[MAXN];
void reset(int x)
{
    for(int i=L[x*block];i<=min(N,R[x*block]);i++)
        a[i]=(a[i]*mul[x]+add[x])%mod;
    mul[x]=1;add[x]=0;
}
void Add(int l,int r,int val)
{
    reset(belong[l]);
    for(int i=l;i<=min(r,R[l]);i++) a[i]+=val,a[i]%=mod;
    if(belong[l]!=belong[r])
    {
        reset(belong[r]);
        for(int i=L[r];i<=r;i++)
             a[i]+=val,a[i]%=mod;
    }
    for(int i=belong[l]+1;i<=belong[r]-1;i++)
        add[i]+=val,add[i]%=mod;
}
void Mul(int l,int r,int val)
{
    reset(belong[l]);
    for(int i=l;i<=min(r,R[l]);i++) a[i]*=val,a[i]%=mod;
    if(belong[l]!=belong[r])
    {
        reset(belong[r]);
        for(int i=L[r];i<=r;i++)
            a[i]*=val,a[i]%=mod;
    }

    for(int i=belong[l]+1;i<=belong[r]-1;i++)
        mul[i]*=val,add[i]*=val,mul[i]%=mod,add[i]%=mod;
}
int main()
{
    #ifdef WIN32
    freopen("a.in","r",stdin);
    #else
    #endif
    N=read();block=sqrt(N);
    fill(mul,mul+N+1,1);
    for(int i=1;i<=N;i++) a[i]=read()%mod,belong[i]=(i-1)/block+1;
    for(int i=1;i<=N;i++) L[i]=(belong[i]-1)*block+1,R[i]=belong[i]*block;
    for(int i=1;i<=N;i++)
    {
        int opt=read(),l=read(),r=read(),c=read()%mod;
        if(opt==0)
            Add(l,r,c);
        else if(opt==1)
            Mul(l,r,c);
        else if(opt==2)
            printf("%d\n",(a[r]*mul[belong[r]]+add[belong[r]])%mod);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zwfymqz/p/8453577.html

时间: 2024-08-30 17:11:35

LOJ#6283. 数列分块入门 7的相关文章

LOJ#6284. 数列分块入门 8

#6284. 数列分块入门 8 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论 1 测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间询问等于一个数 ccc 的元素,并将这个区间的所有元素改为 ccc. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入三个数字 ll

Loj 6279. 数列分块入门 3

题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,询问区间内小于某个值 xxx 的前驱(比其小的最大元素). 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 iii 个数字为 aia_iai?,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 opt=0\mathrm{opt} = 0opt=0,表示将位于 [l,r][l, r][l,r] 的之间的数字

Loj 6277 数列分块入门 1

题目链接:https://loj.ac/problem/6277 题面: 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 iii 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 opt=0\mathrm{opt} = 0opt=0,表示将位于 [l,r][l,

loj#6285 数列分块入门 9 ( 回 滚 )

题目 :  链接 :https://loj.ac/problem/6285 题意:给出一个长为 n的数列,以及 n个操作,操作涉及询问区间的最小众数. 思路:虽然这不是一道 回滚莫队题,就是 暴力分块 的题, 但是 还是 可以用回滚莫队 写滴,好像大部分题解都是 暴力分块. #include<bits/stdc++.h> #define LL long long #define ULL unsigned long long #define rep(i,j,k) for(int i=j;i<

LOJ#6278. 数列分块入门 2

内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,询问区间内小于某个值 xxx 的元素个数. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,

#6283. 数列分块入门 7(区间乘法,区间加法,单点询问)

题目链接:https://loj.ac/problem/6283 题目大意:中文题目 具体思路:和线段树的思路相同,注意lazy的下放,对于不完整的区间,我们需要先更新数组a的值,然后再对数组a进行操作.对于完整的操作,我们要注意优先级,如果原来是a*b+c的话,我们要对这个区间乘以e的话,就表示成a*(b*e)+c*e. AC代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 # define ll long long 4 const

LOJ#6281. 数列分块入门 5

内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论 1 测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间开方,区间求和. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 opt=

LOJ#6282. 数列分块入门 6

内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及单点插入,单点询问,数据随机生成. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 

Loj 6280 数列分块入门 4

链接:https://loj.ac/problem/6280 思路: 多设置一个数组sum去存区间值的和就好了,因为数据范围比较大,需要开long long . 实现代码; #include<bits/stdc++.h> using namespace std; #define ll long long const ll M = 1e5+10; ll sum[M],a[M],tag[M],bl[M],n,block; void update(ll l,ll r,ll c){ for(ll i