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;
    }
    
    
  • 相关阅读:
    Python学习(一)
    使用OpenSSL生成CSR文件,并申请全球通用SSL证书
    windows 8.0 mysql 修改root 密码
    redis 本地安装
    spring boot(三)Junit 测试controller
    idea自动生成serialversionUID
    spring boot(二)热部署
    spring boot(一)创建项目
    Oracle中dual表的用途介绍
    Pass Data Between ASP.NET Pages
  • 原文地址:https://www.cnblogs.com/wxjor/p/9532095.html
Copyright © 2011-2022 走看看