Ryuji doesn't want to study

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

题目来源

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

 

进阶版的线段树

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define MAXN 100005
typedef struct segmentree
{
    int       left;
    int       right;
    long long sum;   //long long 型
    long long val;
}segmentree;
//线段树是满二叉树形式,但不是树的链表结构
//所以可以用下标法找结点
segmentree segtree[MAXN<<2];
long long a[MAXN];
void push_up(int root)
{
    segtree[root].val=segtree[root*2].val+segtree[root*2+1].val;
    segtree[root].sum=segtree[root*2].sum+(segtree[root].right-segtree[root*2].right)*segtree[root*2].val+segtree[root*2+1].sum;  //后面反正是从1开始的,所以也不用累加什么的
}
void CreateSegmentree(int root,int l,int r)  //创建线段树
{
    segtree[root].left = l;
    segtree[root].right = r;
    if(l==r)
    {
         segtree[root].val=a[l];
         segtree[root].sum=a[l];
    }
    else
    {
     int mid = (l+r)>>1;//mid=(l+r)>>1
     CreateSegmentree(root<<1,l,mid);
     CreateSegmentree(root<<1|1,mid+1,r);   //右移|1才是加1的意思
     push_up(root);
     //递归的终止就是l==r
    }
    return;
    
}
long long query(long long root,long long L,long long R)
{
    if(L<=segtree[root].left&&segtree[root].right<=R)
    {
        return segtree[root].sum+(R-segtree[root].right)*segtree[root].val;
    }

    long long mid=(segtree[root].left+segtree[root].right)/2;
    long long  ans=0;
    if(L<=mid) ans+=query(root*2,L,R);
    if(R>mid)  ans+=query(root*2+1,L,R);
    return ans;
}
void revise(int root,int x, long long val)    //单点修改,单点修改判断只有两种
{
    if(segtree[root].left == segtree[root].right)
    {
        segtree[root].val = val;
        segtree[root].sum =val;
    }
    else
    {
        if(x<= segtree[root<<1].right)
        revise(root<<1,x,val);              //如果小于左子树右边界进入左子树
        else
        revise(root<<1|1,x,val);           //不然进入右子树
        push_up(root);
    }
    return ;
}
int main()
{
    int l,r;
    int x;
    long long val;
    int n,i,m;
    char ops[20];
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    {
       scanf("%lld",&a[i]);
    }
    CreateSegmentree(1,1,n);
    while(m--)
    {
            scanf("%s",ops);
            if(ops[0]=='1')
            {
                scanf("%d%d",&l,&r);
                printf("%lld\n",query(1,l,r));
            }
            else
            {
                scanf("%d%lld",&x,&val);
                revise(1,x,val);
            }
    }
  return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值