[UOJ 53] tree line interval modification

[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 Description:

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] Description:

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

[Sample input]:

5 5
1 5 4 2 3
2 2 4
1 2 3 2
2 3 4
1 1 5 1
2 1 4

[] Sample output:

11
8
20

[Time limit, and the range of data Description:

Time: 1s space: 128M

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

(Guaranteed data within int64 / long long range data)

 

Problem solution: emm segment tree board, but the wrong look for a long time, hey

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const ll maxn=100005; 
ll n,m,x,y,jjj,z;
long long Sum[maxn<<2],Add[maxn<<2];
 ll A[maxn];

void PushUp(ll rt){Sum[rt]=Sum[rt*2]+Sum[rt*2+1];}

void Build(ll l,ll r,ll rt){
    if(l==r) {
        Sum[rt]=A[l];
        return;
    }
    ll m=(l+r)>>1;
    Build(l,m,rt<<1);
    Build(m+1,r,rt<<1|1);
    PushUp(rt);
}
void PushDown(ll rt,ll ln,ll rn){
    if(Add[rt]){
        Add[rt<<1]+=Add[rt];
        Add[rt<<1|1]+=Add[rt];
        Sum[rt<<1]+=Add[rt]*ln;
        Sum[rt<<1|1]+=Add[rt]*rn;
        Add[rt]=0;
    }
}
long long Query(ll L,ll R,ll l,ll r,ll rt){
    if(L <= l && r <= R)
        return Sum[rt];
    ll m=(l+r)>>1;
    PushDown(rt,m-l+1,r-m); 
    long long ANS=0;
    if(L <= m) ANS+=Query(L,R,l,m,rt<<1);
    if(R >  m) ANS+=Query(L,R,m+1,r,rt<<1|1);
    return ANS;
} 

void Update(ll L,ll R,ll C,ll l,ll r,ll rt){
    if(L <= l && r <= R){
        Sum[rt]+=C*(r-l+1);
        Add[rt]+=C;
        return ; 
    }
    ll m=(l+r)>>1;
    PushDown(rt,m-l+1,r-m);
    if(L <= m) Update(L,R,C,l,m,rt<<1);
    if(R >  m) Update(L,R,C,m+1,r,rt<<1|1); 
    PushUp(rt);
} 
int main(){
    freopen("53.in","r",stdin);
    freopen("53.out","w",stdout);
    ll mt;
    scanf("%lld %lld",&n,&mt);
    for(ll i=1;i<=n;i++)
        scanf("%lld",&A[i]);
    Build(1,n,1);
    while(mt--){
        scanf("%lld",&jjj);
        if(jjj==1){
            scanf("%lld %lld %lld",&x,&y,&z);
            Update(x,y,z,1,n,1);
        }
        else{
            scanf("%lld %lld",&x,&y);
            printf("%lld\n",Query(x,y,1,n,1));
        }
    }
    return 0;
}

 

Guess you like

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