A segment tree template

Title Description

If that is known to a number of columns, you need to perform the following two operations:

1. The section of each of a number of x plus

2. obtaining a number of sections and each

Input Format

The first line contains two integers N, M, respectively, represents the number of the total number of columns and number of operations.

The second line contains N integers separated by spaces, wherein the number indicates the i-th column of the item i of the initial value.

Next M lines contains an integer of 3 or 4, represents an operation, as follows:

Operation 1: Format: 1 XYK Meaning: the interval [x, y] k each number plus

Operation 2: Format: 2 XY Meaning: the number of outputs of each interval [x, y] and the

Output Format

Output contains an integer number of lines, that is, the operation results of all 2.

Sample input and output

Input # 1
5 5
1 5 4 2 3
2 2 4
1 2 3 2
2 3 4
1 1 5 1
2 1 4
Output # 1
11
8
20

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
struct node{
    int l,r;
    ll sum,lazy;
}a[maxn<<2];
ll s[maxn];
inline void update(int k)
{
    a[k].sum=(a[k<<1].sum+a[k<<1|1].sum);
}
inline void build(int k,int l,int r)
{
    a[k].l=l,a[k].r=r;
    if(l==r)
    {
        a[k].sum=s[l];
        return;
    }
    int mid=(l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
    update(k);
}
//void change(int k,int x,int y)
//{
//    if(a[k].l==a[k].r)
//    {
//        a[k].sum=y;return;
//    }
//    int mid=(a[k].l+a[k].r)>>1;
//    if(x<=mid)change(k<<1,x,y);
//    else change(k<<1|1,x,y);
//    update(k);
//}
inline void pushdown(int k)
{
    if(a[k].l==a[k].r)
    {
        a[k].lazy=0;return;
    }
    a[k<<1].sum+=(a[k<<1].r-a[k<<1].l+1)*a[k].lazy;
    a[k<<1|1].sum+=(a[k<<1|1].r-a[k<<1|1].l+1)*a[k].lazy;
    a[k<<1].lazy+=a[k].lazy;
    a[k<<1|1].lazy+=a[k].lazy;
    a[k].lazy=0;
}
inline void changeplus(int k,int l,int r,int x)
{
    if(a[k].l>=l&&a[k].r<=r)
    {
        a[k].sum+=(a[k].r-a[k].l+1)*x;
        a[k].lazy+=x;
        return;
    }
    if(a[k].lazy)pushdown(k);
    int mid=(a[k].l+a[k].r)>>1;
    if(l<=mid)changeplus(k<<1,l,r,x);
    if(r>mid)changeplus(k<<1|1,l,r,x);
//    else changeplus(k<<1,l,mid,x),changeplus(k<<1|1,mid+1,r,x);
    update(k);
}
//void changemul(int k,int l,int r,int x)
//{
//    
//}

inline ll query(int k,int l,int r)
{
    if(a[k].lazy)pushdown(k);
    if(a[k].l>=l&&r>=a[k].r)
    {
        return a[k].sum;
    }
    ll ans=0;
    int mid=(a[k].l+a[k].r)>>1;
    if(l<=mid)ans+=query(k<<1,l,r);
    if(r>mid)ans+=query(k<<1|1,l,r);
//    else ans+=query(k<<1,l,mid)+query(k<<1|1,mid+1,r);
    return ans;
}
int main()
{
    int n,m,i,L,R,c,opt;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    scanf("%lld",&s[i]);
    build(1,1,n);
    while(m--)
    {
        scanf("%d",&opt);
        if(opt==1)
        {
            scanf("%d%d%d",&L,&R,&c);
            changeplus(1,L,R,c);
        }
        else
        {
            scanf("%d%d",&L,&R);
            printf("%lld\n",query(1,L,R));
        }
    }
    
}

 

Guess you like

Origin www.cnblogs.com/HHHEN/p/11724426.html