[AHOI2009]维护序列

寒假填坑五十道省选题——第三题

[AHOI2009]维护序列

题目描述

老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。 有长为N的数列,不妨设为a1,a2,…,aN 。有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值。

输入输出格式

输入格式:

第一行两个整数N和P(1≤P≤1000000000)。第二行含有N个非负整数,从左到右依次为a1,a2,…,aN, (0≤ai≤1000000000,1≤i≤N)。第三行有一个整数M,表示操作总数。从第四行开始每行描述一个操作,输入的操作有以下三种形式: 操作1:“1 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai×c (1≤t≤g≤N,0≤c≤1000000000)。 操作2:“2 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai+c (1≤t≤g≤N,0≤c≤1000000000)。 操作3:“3 t g”(不含双引号)。询问所有满足t≤i≤g的ai的和模P的值 (1≤t≤g≤N)。 同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。

输出格式:

对每个操作3,按照它在输入中出现的顺序,依次输出一行一个整数表示询问结果。

输入输出样例

输入样例#1:
7 43
1 2 3 4 5 6 7
5
1 2 5 5
3 2 4
2 3 7 9
3 1 3
3 4 7
输出样例#1: 
2
35
8

说明

【样例说明】

初始时数列为(1,2,3,4,5,6,7)。

经过第1次操作后,数列为(1,10,15,20,25,6,7)。

对第2次操作,和为10+15+20=45,模43的结果是2。

经过第3次操作后,数列为(1,10,24,29,34,15,16}

对第4次操作,和为1+10+24=35,模43的结果是35。

对第5次操作,和为29+34+15+16=94,模43的结果是8。

测试数据规模如下表所示

数据编号 1 2 3 4 5 6 7 8 9 10

N= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000

M= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000

Source: Ahoi 2009

线段树模版,记得取模取对。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int N=400001;
ll sum[N],lazyj[N],lazyc[N],a[N],n,m,mod,x,y,k;
void build(int root,int l,int r)
{
    lazyc[root]=1;
    if(l==r)
    {sum[root]=a[l]%mod;return ;}
    int mid=(l+r)/2;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
    sum[root]=(sum[root<<1]+sum[root<<1|1])%mod;
    return ;
}
void push(int root,int l,int r)
{
    if(lazyc[root]!=1)
    {
        lazyj[root<<1]=(lazyj[root<<1]*lazyc[root])%mod;
        lazyj[root<<1|1]=(lazyj[root<<1|1]*lazyc[root])%mod;
        lazyc[root<<1|1]=(lazyc[root<<1|1]*lazyc[root])%mod;
        lazyc[root<<1]=(lazyc[root<<1]*lazyc[root])%mod;
        sum[root<<1]=(sum[root<<1]*lazyc[root])%mod;
        sum[root<<1|1]=(sum[root<<1|1]*lazyc[root])%mod;
        lazyc[root]=1;
    }
    if(lazyj[root])
    {
        lazyj[root<<1]=(lazyj[root]+lazyj[root<<1])%mod;
        lazyj[root<<1|1]=(lazyj[root<<1|1]+lazyj[root])%mod;
        int mid=(l+r)/2;
        sum[root<<1]=(sum[root<<1]+lazyj[root]*(mid-l+1))%mod;
        sum[root<<1|1]=(sum[root<<1|1]+lazyj[root]*(r-mid))%mod;
        lazyj[root]=0;
    }
    //sum[root]=(sum[root<<1]+sum[root<<1|1])%mod;
    return ;
}
void update(int root,int left,int right,int l,int r,ll v,bool f)
{
    if(left>=l&&right<=r)
    {
        if(!f)
        {
            lazyj[root]=(lazyj[root]+v)%mod;
            sum[root]=(sum[root]+(right-left+1)*v)%mod;
        }
        if(f)
        {
            lazyj[root]=(lazyj[root]*v)%mod;
            lazyc[root]=(lazyc[root]*v)%mod;
            sum[root]=(sum[root]*v)%mod;
        }
        return ;
    }
    if(right<l||left>r)return ;
   push(root,left,right);
    int mid=(left+right)/2;
    if(mid>=l) update(root<<1,left,mid,l,r,v,f);
    if(mid<r)  update(root<<1|1,mid+1,right,l,r,v,f);
    sum[root]=(sum[root<<1|1]+sum[root<<1])%mod;
    return ;
}
ll query(int root,int left,int right,int l,int r)
{
    //cout<<left<<" "<<right<<" "<<l <<" "<<r<<" "<<root<<endl;
    if(left>=l&&r>=right)return sum[root];
    if(left>r||right<l)return 0;
    /*if(lazyj[root]||lazyc[root]!=1)*/push(root,left,right);
    int mid=(left+right)/2;
//    cout<<mid<<endl;
    ll a=0,b=0;
    if(l<=mid) a=query(root<<1,left,mid,l,r);
    if(mid<r)  b=query(root<<1|1,mid+1,right,l,r);
//    sum[root]=(sum[root<<1]+sum[root<<1|1])%mod;
    return (a+b)%mod;
}
ll read()
{
    ll w=1,x=0;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*w;
}
int main()
{
    n=read();mod=read();
    for(int i=1;i<=n;i++)
    {
        a[i]=read();
    }
    build(1,1,n);//cout<<sum[1]<<endl;
    m=read();
    for(int i=1;i<=m;i++)
    {
        int qwq;qwq=read();
    //    cout<<qwq<<endl;
        if(qwq==1)
        {
            x=read();y=read();k=read();
            update(1,1,n,x,y,k,1);
        }
        if(qwq==2)
        {
            x=read();y=read();k=read();
            update(1,1,n,x,y,k,0);
            //cout<<sum[1]<<endl;
        }
        if(qwq==3)
        {
            x=read();y=read();
            printf("%lld
",query(1,1,n,x,y)%mod);
        }
    }
}
原文地址:https://www.cnblogs.com/hhh1109/p/8436549.html