一、建树、单点修改、单点查询、区间修改、区间查询:
单点修改、区间查询:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 500010;
struct node
{
    int sum;
    int l,r;
}tree[maxn*4];
int a[maxn];
void bulid(int l,int r,int cnt)
{
    tree[cnt].l=l;tree[cnt].r=r;
    if(l==r)
    {
        tree[cnt].sum=a[l];
        return ;
    }
    int mid=(l+r)/2;
    bulid(l,mid,cnt*2);
    bulid(mid+1,r,cnt*2+1);
    tree[cnt].sum=tree[cnt*2].sum+tree[cnt*2+1].sum;
    return ;
}

void change(int x,int val,int cnt)
{
    if(tree[cnt].l==tree[cnt].r)
    {
        tree[cnt].sum+=val;
        return ;
    }
    //tree[cnt].sum+=val;
    if(x<=tree[cnt*2].r) change(x,val,cnt*2);
    else change(x,val,cnt*2+1);
    tree[cnt].sum=tree[cnt*2].sum+tree[cnt*2+1].sum;
    return ;
}

int ask(int l,int r,int cnt)
{
    if(l<=tree[cnt].l&&tree[cnt].r<=r)
    {
        return tree[cnt].sum;
    }

    int sum=0;
    if(l<=tree[cnt*2].r) sum+=ask(l,r,cnt*2);
    if(r>=tree[cnt*2+1].l) sum+=ask(l,r,cnt*2+1);
    return sum;
}

int main(void)
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    bulid(1,n,1);

    int t,x,y;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&t,&x,&y);
        if(t==1)
        {
            change(x,y,1);
        }
        else printf("%d\n",ask(x,y,1));
    }
    return 0;
}


区间修改、单点查询:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 500010;
struct node
{
    int sum;
    int l,r;
    int laz=0;
}tree[maxn*4];
int a[maxn];
void bulid(int l,int r,int cnt)
{
    tree[cnt].l=l;tree[cnt].r=r;
    if(l==r)
    {
        tree[cnt].sum=a[l];
        return ;
    }
    int mid=(l+r)/2;
    bulid(l,mid,cnt*2);
    bulid(mid+1,r,cnt*2+1);
    tree[cnt].sum=tree[cnt*2].sum+tree[cnt*2+1].sum;
    return ;
}

void pushdown(int cnt)
{
    if(tree[cnt].laz)
    {
        tree[cnt*2].laz+=tree[cnt].laz;
        tree[cnt*2+1].laz+=tree[cnt].laz;
        tree[cnt*2].sum+=(tree[cnt*2].r-tree[cnt*2].l+1)*tree[cnt].laz;
        tree[cnt*2+1].sum+=(tree[cnt*2+1].r-tree[cnt*2+1].l+1)*tree[cnt].laz;
        tree[cnt].laz=0;
    }
    return ;
}

void change(int l,int r,int val,int cnt)
{
   if(l<=tree[cnt].l&&tree[cnt].r<=r)
   {
       tree[cnt].sum+=(tree[cnt].r-tree[cnt].l+1)*val;
       tree[cnt].laz+=val;
       return ;
   }
   pushdown(cnt);
   if(l<=tree[cnt*2].r) change(l,r,val,cnt*2);
   if(r>=tree[cnt*2+1].l) change(l,r,val,cnt*2+1);
   tree[cnt].sum=tree[cnt*2].sum+tree[cnt*2+1].sum;
}

int ask(int pos,int cnt)
{
    if(tree[cnt].l==tree[cnt].r)
    {
        return tree[cnt].sum;
    }
    pushdown(cnt);
    if(pos<=tree[cnt*2].r) return ask(pos,cnt*2);
    else return ask(pos,cnt*2+1);
}

int main(void)
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    bulid(1,n,1);

    int t,x,y,z;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&t);
        if(t==1)
        {
            scanf("%d%d%d",&x,&y,&z);
            change(x,y,z,1);
        }
        else
        {
            scanf("%d",&x);
            printf("%d\n",ask(x,1));
        }
    }
    return 0;
}


区间修改、区间查询:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 500010;
struct node
{
    ll sum;
    int l,r;
    ll laz=0;
}tree[maxn*4];
int a[maxn];
void bulid(int l,int r,int cnt)
{
    tree[cnt].l=l;tree[cnt].r=r;
    if(l==r)
    {
        tree[cnt].sum=a[l];
        return ;
    }
    int mid=(l+r)/2;
    bulid(l,mid,cnt*2);
    bulid(mid+1,r,cnt*2+1);
    tree[cnt].sum=tree[cnt*2].sum+tree[cnt*2+1].sum;
    return ;
}

void pushdown(int cnt)
{
    if(tree[cnt].laz)
    {
        tree[cnt*2].laz+=tree[cnt].laz;
        tree[cnt*2+1].laz+=tree[cnt].laz;
        tree[cnt*2].sum+=(tree[cnt*2].r-tree[cnt*2].l+1)*tree[cnt].laz;
        tree[cnt*2+1].sum+=(tree[cnt*2+1].r-tree[cnt*2+1].l+1)*tree[cnt].laz;
        tree[cnt].laz=0;
    }
    return ;
}

void change(int l,int r,ll val,int cnt)
{
   if(l<=tree[cnt].l&&tree[cnt].r<=r)
   {
       tree[cnt].sum+=(tree[cnt].r-tree[cnt].l+1)*val;
       tree[cnt].laz+=val;
       return ;
   }
   pushdown(cnt);
   if(l<=tree[cnt*2].r) change(l,r,val,cnt*2);
   if(r>=tree[cnt*2+1].l) change(l,r,val,cnt*2+1);
   tree[cnt].sum=tree[cnt*2].sum+tree[cnt*2+1].sum;
}

ll ask(int l,int r,int cnt)
{
    if(l<=tree[cnt].l&&tree[cnt].r<=r)
    {
        return tree[cnt].sum;
    }
    pushdown(cnt);
    ll ans=0;
    if(l<=tree[cnt*2].r) ans+=ask(l,r,cnt*2);
    if(r>=tree[cnt*2+1].l) ans+=ask(l,r,cnt*2+1);
    return ans;
}

int main(void)
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    bulid(1,n,1);

    int t,x,y;
    ll z;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&t);
        if(t==1)
        {
            scanf("%d%d%lld",&x,&y,&z);
            change(x,y,z,1);
        }
        else
        {
            scanf("%d%d",&x,&y);
            printf("%lld\n",ask(x,y,1));
        }
    }
    return 0;
}