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] \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网络赛 树状数组

原文地址:https://www.cnblogs.com/l609929321/p/9621795.html

时间: 2024-10-11 21:01:03

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

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

There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy) means the wave is a rectangle whose vertexes are ( 00 , 00 ), ( xx , 00 ), ( 00 , yy ), ( xx , yy ). Every time the wave will wash out the trace of f

ICPC 2018 徐州赛区网络赛

ACM-ICPC 2018 徐州赛区网络赛 ?去年博客记录过这场比赛经历:该死的水题 ?一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进. ? ? D. Easy Math 题意: ? 给定 \(n\), \(m\) ,求 \(\sum _{i=1}^{m} \mu(in)\) .其中 $ 1 \le n \le 1e12$ , $ 1 \le m \le 2e9$ ,\(\mu(n)\) 为莫比乌斯函数. ? 思路: ? 容易知道,\(i\) 与 \(n\) 不互质时, \(\m

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

A. Hard to prepare After Incident, a feast is usually held in Hakurei Shrine. This time Reimu asked Kokoro to deliver a Nogaku show during the feast. To enjoy the show, every audience has to wear a Nogaku mask, and seat around as a circle. There are

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn&#39;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 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 wi

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn&#39;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

ACM-ICPC 2018 徐州赛区网络预赛 D. EasyMath

ACM-ICPC 2018 徐州赛区网络预赛 D. EasyMath 做法: \[f(m,n) = \sum _{i=1}^{m} \mu(in) = \sum_{i=1}^{m}[gcd(i,n)=1]\mu(i)\mu(n) = \mu(n)\sum_{d|n}\mu(d)f(\frac{m}{d},d)\] 边界: n=1,杜教筛求\(\sum_{i=1}^{m}\mu(i)\),m = 1, 返回\(\mu(n)\),预处理尽可能把空间卡满. 2个小时的时候就推出来了这个式子,不会算复杂

ACM-ICPC 2018 徐州赛区网络预赛 B. BE, GE or NE

In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named "Sena" are playing a video game. The game system of this video game is quite unique: in the process of playing this game, you need to constantly fac

ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer (最大生成树+LCA求节点距离)

ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer master has to do his job. A tour company gives him a map which is a rectangle. The map consists of N \times MN×M little squares. That is to say, the h

2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ&#39;s Salesman 【离散化+树状数组维护区间最大值】

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 919    Accepted Submission(s): 290 Problem Description YJJ is a salesman who h