线段树模板(区间和,区间最值)

· · 个人记录

//区间和线段树
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const ll maxn=5*1e5+5;
ll n,m,a[maxn],f[maxn*4],mrk[maxn*4],k,s,t,x;
void build(ll o,ll l,ll r){
    if(l==r){
        f[o]=a[l];
        return;
    }
    ll mid=(l+r)>>1;
    build(o*2,l,mid);
    build(o*2,mid+1,r);
    f[o]=f[o*2]+f[o*2+1]; 
}//建树 
void pushdown(ll o,ll l,ll r){
    mrk[o*2]+=mrk[o];mrk[o*2+1]+=mrk[o];
    ll mid2=(l+r)>>1; 
    f[o*2]+=(mid2-l+1)*mrk[o];f[o*2+1]+=(r-mid2)*mrk[o];
    mrk[o]=0;
}//标记传递 
void update(ll o,ll l,ll r,ll i,ll x){
    if(l==r){
        f[o]+=x;
        return;
    }
    ll mid=(l+r)>>1;
    if(i<=mid) update(o*2,l,mid,i,x);
    else update(o*2+1,mid+1,r,i,x);
    f[o]=f[o*2]+f[o*2+1];
}//单点修改 
void lupdate(ll o,ll l,ll r,ll s,ll t,ll x){
    if(l==s&&r==t){
        mrk[o]+=x,f[o]+=(t-s+1)*x;
        return;
    }
    pushdown(o,l,r);
    ll mid=(l+r)>>1;
    if(t<=mid) lupdate(o*2,l,mid,s,t,x);
    else if(s>=mid+1) lupdate(o*2+1,mid+1,r,s,t,x);
    else{
        lupdate(o*2,l,mid,s,mid,x);
        lupdate(o*2+1,mid+1,r,mid+1,t,x);
    }
    f[o]=f[o*2]+f[o*2+1];
}//区间修改 
ll lquery(ll o,ll l,ll r,ll s,ll t){
    if(l==s&&r==t) return f[o];
    pushdown(o,l,r);
    ll mid=(l+r)>>1;
    if(t<=mid) return lquery(o*2,l,mid,s,t);
    if(s>=mid+1) return lquery(o*2+1,mid+1,r,s,t);
    return lquery(o*2,l,mid,s,mid)+lquery(o*2+1,mid+1,r,mid+1,t);
    f[o]=f[o*2]+f[o*2+1];
}//区间查询 
ll query(ll o,ll l,ll r,ll i){
    if(l==r){
        return f[o]; 
    }
    pushdown(o,l,r);
    ll mid=(l+r)>>1;
    if(i<=mid) return query(o*2,l,mid,i);
    else return query(o*2+1,mid+1,r,i);
    f[o]=f[o*2]+f[o*2+1];
}//单点查询 
int main(){
    return 0;
} 
//区间最值线段树
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const ll maxn=5*1e5+5;
ll n,m,a[maxn],f[maxn*4],mrk[maxn*4],k,s,t,x;
void build(ll o,ll l,ll r){
    if(l==r){
        f[o]=a[l];
        return;
    }
    ll mid=(l+r)>>1;
    build(o*2,l,mid);
    build(o*2,mid+1,r);
    f[o]=max(f[o*2],f[o*2+1]); 
}//建树 
void pushdown(ll o,ll l,ll r){
    mrk[o*2]+=mrk[o];mrk[o*2+1]+=mrk[o];
    ll mid2=(l+r)>>1; 
    f[o*2]+=(mid2-l+1)*mrk[o];f[o*2+1]+=(r-mid2)*mrk[o];
    mrk[o]=0;
}//标记传递 
void update(ll o,ll l,ll r,ll i,ll x){
    if(l==r){
        f[o]+=x;
        return;
    }
    ll mid=(l+r)>>1;
    if(i<=mid) update(o*2,l,mid,i,x);
    else update(o*2+1,mid+1,r,i,x);
    f[o]=max(f[o*2],f[o*2+1]);
}//单点修改 
void lupdate(ll o,ll l,ll r,ll s,ll t,ll x){
    if(l==s&&r==t){
        f[o]+=x;
        return;
    }
    ll mid=(l+r)>>1;
    if(t<=mid) lupdate(o*2,l,mid,s,t,x);
    else if(s>=mid+1) lupdate(o*2+1,mid+1,r,s,t,x);
    else{
        lupdate(o*2,l,mid,s,mid,x);
        lupdate(o*2+1,mid+1,r,mid+1,t,x);
    }
    f[o]=max(f[o*2],f[o*2+1]);
}//区间修改 
ll lquery(ll o,ll l,ll r,ll s,ll t){
    if(l==s&&r==t) return f[o];
    ll mid=(l+r)>>1;
    if(t<=mid) return lquery(o*2,l,mid,s,t);
    if(s>=mid+1) return lquery(o*2+1,mid+1,r,s,t);
    return max(lquery(o*2,l,mid,s,mid),lquery(o*2+1,mid+1,r,mid+1,t));
    f[o]=max(f[o*2],f[o*2+1]);
}//区间查询 
ll query(ll o,ll l,ll r,ll i){
    if(l==r) return f[o]; 
    ll mid=(l+r)>>1;
    if(i<=mid) return query(o*2,l,mid,i);
    else return query(o*2+1,mid+1,r,i);
    f[o]=max(f[o*2],f[o*2+1]);
}//单点查询 
int main(){
    return 0;
}