zoukankan      html  css  js  c++  java
  • [BZOJ3155] Preprefix sum

    [BZOJ3155] Preprefix sum

    Description

    Input

    第一行给出两个整数N,M。分别表示序列长度和操作个数接下来一行有N个数,即给定的序列a1,a2,....an接下来M行,每行对应一个操作,格式见题目描述

    Output

    对于每个询问操作,输出一行,表示所询问的SSi的值。

    Sample Input

    5 3
    1 2 3 4 5
    Query 5
    Modify 3 2
    Query 5

    Sample Output

    35
    32

    试题分析

    题目要求:$$sum_{i=1}^{n} sum_{j=1}^{i} a_j$$。
    转化一下可以变成:$$n imes sum_{i=1}^{n} a_i - sum_{i=1}{n}sum_{j=i+1}{n} a_i$$
    然后这个式子就可以两个树状数组维护了。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
     
    using namespace std;
    #define LL long long
     
    inline LL read(){
        LL x=0,f=1; char c=getchar();
        for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
        for(;isdigit(c);c=getchar()) x=x*10+c-'0';
        return x*f;
    }
    const LL INF = 2147483600;
    const LL MAXN = 101000;
     
    LL N,M; LL c[2][MAXN+1],a[MAXN+1];
    char str[MAXN+1];
     
    inline LL lowbit(LL x){return x&(-x);}
    inline LL Query(LL t,LL x){
        LL res=0;
        while(x) res+=c[t][x],x-=lowbit(x); return res;
    }
    inline void Add(LL t,LL x,LL k){
        while(x<=N) c[t][x]+=k,x+=lowbit(x); return ;
    }
     
    int main(){
        //freopen(".in","r",stdin);
        //freopen(".out","w",stdout);
        N=read(),M=read();
        for(LL i=1;i<=N;i++) a[i]=read(),Add(0,i,(1-i)*a[i]),Add(1,i,a[i]);
        while(M--){
            scanf("%s",str);
            if(str[0]=='Q'){
                LL n=read();
                printf("%lld
    ",1LL*n*Query(1,n)+Query(0,n));
            }
            else{
                LL n=read(),x=read();
                Add(0,n,(1-n)*(x-a[n])); Add(1,n,x-a[n]);
                a[n]=x;
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    洛谷 简单字符串 'P1055ISBN号码' 问题
    P1424
    洛谷训练P1008(循环+暴力)
    C语言中一些知识点
    二叉树的基本功能实现方法(C++)
    值传递,引用传递,指针传递
    istringstream、ostringstream、stringstream类介绍
    类模板的友元
    C++ 标准库和标准模板库(STL)
    常用数据类型对应字节数
  • 原文地址:https://www.cnblogs.com/wxjor/p/9532095.html
Copyright © 2011-2022 走看看