Segment tree (update interval, the interval sum)

1082 segment tree Exercise 3

Title Description Description

Give you the number N, there are two modes of operation:


1: number of sections to all [a, b] is increased X


2: Number interrogation interval [a, b] and.

Enter a description Input Description

The first line of a positive integer n, the next line n integers n,

 

Then again a positive integer Q, each row represents the number of operations,

 

If the first number is one, followed by three positive integers,

 

Indicates the interval [a, b] each increase in the number X, if it is 2,

 

2 represents the operation interrogation interval [a, b], and how much.

 

pascal players do not use readln read

Output Description Output Description

For each answer to a query output line

Sample input Sample Input

3

1

2

3

2

1 2 3 2

2 2 3

Sample Output Sample Output

9

Data range and prompt Data Size & Hint

data range

1<=n<=200000

1<=q<=200000

wa the first serve, because the demand interval and burst int

#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#define rep(i , n) for(int i = 0 ; i < (n) ; i++)
using namespace std;
const int N = 1100009 ;
long long ans = 0 , flag = 1;
int a[1000009] , b[1000009];

struct Node{
    long long  l , r , val , lazy_tag ;
}tree[N * 4];

void build(int l , int r , int root)
{
    tree[root].l = l , tree[root].r = r ;
    tree[root].lazy_tag = 0 ;
    if(l == r)
    {
        scanf("%d" , &tree[root].val);
        return ;
    }
    int mid = (l + r) >> 1;
    Build (L, MID, the root * 2 ); 
    Build (MID + . 1 , R & lt, the root * 2 + . 1 ); 
    Tree [the root] .val = Tree [the root * 2 ] + Tree .val [the root * 2 + . 1 ] .val; 
} 

void pushdown ( int the root) 
{ 
    Tree [the root * 2 ] + = .lazy_tag Tree [the root] .lazy_tag; // note marks accumulated 
    Tree [the root * 2 + . 1 ] + = .lazy_tag Tree [the root]. lazy_tag;
     // update the value of the child node 
    Tree [the root * 2 ] + = .val (Tree [the root * 2] .r - Tree [the root * 2 ] + .L . 1 ) Tree * [the root] .lazy_tag; // the lazy interval length of the parent node's child multiplying 
    Tree [the root * 2 + . 1 ] .val + = ( Tree [the root * 2 + . 1 ] .r - Tree [the root * 2 + . 1 ] .L + . 1 ) * Tree [the root] .lazy_tag; 
    Tree [the root] .lazy_tag = 0 ; // unmark the parent node 
}
 void Update ( int L, int R & lt, int addval, int the root) 
{ 
    IF(Tree [the root] .L> = L && Tree [the root] .r <= R & lt) 
    { 
        Tree [the root] .lazy_tag + = addval; // Note that this flag is additive, there may be accumulated before the 
        tree [root] .val = + (Tree [the root] .r - Tree [the root] .L + . 1 ) * addval; // this is multiplied by addval, because each mark would have accumulated to 
        return ; 
    } 
    IF (Tree [the root] .lazy_tag) 
        pushdown (the root); 
    int MID = (Tree [the root] .L + Tree [the root] .r) >> . 1 ;
     IF (L <= MID) 
        Update (L, R & lt, addval, the root * 2 );
     IF (R & lt > MID) 
        Update (L, R & lt, addval, the root * 2 + . 1);

    tree[root].val = tree[root*2].val + tree[root*2+1].val;
}

void  query(int l,int r  ,int root )
{
    if(tree[root].l >= l && tree[root].r <= r)
    {
        ans += tree[root].val ;
        return ;
    }
    if(tree[root].lazy_tag)
        pushdown(root);
    int mid = (tree[root].l + tree[root].r) >> 1;
    if(l <= mid)
        query(l , r , root*2);
    if(r > mid)
        query(l , r , root*2+1);

    tree[root].val = tree[root*2].val + tree[root*2+1].val;
}



int main()
{
    int n ;
    while(~scanf("%d" , &n))
    {
        build(1 , n , 1);
        int q ;
        scanf("%d" , &q);
        for(int i = 0 ; i < q ; i++)
        {
            int f  ;
            scanf("%d" , &f );
            if(f == 1)
            {
                int x , y , addval ;
                scanf("%d%d%d" , &x ,&y , &addval);
                update(x , y , addval , 1);
            }
            else
            {
                int x , y ;
                scanf("%d%d" , &x , &y);
                query(x , y , 1);
                cout << ans << endl ;
                ans = 0 ;
            }
        }
    }

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/11260015.html