数列分块入门 1

题目描述

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

输入格式

第一行输入一个数字 nnn

第二行输入 nnn 个数字,第 iii 个数字为 aia_iai,以空格隔开。

接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}optlllrrrccc,以空格隔开。

若 opt=0\mathrm{opt} = 0opt=0,表示将位于 [l,r][l, r][l,r] 的之间的数字都加 ccc

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

输出格式

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

#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;
			}
		}
	}
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值