牛客13891 The Trip On Abandoned Railway

链接

点击跳转

题解

加等差数列可以看作两个事情:先给整个区间加上首项,然后从起始位置开始依次加 0 , d , 2 d , 3 d , . . . 0,d,2d,3d,... 0,d,2d,3d,...

第一个过程可以用一个树状数组维护,第二个过程用两个树状数组维护

代码

#include 
#include 
#include 
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct BIT
{
    ll bit[maxn], n;
    void init(int N){n=N;for(int i=1;i<=n;i++)bit[i]=0;}
    ll lowbit(ll x){return x&(-x);}
    void add(ll pos, ll v)
    {
        for(;pos<=n;pos+=lowbit(pos))bit[pos]+=v;
    }
    ll sum(ll pos)
    {
        ll ans(0);
        for(;pos;pos-=lowbit(pos))ans+=bit[pos];
        return ans;
    }
}bit1, bit2, bit3;
#define mod 1000000007ll
ll a[maxn];
int main()
{
    ll T=read();
    while(T--)
    {
        ll n=read(), m=read(), d=read();
        bit1.init(n), bit2.init(n), bit3.init(n);
        ll i; rep(i,1,n)a[i]=read();
        while(m--)
        {
            ll type=read(), x=read();
            if(type==1)
            {
                ll y=read();
                bit1.add(x,+1);
                bit2.add(x,+x);
                bit3.add(x,+y);
            }
            else
            {
                ll q = (bit1.sum(x)*x-bit2.sum(x))*d + bit3.sum(x) + a[x];
                a[x] -= q;
                printf("%lld\n",q%mod);
            }
        }
    }
    return 0;
}

你可能感兴趣的:(树状数组)