分块入门1:区间修改+单点查询

题目链接:https://www.luogu.org/problemnew/show/P3368
题目大意:
在这里插入图片描述
在这里插入图片描述
这题分快很容易T。

/*
给出一个长为n的数列,以及n个操作,操作涉及区间加法,单点查值。
5 5
1 5 4 2 3
1 2 4 2
2 3
1 1 5 -1
1 3 5 7
2 4

6
10
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+5;
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,blo;
int v[500005],bl[500005],atag[500005];
void add(int a,int b,int c)
{
    for(int i=a;i<=min(bl[a]*blo,b);i++)
    {
        v[i]+=c;
    }
    if(bl[a]!=bl[b])
    {
        for(int i=(bl[b]-1)*blo+1;i<=b;i++)
        {
            v[i]+=c;
        }
    }
    for(int i=bl[a]+1;i<=bl[b]-1;i++)
    {
        atag[i]+=c;
    }
}
int main()
{
    n=read();int m=read();blo=sqrt(n);
    for(int i=1;i<=n;i++)v[i]=read();
    for(int i=1;i<=n;i++)bl[i]=(i-1)/blo+1;
    while(m--)
    {
        int f=read();
        if(f==1)
        {
        	int a,b,c;
        	a=read();b=read();c=read();
            add(a,b,c);
        }
        if(f==2)
        {
        	int b;
        	b=read();
            printf("%d\n",v[b]+atag[bl[b]]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值