LOJ——#6277. 数列分块入门 1

#6277. 数列分块入门 1

题目描述

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

输入格式

第一行输入一个数字 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] 的之间的数字都加 ccc。

若 opt=1\mathrm{opt} = 1opt=1,表示询问 ara_rar​​ 的值(lll 和 ccc 忽略)。

输出格式

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

样例

样例输入

4
1 2 2 3
0 1 3 1
1 0 1 0
0 1 2 2
1 0 2 0

样例输出

2
5

数据范围与提示

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

 
分块入门,区间修改+单点查询
#include<bits/stdc++.h>

#define N 1010100
using namespace std;

int n,a[N],blo[N],atag[N];

void update(int l,int r,int c){
    for(int i=l;i<=min(blo[l]*blo[0],r);i++)
        a[i]+=c;
    if(blo[l]!=blo[r])
        for(int i=(blo[r]-1)*blo[0]+1;i<=r;i++)
            a[i]+=c;
    for(int i=blo[l]+1;i<=blo[r]-1;i++)
        atag[i]+=c;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    blo[0]=sqrt(n);
    for(int i=1;i<=n;i++) blo[i]=(i-1)/blo[0]+1;
    for(int i=1;i<=n;i++){
        int opt,l,r,c;
        scanf("%d%d%d%d",&opt,&l,&r,&c);
        if(!opt) update(l,r,c);
        else printf("%d\n",a[r]+atag[blo[r]]);
    }return 0;
}

~~推荐播客~~

「分块」数列分块入门1 – 9 by hzwer

猜你喜欢

转载自www.cnblogs.com/song-/p/9470487.html