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, cc represents [ 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 徐州赛区网络预赛

题意:两种操作 一种是更新某节点

一种是查询l-r的一个值 这个值的计算公式是:

思路:

看上去就应该是一个线段树 区间查询单点更新

但是对于查询的处理没有那么简单

可以采用前缀和的思想 因为这个公式中的系数是递减的

那么我们在线段树中维护两个值 一个是本身的值 一个是(n-i+1)倍的值

那么我们查询的时候只需要查到倍数之和 减去 本身之和的(n-r)倍就可以了

WA了一会 因为没用long long

还是要注意啊这些细节 虽然单个没有超int 但是相加就会超的


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<set>
//#include<bits/stdc++.h>
#define inf 0x7f7f7f7f7f7f7f7f
using namespace std;
typedef long long LL; const int maxn = 1e5 + 10;
LL tree[maxn << 2], treetime[maxn << 2], a[maxn];
int n, q; void pushup(int rt)
{
tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
treetime[rt] = treetime[rt << 1] + treetime[rt << 1 | 1];
} void build(int rt, int l, int r)
{
if (l == r) {
tree[rt] = a[l];
treetime[rt] = a[l] * (n - l + 1);
return;
}
int m = (l + r) / 2;
build(rt << 1, l, m);
build(rt << 1 | 1, m + 1, r);
pushup(rt);
} void update(int x, LL val, int l, int r, int rt)
{
if (l == r) {
tree[rt] = val;
treetime[rt] = val * (n - l + 1);
return;
}
int m = (l + r) / 2;
if (x <= m) {
update(x, val, l, m, rt << 1);
}
else {
update(x, val, m + 1, r, rt << 1 | 1);
}
pushup(rt);
} LL query(int L, int R, int l, int r, int rt)
{
if (L <= l && R >= r) {
return tree[rt];
}
int m = (l + r) / 2;
LL ans = 0;
if (L <= m) {
ans += query(L, R, l, m, rt << 1);
}
if (R > m) {
ans += query(L, R, m + 1, r, rt << 1 | 1);
}
pushup(rt);
return ans;
} LL querytime(int L, int R, int l, int r, int rt)
{
if (L <= l && R >= r) {
return treetime[rt];
}
int m = (l + r) / 2;
LL ans = 0;
if (L <= m) {
ans += querytime(L, R, l, m, rt << 1);
}
if (R > m) {
ans += querytime(L, R, m + 1, r, rt << 1 | 1);
}
return ans;
} void init()
{
memset(tree, 0, sizeof(tree));
memset(treetime, 0, sizeof(treetime));
} int main()
{
while (scanf("%d%d", &n, &q) != EOF) {
if(n == 0){
while(q--){
int op, l, r;
scanf("%d%d%d", &op, &l, &r);
printf("0\n");
}
continue;
}
init();
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
build(1, 1, n); for (int i = 0; i < q; i++) {
int op, l, r;
scanf("%d%d%d", &op, &l, &r);
if (op == 1) {
LL ans = querytime(l, r, 1, n, 1) - (n - r) * query(l, r, 1, n, 1);
printf("%lld\n", ans);
}
else {
update(l, r, 1, n, 1);
}
}
}
return 0;
}

徐州网络赛H-Ryuji doesn't want to study【线段树】的更多相关文章

  1. 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] ...

  2. 2018徐州网络赛H. Ryuji doesn't want to study

    题目链接: https://nanti.jisuanke.com/t/31458 题解: 建立两个树状数组,第一个是,a[1]*n+a[2]*(n-1)....+a[n]*1;第二个是正常的a[1], ...

  3. 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 ...

  4. 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 ...

  5. 计蒜客 31460 - Ryuji doesn't want to study - [线段树][2018ICPC徐州网络预赛H题]

    题目链接:https://nanti.jisuanke.com/t/31460 Ryuji is not a good student, and he doesn't want to study. B ...

  6. ACM-ICPC 2018 徐州赛区网络预赛 H. 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, ea ...

  7. 南昌网络赛 I. Max answer (单调栈 + 线段树)

    https://nanti.jisuanke.com/t/38228 题意给你一个序列,对于每个连续子区间,有一个价值,等与这个区间和×区间最小值,求所有子区间的最大价值是多少. 分析:我们先用单调栈 ...

  8. ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn't want to study

    死于update的一个long long写成int了 真的不想写过程了 ******** 树状数组,一个平的一个斜着的,怎么斜都行 题库链接:https://nanti.jisuanke.com/t/ ...

  9. 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 ...

  10. 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[ ...

随机推荐

  1. Spring的p标签

    看Spring in action的时候看过p标签,可惜这东西不用就忘. p标签是为了简化setter的注入而引入的. 用法: p:属性 = "{值}" p:属性-ref = &q ...

  2. IoC就是IoC,不是什么技术,与GoF一样,是一种 设计模式。

    IoC就是IoC,不是什么技术,与GoF一样,是一种 设计模式. Interface Driven Design接口驱动,接口驱动有很多好处,可以提供不同灵活的子类实现,增加代码稳定和健壮性等等,但是 ...

  3. REFLECTOR和FILEDISASSEMBLER的下载与使用

    .NET Reflector 下载地址 http://www.aisto.com/roeder/dotnet FileDisassembler 下载地址 http://www.denisbauer.c ...

  4. shell脚本中判断一个字符串是否是空字符串

    需求说明: 在写脚本的时候,有的时候,需要判断一个字符串是否为空,因此,在此写出如何判断一个字符串为空的方法. 简单来说,就是字符串的比较. 测试脚本: 以下的脚本用于测试str_1和str_2是否是 ...

  5. 关于代理ip

    反爬很重要的手段之一就是根据ip来了,包括新浪微博搜索页 微信搜索页 360全系网站360搜索 360百科 360 问答 360新闻,这些都是明确的提示了是根据ip反扒的,所以需要买ip.买得是快代理 ...

  6. git for c#, commit本地,pushserver

    //ok private static void push() { string wkDir = @"E:\DotNet2010\单位project\Git.Client\lib2Test\ ...

  7. Java精选笔记_IO流(字节流、InputStream、OutputStream、字节文件、缓冲区输入输出流)

    字节流 操作图片数据就要用到字节流. 字符流使用的是字符数组char[],字节流使用的是字节数组byte[]. 字节流读写文件 针对文件的读写,JDK专门提供了两个类,分别是FileInputStre ...

  8. swift--触摸(UITouch)事件(点击,移动,抬起)

    触摸事件: UITouch:一个手机第一次点击屏幕,会形成一个UITouch对象,知道离开销毁.表示触碰.UITouch对象能表明当前手指触碰的屏幕位置.状态,状态分为开始触碰.移动.离开. 具体方法 ...

  9. Hash表(hash table ,又名散列表)

    直接进去主题好了. 什么是哈希表? 哈希表(Hash table,也叫散列表),是根据key而直接进行访问的数据结构.也就是说,它通过把key映射到表中一个位置来访问记录,以加快查找的速度.这个映射函 ...

  10. Android 使用GridView以表格的形式显示多张图片

    GridView用于在界面上按行.列分布的方式来显示多个组件(而ListView只是以按行的方式) 课程目标 学会使用GridView制作二维布局界面(行.列分布) 数据源(集合) --> 适配 ...