Giter Site home page Giter Site logo

fafqarr's Introduction

fafqarr

支持快速单点修改,区间加,区间求和的数组


fafqarr,即 fast_add_fast_query_array (原谅我小学英语水平),支持在 $O(\log n)$ 的时间复杂度上单点修改,区间加,区间求和的数组

内部由“超级树状数组”实现

用法:

fafqarr<long long,5e5> a;
//定义一个元素种类为long long,元素个数为5e5的fafqarr
a[3]=1;
//将a[3]赋值为1
a[4]+=3;
//将a[4]加上3
a[4]-=3;
//将a[4]减去3
a(2,5)=1;
//错误,不支持区间赋值
a(2,5)+=2;
//将区间[2,5]加上2
a(2,5)-=2;
//将区间[2,5]减去2
int tmp=a[3];
//查询a[3]的值
cout<<a[3];
//可以直接输出
int tmp=a(2,5);
cout<<a(2,5);
//查询区间的和同理

a[{x,y}]、a(x)分别与a(x,y)、a[x]用法相同,可根据自己习惯取用

实例:(线段树1

#include<bits/stdc++.h>
#include"fafqarr.h"
using namespace std;
#define F(i,j,k) for(int i=j;i<=k;i++)
fafqarr<long long,(int)5e5+5> q;
int n,m,x,y,k,op;
int main(){
    cin>>n>>m;
    F(i,1,n) cin>>x,q[i]=x;
    F(i,1,m) {
        cin>>op>>x>>y;
        if(op==1) cin>>k,q(x,y)+=k;
        else cout<<q(x,y)<<endl;
    }
    return 0;
}

注:

  • 运算时可能会溢出(不单单是求和的溢出,有可能求和没有溢出,但是因为实现的原因会溢出

  • afafqarrxy 为整数,则调用 a[{x,y}]a(x,y) 时必须保证 $1\leq x\leq y\leq \text{maxn}$ ,调用 a[x]a(x) 时必须保证 $1\leq x\leq \text{maxn}$(即 有效下标为1~maxn

在luogu blog中查看

fafqarr's People

Contributors

konyakest avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.