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 徐州赛区网络预赛

题意:有n个数,你可以对这n个数进行m次操作,可以进行的操作有两种,第一种将b位置的数置为c,第二种求a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r]

分析:注意表达式:a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r]可以化为:

    

  化成这个式子后,我们每次只要维护a[i]*(n-i+1)和a[i]就可以了,更新的时候加上c-a[i]就可以将a[i]更新为c

做题目做少了,打网络赛的时候都没有想到这样化简式子

参考博客:https://blog.csdn.net/qq_39826163/article/details/82586489

AC代码:

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e5+10;
const ll mod = 2e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll n, m, q, a[maxn], t1[maxn], t2[maxn];
ll lowbit( ll x ) {
return x&(-x);
}
void update1( ll x, ll v ) {
while( x <= n ) {
t1[x] += v;
x += lowbit(x);
}
}
void update2( ll x, ll v ) {
while( x <= n ) {
t2[x] += v;
x += lowbit(x);
}
}
ll query1( ll x ) {
ll sum = 0;
while(x) {
sum += t1[x];
x -= lowbit(x);
}
return sum;
}
ll query2( ll x ) {
ll sum = 0;
while(x) {
sum += t2[x];
x -= lowbit(x);
}
return sum;
}
int main() {
scanf("%lld%lld",&n,&m);
for( ll i = 1; i <= n; i ++ ) {
scanf("%lld",&a[i]);
update1(i,a[i]);
update2(i,a[i]*(n-i+1));
}
while( m -- ) {
ll k, b, c;
scanf("%lld%lld%lld",&k,&b,&c);
if( k == 1 ) {
ll sum1 = query1(c) - query1(b-1);
ll sum2 = query2(c) - query2(b-1);
ll ans = sum2-(n-c)*sum1;
printf("%lld\n",ans);
} else {
update1(b,c-a[b]);
update2(b,(c-a[b])*(n-b+1));
a[b] = c;
}
}
return 0;
}

  

Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组的更多相关文章

  1. Trace 2018徐州icpc网络赛 (二分)(树状数组)

    Trace There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx ...

  2. Trace 2018徐州icpc网络赛 思维+二分

    There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy) ...

  3. Features Track 2018徐州icpc网络赛 思维

    Morgana is learning computer vision, and he likes cats, too. One day he wants to find the cat moveme ...

  4. Ryuji doesn't want to study 2018 徐州赛区网络预赛

    题意: 1.区间求 a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r](L is the length of [ l, r ] that equals to r - l + 1) ...

  5. ICPC 2018 徐州赛区网络赛

    ACM-ICPC 2018 徐州赛区网络赛  去年博客记录过这场比赛经历:该死的水题  一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进.     D. Easy Math 题意:   ...

  6. ACM-ICPC 2018 徐州赛区(网络赛)

    目录 A. Hard to prepare B.BE, GE or NE F.Features Track G.Trace H.Ryuji doesn't want to study I.Charac ...

  7. Supreme Number 2018沈阳icpc网络赛 找规律

    A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...

  8. 2019 徐州icpc网络赛 E. XKC's basketball team

    题库链接: https://nanti.jisuanke.com/t/41387 题目大意 给定n个数,与一个数m,求ai右边最后一个至少比ai大m的数与这个数之间有多少个数 思路 对于每一个数,利用 ...

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

随机推荐

  1. Nginx 的简单使用 (IIS,Asp.Net)

    Nginx 的一些常见功能(windows,AspNet ,IIS) 下载 官方网站:https://nginx.org/en/download.html 下载,解压缩是这个样子 启动: 启动方式有两 ...

  2. Linux系统命令。

      help:命令用于显示shell内部命令的帮助信息.help命令只能显示shell内部的命令              帮助信息.而对于外部命令的帮助信息只能使用man或者info命令查看   m ...

  3. unity3d立方体碰撞检测(c#代码实现)

    由于unity自带的碰撞组件特别耗费性能,网上的unity物体碰撞的c#代码实现比较少,没有适合的,只能自己写一个来用: using System; using System.Collections. ...

  4. 手动编译PHP开发环境

    目录 手动编译PHP开发环境 问题复盘 部署环境及配置 目标环境 安装部署环境开始 首先安装PHP 安装mysql 安装nginx 手动编译PHP开发环境 这是一篇来自深夜加班的手稿 问题复盘 你有没 ...

  5. Android--SharedPreferences数据存储方案

            SharedPreferences是使用键值对的形式存储的,并且支持多种不同的数据类型,存的是String,取得值也是String.         使用SharedPreferenc ...

  6. 用HTML5的Audio标签做一个歌词同步的效果

    HTML5出来这么久了,但是关于它里面的audio标签也就用过那么一次,当然还仅仅只是把这个标签插入到了页面中.这次呢就刚好趁着帮朋友做几个页面,拿这个audio标签来练练手. 首先你需要向页面中插入 ...

  7. Leetcode solution 227: Basic Calculator II

    Problem Statement Implement a basic calculator to evaluate a simple expression string. The expressio ...

  8. CSS等分布局方法

    原文链接:http://caibaojian.com/css-equal-layout.html CSS等比例划分,在CSS布局中是比较重要的,下面分享几种常用方法和探讨一下兼容性. 一:浮动布局+百 ...

  9. 一文了解:Redis过期键删除策略

    Redis过期键删除策略 Redis中所有的键都可以设置过期策略,就像是所有的键都可以上"生死簿",上了生死簿的键到时间后阎王就会叉掉这个键.同一时间大量的键过期,阎王就会忙不过来 ...

  10. JVM类生命周期概述:加载时机与加载过程

    一个.java文件在编译后会形成相应的一个或多个Class文件,这些Class文件中描述了类的各种信息,并且它们最终都需要被加载到虚拟机中才能被运行和使用.事实上,虚拟机把描述类的数据从Class文件 ...