zoukankan      html  css  js  c++  java
  • 更新,线段树模板(支持相关基本操作)

    #define ls p<<1
    #define rs p<<1|1
    struct Seg
    {
        int p,l,r;
        ll sum,tag,mx;
    #define l(i) t[i].l
    #define r(i) t[i].r
    #define mx(i) t[i].mx
    #define tag(i) t[i].tag
    #define sum(i) t[i].sum
    }t[N<<2];
    
    void pushup(int p)
    {
        sum(p)=sum(ls)+sum(rs);
        mx(p)=max(mx(ls),mx(rs));
    }
    void pushdown(int p)
    {
        if(tag(p))
        {
            sum(ls)+=tag(p)*(r(ls)-l(ls)+1);
            sum(rs)+=tag(p)*(r(rs)-l(rs)+1);
            mx(ls)+=tag(p);mx(rs)+=tag(p);
            tag(ls)+=tag(p);
            tag(rs)+=tag(p);
            tag(p)=0;
        }
    }
    void build(int p,int l,int r)
    {
        l(p)=l;r(p)=r;
        if(l==r)
        {
            mx(p)=sum(p)=a[l];
            return;
        }
        int mid=l+r>>1;
        build(ls,l,mid);
        build(rs,mid+1,r);
        pushup(p);
    }
    void update(int p,int l,int r,ll d)
    {
        if(l<=l(p)&&r>=r(p))
        {
            sum(p)+=d*(r(p)-l(p)+1);
            mx(p)+=d;
            tag(p)+=d;
            return;
        }
        pushdown(p);
        int mid=(l(p)+r(p))>>1;
        if(l<=mid) update(ls,l,r,d);
        if(r>mid) update(rs,l,r,d);
        pushup(p);
    }
    void change(int p,int x,ll d)
    {
        if(l(p)==r(p))
        {
            mx(p)+=d;sum(p)+=d;
            return;
        }
        pushdown(p);
        int mid=(l(p)+r(p))>>1;
        if(x<=mid) change(ls,x,d);
        else change(rs,x,d);
       pushup(p); } ll query(
    int p,int l,int r) { if(l<=l(p)&&r>=r(p)) return sum(p); pushdown(p); int mid=l(p)+r(p)>>1; ll ans=0; if(l<=mid) ans+=query(ls,l,r); if(r>mid) ans+=query(rs,l,r); return ans; } ll ask(int p,int l,int r) { if(l<=l(p)&&r>=r(p)) return mx(p); pushdown(p); int mid=l(p)+r(p)>>1; ll ans=-0x3f3f3f3f3f3f3f3f; if(l<=mid) ans=max(ans,ask(ls,l,r)); if(r>mid) ans=max(ans,ask(rs,l,r)); return ans; }
  • 相关阅读:
    BZOJ 1391: [Ceoi2008]order
    BZOJ 4504: K个串
    2019 年百度之星·程序设计大赛
    POJ 2398 Toy Storage (二分 叉积)
    POJ 2318 TOYS (二分 叉积)
    HDU 6697 Closest Pair of Segments (计算几何 暴力)
    HDU 6695 Welcome Party (贪心)
    HDU 6693 Valentine's Day (概率)
    HDU 6590 Code (判断凸包相交)
    POJ 3805 Separate Points (判断凸包相交)
  • 原文地址:https://www.cnblogs.com/Suiyue-Li/p/11379748.html
Copyright © 2011-2022 走看看