题解:P13976 数列分块入门 1

· · 题解

P13976 数列分块入门 1

题目描述:

解题思路:

Ac Code:

#include<bits/stdc++.h>
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#ifdef __linux__
#define pc putchar_unlocked
#else
#define pc _putchar_nolock
#endif
#define int long long
#define rint register int
#define R register
#define _ read<int>()
template<class T>inline T read()
{
    R T r=0,f=1;R char c=gc();
    while(!isdigit(c))
    {
        if(c=='-') f=-1;
        c=gc();
    } 
    while(isdigit(c)) r=(r<<1)+(r<<3)+(c^48),c=gc();
    return f*r;
}
inline void out(rint x)
{
    if(x<0) pc('-');
    if(x<10) pc(x+'0');
    else out(x/10),pc(x%10+'0');
}
const int N=3e5+10;
int a[N],tr[N];
inline int lowbit(rint x)
{
    return x&(-x);
}
inline void modify(rint p,rint x)
{
    while(p<N)
    {
        tr[p]+=x;
        p+=lowbit(p);
    }
}
inline int query(rint p)
{
    rint ans=0;
    while(p>0)
    {
        ans+=tr[p];
        p-=lowbit(p);
    }
    return ans;
}
signed main()
{
    rint n=_;
    for(rint i=1;i<=n;i++)
    {
        rint x=_;
        modify(i,x);
        modify(i+1,-x);
    }
    for(rint i=1;i<=n;i++)
    {
        rint op=_;
        if(op==0)
        {
            rint l=_,r=_,c=_;
            modify(l,c);
            modify(r+1,-c);
        }
        else
        {
            rint l=_,r=_,c=_;
            printf("%lld\n",query(r));
        }
    }
    return 0;
}