Introduction to sequence block 1

Posted by Rocu on Sat, 28 Mar 2020 17:26:27 +0100

Title Description

In this paper, we give a sequence of nnn and nnn operations, which involve interval addition and single point search.

Input format

Enter a number nnn in the first line.

In the second line, enter nnn numbers, and the third number is AIA ﹣ ia i, separated by spaces.

Next, enter the nnn line query, four numbers for each line: opt\mathrm{opt}opt, lll, rrr, ccc, separated by spaces.

If opt=0\mathrm{opt} = 0opt=0, the number between [l,r][l, r][l,r] will be added with ccc.

If opt=1\mathrm{opt} = 1opt=1, ask for the value of Ara ﹣ RA r (lll and ccc ignored).

Output format

For each query, output a line and a number to indicate the answer.

Train of thought:

The simplest sequence block

First, the concept of one wave block is defined:

A sequence of n-length numbers is divided into k-blocks (generally k=sqrt (n));

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

How to describe the modification and query of interval?

How about lazy!!

Create an array of tags to store changes to the entire interval

Finally, output the sum of the tag array and the original value

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;  
int main()  
{  
    // 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]);  
        sy[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;  
            }  
        }  
    }  
}

Topics: C++