线段树-----模版

#include<stdio.h>
#define ll      long long
#define lson    rt<<1,l,m
#define rson    rt<<1|1,m+1,r
const int maxn=1<<20;
int n,m;
ll lz[maxn];
inline void push_down(int rt)
{
    if (!lz[rt])
        return;
    lz[rt<<1]+=lz[rt];
    lz[rt<<1|1]+=lz[rt];
    lz[rt]=0;
}
void build(int rt,int l,int r)
{
    if (l==r)
    {
        scanf("%lld",&lz[rt]);
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void update(int rt,int l,int r,int L,int R,ll v)
{
    if (L<=l&&r<=R)
    {
        lz[rt]+=v;
        return;
    }
    push_down(rt);
    int m=(l+r)>>1;
    if (L<=m)
        update(lson,L,R,v);
    if (m<R)
        update(rson,L,R,v);
}
ll query(int rt,int l,int r,int p)
{
    if (l==r)
        return lz[rt];
    push_down(rt);
    int m=(l+r)>>1;
    if (p<=m)
        return query(lson,p);
    return
    query(rson,p);
}
int main()
{
    scanf("%d%d",&n,&m);
    build(1,1,n);
    for (int i=1;i<=m;i++)
    {
        int op,p,x,y;
        scanf("%d",&op);
        if(op==1) {
            scanf("%d%d%d",&p,&x,&y);
            ll tmp=query(1,1,n,p);
            if (tmp<y-x+1)
                continue;
            update(1,1,n,p,p,-tmp/(y-x+1)*(y-x+1));
            update(1,1,n,x,y,tmp/(y-x+1));
        }
        else {
            scanf("%d",&p);
            printf("%lld\n",query(1,1,n,p));
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41421433/article/details/84310884