Getting Started with Sequence Blocking 1

Topic description

gives a length of  nnsequence of n, and  nnn operations, operations involving interval addition, single-point lookup.

input format

Enter a number  nn on the first linen。

Enter  nn on the second linen digits,  iithi numbers are  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 to ask  ara_rar​​ value ( lll and  ccc ignored).

output format

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

Ideas:

The simplest sequence of blocks

First clarify the concept of a wave of blocks:

Divide a long sequence of n into k pieces (generally k=sqrt(n));

Then the modification of the interval becomes the modification of the complete block and the violence of the incomplete blocks at both ends of the interval

How to describe the modification and query of the interval?

Lazy Dafa is good! !

Create a marker array to store changes to the entire range

Finally, the sum of the tag array and the original value can be output.

Code:

#include<iostream>  
#include<cstdio>  
#include<cmath>  
using namespace std;  
int x[50005],sy[50005],fk[50005],n,opt,l,r,c,a,b,d,dx,cnt,bnt;  
intmain()  
{  
    // freopen("a1.in","r",stdin);  
    // freopen("1.out","w",stdout);  
    cin>>n;  
    dx=sqrt(n);  
    bnt=1;  
    for(a=1;a<=n;a++)  
    {  
        scanf("%d",&x[a]);  
        and [a] = (a-1)/dx+1;  
    }  
    for(a=1;a<=n;a++)  
    {  
        scanf("%d%d%d%d",&opt,&l,&r,&c);  
        if(opt==1)  
        {  
            printf("%d\n",x[r]+fk[sy[r]]);  
        }  
        else  
        {  
            if (sy [l] == sy [r])  
            {  
                for(b=l;b<=r;b++)  
                {  
                    x[b]+=c;  
                }  
                continue;  
            }   
            int lf, ri;  
            lf = sy [l];  
            ri = sy [r];  
            for(b=l;b<=lf*dx;b++)  
            {  
                x[b]+=c;  
            }  
            for(b=(ri-1)*dx+1;b<=r;b++)  
            {  
                x[b]+=c;  
            }  
            for(b=lf+1;b<=ri-1;b++)  
            {  
                fk[b]+=c;  
            }  
        }  
    }  
}

 

Guess you like

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