[3372] Luo Gu segment tree 1

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

Description / Tips

Constraints of time: 1000ms, 128M

Data Scale:

For 30% of the data: N <= 8, M <= 10

For 70% of the data: N <= 1000, M <= 10000

To 100% of the data: N <= 100000, M <= 100000

(Data ^ _ ^ has been strengthened to ensure that within int64 / long long range data)

Sample Description:

 

       I am looking for a long time with my classmates next to the king. . . . Mad, trained, trained, trained,

// luogu-judger-enable-o2
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int N=100003;
typedef long long ll;
ll z,k,sum[N*4],add[N*4],a[N];
int n,akioi,x,y,biu;

void pushup(int rt){
    sum[rt]=sum[rt*2]+sum[rt*2+1];
}

void build(int l,int r,int rt){
    if(l==r){
        sum[rt]=a[l];
        return;
    }
    int m=(l+r)/2;
    build(l,m,rt*2);
    build(m+1,r,rt*2+1);
    pushup(rt);
}

void pushdown(int rt,int ln,int rn){
    if(add[rt]){
        add[rt*2]+=add[rt];
        add[rt*2+1]+=add[rt];
        sum[rt*2]+=add[rt]*ln;
        sum[rt*2+1]+=add[rt]*rn;
        add[rt]=0;
    }
}

ll query(int L,int R,int l,int r,int rt){
    if(L<=l && R>=r)
       return sum[rt];
    int m=(l+r)/2;
    pushdown(rt,m-l+1,r-m);
    ll ans=0;
    if(L<=m) ans+=query(L,R,l,m,rt*2);
    if(R> m) ans+=query(L,R,m+1,r,rt*2+1);
    return ans;
}

void update(int L,int R,ll k,int l,int r,int rt){
    if(L<=l && R>=r){
       sum[rt]+=k*(r-l+1);
       add[rt]+=k;
       return ;    
    } 
    int m=(l+r)/2;
    pushdown(rt,m-l+1,r-m);
    if(L<=m) update(L,R,k,l,m,rt*2);
    if(R>m)  update(L,R,k,m+1,r,rt*2+1);
    pushup(rt);
}

int main(){
    scanf("%d %d",&n,&akioi);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    build(1,n,1);
    while(akioi--){
        //cout<<7666;
        scanf("%d",&biu);
        if(biu==2){
            scanf("%d %d",&x,&y);
            printf("%lld\n",query(x,y,1,n,1));
        }
        else{
            scanf("%d %d %lld",&x,&y,&z);
            update(x,y,z,1,n,1);
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11324509.html