ACM-ICPC 2018 徐州赛区网络预赛H

 

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i].

Unfortunately, the longer he learns, the fewer he gets.

That means, if he reads books from ll to rr, he will get

 a[l] \times L + a[l+1] \times (L-1) + \cdots + a[r-1] \times 2 + a[r](L is the length of [ l, r ] that equals to r - l + 1).

Now Ryuji has qq questions, you should answer him:

1. If the question type is 11, you should answer how much knowledge he will get after he reads books [ ll, rr ].

2. If the question type is 22, Ryuji will change the ith book's knowledge to a new value.

Input

First line contains two integers n and q ( n, q \le 100000)≤100000.

The next line contains n integers represent a[i]( a[i] \le 1e9)a[i](a[i]≤1e9).

Then in next qq line each line contains three integers aa, bb, cc, if a = 1a=1, it means question type is 11, and bb, cc represents [ l , r ]. if a = 2a=2 , it means question type is 22 , and bb, cc means Ryuji changes the bth book' knowledge to cc

Output

For each question, output one line with one integer represent the answer.

样例输入复制

5 3
1 2 3 4 5
1 1 3
2 5 0
1 4 5

样例输出复制

10
8

 

 

真菜,这次,做题做的一脸懵,三个人心态都炸了。。

 

 

很简单的推理

\sum_{i=l}^{r}(r+1-i)*a[i]进一步化成(r+1)*\sum_{i=l}^{r}a[i]-\sum_{i=l}^{r}i*a[i]

然后维护前缀和a[i]和i*a[i]就好了,这里用的是树状数组

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100005],b[100005],
n,q;
void add(ll a[],int x,ll val)
{
    while(x<=n)
    {
        a[x]+=val;
        x += x&-x;
    }
}
ll query(ll a[],int x)
{
    ll ans = 0;
    while(x)
    {
        ans += a[x];
        x -= x&-x;
    }
    return ans;
}
int main()
{
    ll t,l,r;
    scanf("%d%d",&n,&q);
    for(int i=0;i<n;i++)
        scanf("%lld",&t),add(a,i+1,t),add(b,i+1,(i+1)*t);
    while(q--)
    {
        scanf("%lld%lld%lld",&t,&l,&r);
        if(t==1)
            printf("%lld\n",(r+1)*(query(a,r)-query(a,l-1))-(query(b,r)-query(b,l-1)));
        else
        {
            ll x = query(b,l) - query(b,l-1);
            add(b,l,l*r-x);
            x = query(a,l) - query(a,l-1);
            add(a,l,r-x);
        }
    }

    return 0;
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值