码迷,mamicode.com
首页 > 编程语言 > 详细

luogu_P3374 【模板】树状数组 1

时间:2019-11-02 16:02:17      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:printf   单点   getchar   clu   name   with   return   fine   区间   

操作:区间和询问,单点增加

#include<iostream>
#include<cstdio>

#define ri register int
#define u int

namespace opt {

    inline u in() {
        u x(0),f(1);
        char s(getchar());
        while(s<0||s>9) {
            if(s==-) f=-1;
            s=getchar();
        }
        while(s>=0&&s<=9) {
            x=(x<<1)+(x<<3)+s-0;
            s=getchar();
        }
        return x*f;
    }

}

using opt::in;

#define NN 500005

namespace mainstay {

    u N,K;
    
    u c[NN];
    
    inline u ask(const u &x){
        u _re(0);
        for(ri i(x);i;i-=i&-i) _re+=c[i];
        return _re;
    }
    
    inline void add(const u &x,const u &y){
        for(ri i(x);i<=N;i+=i&-i) c[i]+=y;
    }

    inline void solve() {
        N=in(),K=in();
        for(ri i(1);i<=N;++i){
            u _a(in());
            add(i,_a);
        }
        for(ri i(1);i<=K;++i){
            u _k(in()),_a(in()),_b(in());
            if(_k==1){
                add(_a,_b);
            }
            else printf("%d\n",ask(_b)-ask(_a-1));
        }
    }

}

int main() {

    //freopen("x.txt","r",stdin);
    std::ios::sync_with_stdio(false);
    mainstay::solve();

}

 

luogu_P3374 【模板】树状数组 1

标签:printf   单点   getchar   clu   name   with   return   fine   区间   

原文地址:https://www.cnblogs.com/ling-zhi/p/11782500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!