cogs 1317. the number of columns C operating range modify the query interval

1317. Sequence Procedure C

★★★ Input file: shuliec.in   Output file: shuliec.out   a simple comparison of
the time limit: 1 s memory limit: 128 MB

Description [title]

Suppose there is a length of  n- ( n- 100000 )  the number of columns  A , supports the following two operations:

1.  A I , A I + . 1 , ... , A J  values were increased  d

2. Query  A S + A S + . 1 + + A T ( S T )  values.

The operational requirements for proper operation, and outputs the result.

[Input Format]

Conduct a first positive integer  n- , it represents the size of the number of columns.

The second row has  n  integers, the number of columns  A  initial value items.

A third row integer  m  , represents the number of operations.

The following are  m  rows, each row describes an operation:

ADD ijd (the  A I , A I + . 1 , ... , A J ( . 1 I , J n- )  values were increased an integer  D )

SUM st (represented query  A S + + A T  value)

[Output format]

For each inquiry, the results of the query output.

[Sample input]

4
1 4 2 3
3
SUM 1 3
ADD 2 2 50
SUM 2 3

[Sample output]

7
56

【prompt】

All answers less than  4611686018427387904

Strengthen the  10  group limit data, not all of the re-test by rvalue 2018.2.26

 

 

Segment tree or just

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100005;
#define ll long long
ll a[maxn];
int n,m;
struct SegmentTree{
    int l,r;
    long long dat;
    long long lazy_tag;
}t[maxn<<2];
void Pushdown(int p,int l,int r,int mid){
    t[p*2].dat+=t[p].lazy_tag*(mid-l+1);
    t[p*2+1].dat+=t[p].lazy_tag*(r-mid);
    t[p*2].lazy_tag+=t[p].lazy_tag;
    t[p*2+1].lazy_tag+=t[p].lazy_tag;
    t[p].lazy_tag=0;
}
void build(int p,int l,int r){
    t[p].l=l;t[p].r=r;
    if(l==r){
        t[p].dat=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    t[p].dat=t[p*2].dat+t[p*2+1].dat;
}
ll Get(int p,int l,int r,int s,int tt){
    if(s>r||tt<l) return 0;
    if(s<=l&&r<=tt) return t[p].dat;
    int mid=(l+r)>>1;
    Pushdown(p,l,r,mid);
    return  Get(p*2,l,mid,s,tt)+ Get(p*2+1,mid+1,r,s,tt);
}
void Add(int p,int l,int r,int s,int tt,ll x){
    if(s>r||tt<l) return;
    if(s<=l&&r<=tt){
        t[p].dat+=x*(r-l+1);
        t[p].lazy_tag+=x;
        return;
    }
    int mid=(l+r)>>1;
    Pushdown(p,l,r,mid);
    Add(p*2,l,mid,s,tt,x);Add(p*2+1,mid+1,r,s,tt,x);
    t[p].dat=t[p*2].dat+t[p*2+1].dat;
}
int main()
{
    freopen("shuliec.in","r",stdin);freopen("shuliec.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    scanf("%d",&m);
    build(1,1,n);
    for(int i=1;i<=m;i++){
        string s;cin>>s;
        if(s[0]=='S'){
            int l,r;scanf("%d%d",&l,&r);
            printf("%lld\n",Get(1,1,n,l,r));    
        }
        else{
            int s,t;
            ll d;
            scanf("%d%d%lld",&s,&t,&d);
            Add(1,1,n,s,t,d);
        }
    }
    
    
    return 0;
 } 

 There is still one

Permanent marker segment tree

 

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11307395.html