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

版权声明:Dream_dog专属 https://blog.csdn.net/Dog_dream/article/details/82563085

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
8

题解:这道题想骂街感觉直接被坑了把所有变量数据改成long long 就过了 直接吐血只怪自己经验太少 直接树状数组每次的存入的值为w*(n-i+1)求和的时候只要减去本来区间的值乘(n-r)就好。

1 2 3 4 5

1 2 3 4 

1 2 3

1 2

1

5 8 9 4

假如:查询查询(1,3)=9+8-(2+3)*2;


#include<cstring>
#include<cstdio>
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<functional>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define rep(a,b,c) for(ll a=b;a<c;a++)
#define dec(a,b,c) for(int a=b;a>c;a--)
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define ps(x) push(x)
#define _INIT ios::sync_with_stdio(false);cin.tie(nullptr);cout.precision(10);cout<<fixed
#define MAX_N 100000+5
#define MAX_M 100
typedef long long ll;
typedef unsigned long long ull;
typedef priority_queue<ll,vector<ll>,greater<ll> >pqg;
const ll maxn=1e3;
const ll inf=1e7;
int n,q;
ll tree[MAX_N],tree1[MAX_N];
void update(int k,ll w,ll nums[])
{
    while(k<=n)
    {
        nums[k]+=w;
        k+=lowbit(k);
    }
    return ;
}
ll sum(int k,ll nums[])
{
    ll ans=0;
    while(k)
    {
        ans+=nums[k];
        k-=lowbit(k);
    }
    return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
    // freopen("data.txt","r",stdin);
#endif
    //_INIT;
    scanf("%d%d",&n,&q);
    clr(tree,0);
    clr(tree1,0);
    for(int i=1; i<=n; i++)
    {
        ll tm;
        scanf("%lld",&tm);
        update(i,tm*(n-i+1),tree);
        update(i,tm,tree1);
    }
    while(q--)
    {
        ll a,b,c;
        scanf("%lld%lld%lld",&a,&b,&c);
        if(a==1)
        {
            ll ans=(sum(c,tree)-sum(c,tree1)*(n-c))-(sum(b-1,tree)-(n-c)*sum(b-1,tree1));
            printf("%lld\n",ans);
        }
        else
        {
            ll w=c-(sum(b,tree1)-sum(b-1,tree1));
            update(b,w,tree1);
            update(b,w*(n-b+1),tree);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dog_dream/article/details/82563085