c++ 模板库(bushi

· · 算法·理论

快读&快写

template<typename TP,typename... TPS>
void PRINT(const TP& x,const TPS&... y){cout<<x<<' ';PRINT(y...);}
template<typename T=int>
inline T read(){
    T X=0; bool flag=1; char ch=getchar();
    while(ch<'0' || ch>'9'){if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0' && ch<='9') X=(X<<1)+(X<<3)+ch-'0',ch=getchar();
    if(flag) return X;
    return ~(X-1);
}

template<typename T=int>
inline void write(T X){
    if(X<0) putchar('-'),X=~(X-1);
    T s[20],top=0;
    while(X) s[++top]=X%10,X/=10;
    if(!top) s[++top]=0;
    while(top) putchar(s[top--]+'0');
    putchar('\n');
}

输入输出优化

ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

调试输出

//#define DEBUG
#ifdef DEBUG
#define print(NUM...) /*cout<<"Line "<<__LINE__<<":\n",*/cout<<#NUM<<": ",PRINT(NUM),cout<<"\n"
#else
#define print(NUM...) while (0){}
#endif
void PRINT(){}
template<typename TP,typename... TPS>
void PRINT(const TP& x,const TPS&... y){cout<<x<<' ';PRINT(y...);}

线段树

#include<bits/stdc++.h>
#define ll long long
#define ls(k) (k<<1)
#define rs(k) (k<<1|1)
#define push_up(root) tree[root]=tree[ls(root)]+tree[rs(root)]
#define N 100005
using namespace std;
ll n,m,a[N],tree[N<<2],tag[N<<2];
inline void build(ll root,ll l,ll r){
    tag[root]=0;
    if(l==r){
        tree[root]=a[l];
        return ;
    }
    ll mid=(l+r)>>1;
    build(ls(root),l,mid);
    build(rs(root),mid+1,r);
    push_up(root);
}
inline void add_tag(ll root,ll l,ll r,ll k){
    tag[root]+=k;
    tree[root]+=k*(r-l+1);
}
inline void push_down(ll root,ll l,ll r){
    if(tag[root]){
        ll mid=(l+r)>>1;
        add_tag(ls(root),l,mid,tag[root]);
        add_tag(rs(root),mid+1,r,tag[root]);
        tag[root]=0;
    }
}
inline void update(ll L,ll R,ll root,ll l,ll r,ll k){
    if(L<=l&&r<=R){
        add_tag(root,l,r,k);
    }
    else {
        push_down(root,l,r);
        ll mid=(l+r)>>1;
        if(L<=mid){
            update(L,R,ls(root),l,mid,k);
        }
        if(mid<R){
            update(L,R,rs(root),mid+1,r,k);
        }
        push_up(root);
    }
}
inline ll query(ll L,ll R,ll root,ll l,ll r){
    if(L<=l&&r<=R){
        return tree[root];
    }
    push_down(root,l,r);
    ll res=0,mid=(l+r)>>1;
    if(L<=mid){
        res+=query(L,R,ls(root),l,mid);
    }
    if(mid<R){
        res+=query(L,R,rs(root),mid+1,r);
    }
    return res;
}
int main(){
    cin>>n>>m;
    for(ll i=1;i<=n;i++){
        cin>>a[i];
    }
    build(1,1,n);
    while(m--){
        ll op,x,y;
        cin>>op>>x>>y;
        if(op==1){
            ll k;
            cin>>k;
            update(x,y,1,1,n,k);
        }
        else {
            cout<<query(x,y,1,1,n)<<'\n';
        }
    }
    return 0;
}

树状数组

#include<bits/stdc++.h>
using namespace std;
#define N 500005
int n,a[N],c[N];
inline int lowbit(int x){
    return x&(-x);
}
inline void add(int i,int k){
    while(i<=n){
        c[i]+=k;
        i+=lowbit(i);
    }
}
inline void build(int *a){
    for(int i=1;i<=n;i++){
        add(i,a[i]);
    }
}
inline int pre_sum(int i){
    int sum=0;
    while(i>=1){
        sum+=c[i];
        i-=lowbit(i);
    }
    return sum;
}
inline int get_sum(int l, int r){
    return pre_sum(r)-pre_sum(l-1);
}
inline int query(int i){
    int ans=c[i],lca=i-lowbit(i);
    i--;
    while(i!=lca){
        ans=ans-c[i];
        i-=lowbit(i);
    }
    return ans;
}
int main() {
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    build(a);
    return 0;
}