Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组

Posted l609929321

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组相关的知识,希望对你有一定的参考价值。

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] imes L + a[l+1] imes (L-1) + cdots + a[r-1] imes 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 100000q100000).

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
",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网络赛 树状数组的主要内容,如果未能解决你的问题,请参考以下文章

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

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

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study (线段树)

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

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

Kanade Doesn‘t Want to Learn CGccpc网络赛 计算机几何 未完成