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
5

题目链接:

https://nanti.jisuanke.com/t/31460

题目大意:

给你一个数列a[1..n],多次求对于[i..j]区间,a[i]*L+a[i+1]*(L-1)+...+a[j]*1,其中L是区间长度(j-i+1)。

线段树水题。

对该数列建立线段树。每个节点维护两个值sum和tri。sum是区间和,tri是区间三角和(即题目中所要求的和)。适当地改动一下操作,就是一个简单的单点修改,区间查询的线段树问题。

由于是在区域赛预赛中做出来的,还是写个博客纪念一下吧。^_^

#include<cstdio>
#include<cmath> using namespace std; const int maxn=; struct ttree
{
int l,r;
long long sum;
long long tri;
};
ttree tree[maxn*+]; void pushup(int x)
{
if(tree[x].l==tree[x].r)
return;
tree[x].sum=tree[x*].sum+tree[x*+].sum;
tree[x].tri=tree[x*].tri+tree[x*+].tri+
tree[x*].sum*(tree[x*+].r-tree[x*+].l+);
} void build(int x,int l,int r)
{
tree[x].l=l;
tree[x].r=r;
if(l==r)
{
scanf("%lld",&tree[x].sum);
tree[x].tri=tree[x].sum;
}
else
{
int mid=(l+r)/;
build(x*,l,mid);
build(x*+,mid+,r);
pushup(x);
}
} void modify(int x,int pos,int val)
{
if(tree[x].l==tree[x].r)
{
tree[x].sum=tree[x].tri=val;
return;
}
int mid=(tree[x].l+tree[x].r)/;
if(pos<=mid)
modify(x*,pos,val);
else
modify(x*+,pos,val);
pushup(x);
} long long query(int x,int l,int r)
{
if(l<=tree[x].l&&r>=tree[x].r)
return tree[x].sum*(r-tree[x].r)+tree[x].tri;
long long ret=;
int mid=(tree[x].l+tree[x].r)/;
if(l<=mid)
ret+=query(x*,l,r);
if(r>mid)
ret+=query(x*+,l,r);
return ret;
} int main()
{
int n,q;
scanf("%d%d",&n,&q);
build(,,n);
int a,b,c;
while(q--)
{
scanf("%d%d%d",&a,&b,&c);
if(a==)
{
printf("%lld\n",query(,b,c));
}
else
{
modify(,b,c);
}
}
return ;
}

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study (线段树)的更多相关文章

  1. ACM-ICPC 2018 徐州赛区网络预赛H Ryuji doesn't want to study(树状数组)题解

    题意:给你数组a,有两个操作 1 l r,计算l到r的答案:a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (L is the length of [ l, r ] that ...

  2. ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study(树状数组)

    Output For each question, output one line with one integer represent the answer. 样例输入 5 3 1 2 3 4 5 ...

  3. ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study

    262144K   Ryuji is not a good student, and he doesn't want to study. But there are n books he should ...

  4. ACM-ICPC 2018 徐州赛区网络预赛 H Ryuji doesn't want to study (树状数组差分)

    https://nanti.jisuanke.com/t/31460 题意 两个操作.1:查询区间[l,r]的和,设长度为L=r-l+1, sum=a[l]*L+a[l+1]*(L-1)+...+a[ ...

  5. 计蒜客 1460.Ryuji doesn't want to study-树状数组 or 线段树 (ACM-ICPC 2018 徐州赛区网络预赛 H)

    H.Ryuji doesn't want to study 27.34% 1000ms 262144K   Ryuji is not a good student, and he doesn't wa ...

  6. ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(模拟+线段树)

    https://nanti.jisuanke.com/t/30996 题意 每天增加m个灯泡,n个房间,能一次性换就换,模拟换灯泡过程.询问第几天的状态 分析 离线做,按题意模拟.比赛时线段树写挫了. ...

  7. 2018icpc徐州网络赛-H Ryuji doesn't want to study(线段树)

    题意: 有n个数的一个数组a,有两个操作: 1 l r:查询区间[l,r]内$a[l]*(r-l+1)+a[l+1]*(r-l)+a[l+2]*(r-l-1)+\cdots+a[r-1]*2+a[r] ...

  8. ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)

    传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 D 杜教筛 前缀和

    链接 https://nanti.jisuanke.com/t/31456 参考题解  https://blog.csdn.net/ftx456789/article/details/82590044 ...

随机推荐

  1. NIO流的学习以及Buffer的相关操作

    NIO的使用 一).什么叫NIO? 定义:是一套新的Java I/O标准, 在java1.4中被纳入JDK中. 二).NIO的实现方法 NIO是基于块的, 以块为基本单位处理数据. 标准的I/O是基于 ...

  2. 在Windows Server 2019通过Docker Compose部署Asp.Net Core

    一.安装Docker Enterprise 安装文档是: https://docs.docker.com/install/windows/docker-ee/ 安装完成后,如下图 二.首先,拉取一个W ...

  3. python的匿名函数

    在Python,有两种函数,一种是def定义,一种是lambda函数. lambda函数是Python一种生成函数对象的表达式形式.匿名函数通常是创建了可以被调用的函数,它返回了函数,而并没有将这个函 ...

  4. 使用FastReport报表工具生成图片格式文档

    之前我在随笔<使用FastReport报表工具生成报表PDF文档>介绍过使用FastReport.Net来根据报表模板进行生成PDF,以及随笔<使用FastReport报表工具生成标 ...

  5. https揭秘

    首先简要说明一下所谓的https证书是什么东西:打个比方,你第一次去银行办理业务的时候都需要手持本人身份中去办理业务,这个身份证从哪里来呢,没错,是从国家相关机关得来的,在中国内是通用的,类比到htt ...

  6. 替换节点(replaceChild())

    replaceChild():方法将把一个给定父元素里面的一个子节点替换为另一个子节点: referencre = element.replaceChild(newChild,oldChild); o ...

  7. c# 窗体开发2 高级控件的使用

    1.单选按钮(RadioButton) 同一组中其他单选按钮不能同时选定 分组形式:panel GoupBox 窗体 方法: 属性 说明 Appearance RadioButton 控件的显示与命令 ...

  8. Solr搜索引擎【索引管理】

    一.索引存储 当文档提交到索引之后,directory目录组件会将它们写入到持久化存储器.Solr的目录组件具有以下重要特点: 1.隐藏持久存储的读写细节,例如,将文档写入到磁盘或通过JDBC在数据库 ...

  9. css优先级 中文版MDN补充翻译

    原文地址:https://developer.mozilla.org/zh-CN/docs/Web/CSS/Specificity css的MDN中文版,这一页是讲css的优先级的. 读到文章的最后, ...

  10. Date、Calendar和GregorianCalendar的使用

    java.util 包提供了 Date 类来封装当前的日期和时间. Date 类提供两个构造函数来实例化 Date 对象. 第一个构造函数使用当前日期和时间来初始化对象. Date public st ...