Getting Started with Blocking Sequences 4

Topic description

gives a length of  nnsequence of n, and  nnn operations, operations involving interval addition, interval summation.

input format

Enter a number  nn on the first linen。

Enter  nn on the second linen numbers, the ith number is  aia_iai​​, separated by spaces.

Next enter  nnn lines of inquiry, enter four numbers per line  opt\mathrm{opt}opt、lll、rrr、ccc, separated by spaces.

若 opt=0\mathrm{opt} = 0o p t = 0, it will be at  [l,r][l,r]All numbers between [ l , r ] add  ccc。

若 opt=1\mathrm{opt} = 1o p t = 1, which means the query is at  [l,r][l,r]sum mod(c+1)mod(c+1) of all numbers of  [ l , r ]m o d ( c + 1 )。

output format

For each query, output a line with a number representing the answer.

Ideas:

Slightly more complex chunking

Compared with the sequence block 1, the more is only interval query

! ! ! Don't look at the difference of only 2 words, the two questions are much different! ! !

 First of all, because it is an interval query, each interval should also maintain a sum

At the same time, when adding, the mark of the non-complete interval should be decentralized (run violently)

When querying, output the sum of the complete interval and the sum of each element of the incomplete interval.

ok

Code:

#include<iostream>  
#include<cstdio>  
#include<cmath>  
using namespace std;  
long long x[50005],sy[50005],bj[10005],sum[10005],n,c,opt,l,r,cd;  
void pushdown(int wz)  
{  
    for (register int ltt = (wz-1)*cd+1; ltt <= wz*cd; ltt ++)  
    {  
        x[ltt]+=bj[wz];  
    }  
    bj[wz]=0;  
}  
void add(int zuo,int you,int v)  
{  
    int k = sy [zuo];  
    int j = sy [you];  
    if(k==j)  
    {  
        pushdown(k);  
        for (register int ltt = zuo; ltt <= you; ltt ++)  
        {  
            x[ltt]+=v;  
            sum [k] + = v;  
        }  
        return;  
    }  
    for (register int ltt = k+1; ltt <= j-1; ltt ++)  
    {  
        bj [ltt]+= v;  
        sum [ltt]+= v*cd;  
    }  
    pushdown(k);  
    pushdown(j);  
    for (register int ltt = zuo; ltt <= k*cd; ltt ++)  
    {  
        x[ltt]+=v;  
        sum [sy [ltt]]+= v;  
    }  
    for (register int ltt = (j-1)*cd+1; ltt <= you; ltt ++)  
    {  
        x[ltt]+=v;  
        sum [sy [ltt]]+= v;  
    }  
}  
long long query(int zuo,int you,int mod)  
{  
    long long ans=0;  
    int k = sy [zuo];  
    int j = sy [you];  
    if(k==j)  
    {  
//      pushdown(k);  
        for (register int ltt = zuo; ltt <= you; ltt ++)  
        {  
            ans+=x[ltt]+bj[k];  
        }  
        ans=ans%(mod+1);  
        return ans;  
    }  
    for(register int kkk=k+1;kkk<=j-1;kkk++)  
    {  
        ans + = sum [kkk];  
    }  
    for (register int ltt = zuo; ltt <= k*cd; ltt ++)  
    {  
        ans+=x[ltt]+bj[k];  
    }  
    for (register int ltt = (j-1)*cd+1; ltt <= you; ltt ++)  
    {  
        ans+=x[ltt]+bj[j];  
    }  
    ans=ans%(mod+1);  
    return ans;  
}  
intmain()  
{  
//  freopen("a1.in","r",stdin);  
//  freopen("1.out","w",stdout);  
    scanf("%d",&n);  
    cd=sqrt(n);  
    int cnt=1;  
    int bnt=0;  
    for(register int i=1;i<=n;i++)  
    {  
        scanf("%d",&x[i]);  
        bnt++;  
        and [i] = cnt;  
        sum[cnt]+=x[i];  
        if(bnt==cd)  
        {  
            bnt=0;  
            cnt++;  
        }  
    }  
    for(register int i=1;i<=n;i++)  
    {  
        scanf("%d%d%d%d",&opt,&l,&r,&c);  
        if(opt==0)  
        {  
            add(l,r,c);  
        }  
        else  
        {  
            printf("%d\n",query(l,r,c));  
        }  
    }  
//  system("fc 1.out a1.out");  
//  system("pause");  
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325113517&siteId=291194637