1 条题解

  • 0
    @ 2025-10-8 16:56:24
    #include <bits/stdc++.h> 
    using namespace std;
    typedef long long LL;
    const LL N=1e6+10;
    LL n,a[N],c[N];
    void add(LL x,LL k){for(;x<=n;x+=x&-x)c[x]+=k;}
    LL getsum(LL x)
    {
        LL res=0;
        for(;x>=1;x-=x&-x)res+=c[x];
        return res;
    }
    int main()
    {
        LL m;scanf("%lld%lld",&n,&m);
        memset(c,0,sizeof(c));
    	a[0]=0;for(LL i=1;i<=n;i++)scanf("%lld",&a[i]),add(i,a[i]-a[i-1]);
        for(LL i=1,op,x,y,d;i<=m;i++)
        {
            scanf("%lld",&op);
            if(op==1)
    		{
    			scanf("%lld%lld%lld",&x,&y,&d);
    			add(x,d);add(y+1,-d);
    		}
            else
            {
                scanf("%lld",&x);
                printf("%lld\n",getsum(x));
            }
        }
        return 0;
    }
    
    
    • 1

    树状数组 2 :区间修改,单点查询

    信息

    ID
    1325
    时间
    1000ms
    内存
    512MiB
    难度
    8
    标签
    递交数
    594
    已通过
    81
    上传者