ACM 线段树模板(模板)

#include <iostream>

using namespace std;

struct Node
{
    int Sum;
    int Delay;
    Node *pLeft,*pRight;
};

void Init(Node **pNode,int Left,int Right)
{
    Node *pNew=new Node;

    if(Right-Left==1)
    {
        pNew->Delay=pNew->Sum=0;
        pNew->pLeft=pNew->pRight=NULL;
    }
    else
    {
        int mid=(Right+Left)>>1;
        pNew->Sum=0;
        pNew->Delay=0;
        Init(&pNew->pLeft,Left,mid);
        Init(&pNew->pRight,mid,Right);
    }
    *pNode=pNew;
}

void Add(Node *pNode,int ID,int val,int Left,int Right)
{
    if(Right-Left==1)
    {
        pNode->Sum+=val;
    }
    else
    {
        int mid=(Left+Right)>>1;

        if(ID<mid)
        {
            Add(pNode->pLeft,ID,val,Left,mid);
        }
        else
        {
            Add(pNode->pRight,ID,val,mid,Right);
        }
        pNode->Sum=pNode->pLeft->Sum+pNode->pRight->Sum;
    }
}

void Add(Node *pNode,int a,int b,int val,int Left,int Right)
{
    if(a<=Left && Right>=b)
    {
        pNode->Sum+=val*(Right-Left);
        pNode->Delay+=val;
    }
    else
    {
        int mid=(Right+Left)>>1;
        if(pNode->Delay)
        {
            pNode->pLeft->Sum+=pNode->Delay*(mid-Left);
            pNode->pLeft->Delay+=pNode->Delay;
            pNode->pRight->Sum+=pNode->Delay*(Right-mid);
            pNode->pRight->Delay+=pNode->Delay;
            pNode->Delay=0;
        }

        if(a<mid)
            Add(pNode->pLeft,a,b,val,Left,mid);
        if(b>mid)
            Add(pNode->pRight,a,b,val,mid,Right);
        pNode->Sum=pNode->pLeft->Sum+pNode->pRight->Sum;
    }
}

int Sum(Node *pNode,int a,int b,int Left,int Right)
{
    if(a<=Left && Right<=b)
    {
        return pNode->Sum;
    }
    else
    {
        cout<<Left<<","<<Right<<endl;
        int s1=0,s2=0;
        int mid=(Left+Right)>>1;

        if(a<mid)
            s1=Sum(pNode->pLeft,a,b,Left,mid);
        if(b>mid)
            s2=Sum(pNode->pRight,a,b,mid,Right);

        return s1+s2;
    }
}

int Sum2(Node *pNode,int a,int b,int Left,int Right)
{
    if(a<=Left && Right<=b)
    {
        cout<<Left<<","<<Right<<endl;
        return pNode->Sum;
    }
    else
    {
        int s1=0,s2=0;
        int mid=(Left+Right)>>1;
        if(pNode->Delay)
        {
            pNode->pLeft->Sum+=pNode->Delay*(mid-Left);
            pNode->pLeft->Delay+=pNode->Delay;
            pNode->pRight->Sum+=pNode->Delay*(Right-mid);
            pNode->pRight->Delay+=pNode->Delay;
            pNode->Delay=0;
        }

        if(a<mid)
            s1=Sum2(pNode->pLeft,a,b,Left,mid);
        if(b>mid)
            s2=Sum2(pNode->pRight,a,b,mid,Right);

        return s1+s2;
    }
}

int main()
{
    int n;

    Node *pNode;
    Init(&pNode,0,5);
    Add(pNode,0,1,0,5);
    Add(pNode,1,2,0,5);
    Add(pNode,2,3,0,5);
    Add(pNode,3,4,0,5);
    Add(pNode,4,5,0,5);
    Add(pNode,0,3,5,0,5);
    cout<<Sum2(pNode,0,3,0,5);

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ACM线段树模板C ```c #include<bits/stdc++.h> using namespace std; const int N=1e5+5;//数组开大一点 int n,m; int a[N]; struct node{ int l,r;//左右端点 int sum,lazy;//区间和和懒标记 }t[N*4]; void up(int p){//向上更新 t[p].sum=t[p<<1].sum+t[p<<1|1].sum; } void down(int p){//向下更新 if(t[p].lazy){ t[p<<1].lazy+=t[p].lazy; t[p<<1|1].lazy+=t[p].lazy; t[p<<1].sum+=t[p].lazy*(t[p<<1].r-t[p<<1].l+1); t[p<<1|1].sum+=t[p].lazy*(t[p<<1|1].r-t[p<<1|1].l+1); t[p].lazy=0; } } void build(int p,int l,int r){//建树 t[p].l=l,t[p].r=r; if(l==r){ t[p].sum=a[l]; return; } int mid=(l+r)/2; build(p<<1,l,mid); build(p<<1|1,mid+1,r); up(p); } void change(int p,int l,int r,int k){//单点修改 if(t[p].l==t[p].r){ t[p].sum=k; return; } down(p); int mid=(t[p].l+t[p].r)/2; if(r<=mid) change(p<<1,l,r,k); else if(l>mid) change(p<<1|1,l,r,k); else{ change(p<<1,l,mid,k); change(p<<1|1,mid+1,r,k); } up(p); } void add(int p,int l,int r,int k){//区间修改 if(t[p].l>=l&&t[p].r<=r){ t[p].sum+=k*(t[p].r-t[p].l+1); t[p].lazy+=k; return; } down(p); int mid=(t[p].l+t[p].r)/2; if(l<=mid) add(p<<1,l,r,k); if(r>mid) add(p<<1|1,l,r,k); up(p); } int query(int p,int l,int r){//区间查询 if(t[p].l>=l&&t[p].r<=r) return t[p].sum; down(p); int mid=(t[p].l+t[p].r)/2,ans=0; if(l<=mid) ans+=query(p<<1,l,r); if(r>mid) ans+=query(p<<1|1,l,r); return ans; } int main(){ cin>>n>>m; for(int i=1;i<=n;i++) cin>>a[i]; build(1,1,n); for(int i=1;i<=m;i++){ int opt,x,y,k; cin>>opt; if(opt==1){ cin>>x>>y>>k; add(1,x,y,k); } if(opt==2){ cin>>x>>y; cout<<query(1,x,y)<<endl; } if(opt==3){ cin>>x>>k; change(1,x,x,k); } } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值