2018徐州模拟赛-H-Ryuji doesn't want to study

  •  1000ms
  •  262144K

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]a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (LL is the length of [ ll, rr ] that equals to r - l + 1r−l+1).

Now Ryuji has qq questions, you should answer him:

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

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

Input

First line contains two integers nn and qq (nn, q \le 100000q≤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, ccrepresents [ ll , rr ]. 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

 

 

这个题啊,好不容易按模板改出来,超时了。不过也算是用树状数组自己做了一回题了


#include<iostream>
#include<cstdio>
typedef long long  LL;
using namespace std;

const int maxn=100024;

LL tree[maxn];

LL lowbit(int x)
{
    return x&(-x);
}
//修改操作
void update(int x,int v)
{
    for(int i=x;i<=maxn;i+=lowbit(i)){
        tree[i]+=v;
    }
}
//查询操作
LL getsum(int x)
{
    LL res=0;
    for(;x;x-=lowbit(x)){
        res+=tree[x];
    }
    return res;
}

int main()
{
    int n,q;
    int a[maxn];
    scanf("%d%d",&n,&q);//区间长度
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);//输入第i个位置的数
        update(i,a[i]);
    }
    for(int i=1;i<=q;i++){
        int opt,l,r,b,c;
        LL sum=0;
        scanf("%d",&opt);
        if(opt==2){           //修改树状数组
            scanf("%d%d",&b,&c);
            update(b,-(a[b]-c));
            a[b]=c;
        }
        else{
            scanf("%d%d",&l,&r);
            int L=r-l+1;
            while(l<=r+1){
                sum+=(getsum(l)-getsum(l-1))*L;
                L--;
                ++l;
            }
            printf("%lld\n",sum);
        }
    }
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值