[Template] segment tree - a single point of modification, the query interval

Easy to understand but hard to play (long and difficult adjustment) ------ represent the personal views

(Can not fight do not fight)

What segment tree?

Probably so long? (Representing the interval 1-6)

Segment tree is a binary tree , by half thought a representation established interval relation tree structure. (In short remember it very easy to use on the right)

How to build a segment tree

Probably thinking:

Half + recursively

Nothing to talk about, specifically to see the code. .

// achievements 
struct Node 
{ 
  int A, B; 
} Tree [ 100001 ]; 

void make_tree ( int P, int X, int Y) // P of the current node number, x, y for the left and right end sections 
{ 
    Tree [P] II.A = X; 
    tree [P] .B = Y;
     IF (X < Y) 
    { 
        int MID = (X + Y) / 2 ; 
        make_tree (P * 2 , X, MID); // left subtree 
        make_tree ( * P 2 + . 1 , MID + . 1 , Y);// right subtree 
    } 
}

Representing the interval [1, n] is the number of nodes in the tree line?

(Do not look the way it looks useless, or very important)

4 * n still open to it, insurance.

Segment tree how to use?

A single point of modification

Probably thinking:

Half + recursively find something, modification

Code:

// single point modified 
void ADDS ( int P, int x, int V) 
{ 
    Tree [P] .v + = V; // add V in the weighted value of each interval containing x 
    IF (Tree [P] == II.A Tree [P] .B)
     return ;
     if (x <= Tree [P * 2 ] .B) 
    ADDS (P * 2 , x, V); // if x left son's current interval 
    if (X> = Tree [P * 2 + . 1 ] II.A) 
    ADDS (P * 2 + . 1 , X, V); // if in the right son 
}

 

(Not read the words below diagram) (this figure is not my painting)

Interval inquiry

Probably thinking:

+ Recursive half, if it is added to the current weight value of the interval [x, y], if not is not added.

Code:

// Interval Query 
int ANS;
 void Find ( int X, int Y, int P) 
{ 
    IF (Tree [P] II.A> X = && Tree [P] .B <= Y) // if the current right in the interval [x, y] inside 
    { 
        ANS + = Tree [P] .v;
         return ; 
    } 
    IF (X <= Tree [P * 2 ] .B) 
    Find (X, Y, P * 2 );
     IF (Y> = Tree [P * 2 + . 1 ] II.A) 
    Find (X, Y, P * 2 + . 1 ); 
    
}

 

(FIG narrated above)

First look at questions: tree line interval + modify a single point of inquiry

Meditation ing. .

AC Code:

#include <the iostream> 
#include <cstdio>
 the using  namespace STD;
 // contribution 
struct Node 
{ 
  int A, B, V; 
} Tree [ 2.00001 million ];
 int A [ 500 010 ];
 void make_tree ( int P, int X, int Y ) // P of the current node number, x, y for the left and right end sections, v is weight 
{ 
    Tree [P] II.A = X; 
    Tree [P] .B = Y;
     IF (X < Y) 
    { 
        int MID = (X + Y) / 2 ; 
        make_tree (P* 2 , X, MID); // left subtree 
        make_tree (P * 2 + . 1 , MID + . 1 , Y); // right subtree 
    } 
} 
int INPUT ( int P) // gift 
{
     IF (Tree [P ] == II.A Tree [P] .B) 
    { 
        Tree [P] .v = A [Tree [P] II.A];
         return Tree [P] .v; 
    } 
    Tree [P] .v = INPUT (* P 2 ) INPUT + (P * 2 + . 1 );
     return Tree [P] .v; 
} 
// single point modified
 void ADDS ( int P, int x, int V) 
{ 
    Tree [P] .v + = V; // add V in the weighted value of each interval containing x 
    IF (Tree [P] == II.A Tree [P] .B)
     return ;
     if (x <= Tree [P * 2 ] .B) 
    ADDS (P * 2 , x, V); // if x left son's current interval 
    if (X> = Tree [P * 2 + . 1 ] II.A) 
    ADDS (P * 2 + . 1 , X, V); // if in the right son 
}
 // interval query 
intANS;
 voidFind ( int X, int Y, int P) 
{ 
    IF (Tree [P] II.A> X = && Tree [P] .B <= Y) // if the current right in the interval [x, y] which 
    { 
        ANS + = Tree [P] .v;
         return ; 
    } 
    IF (X <= Tree [P * 2 ] .B) 
    Find (X, Y, P * 2 );
     IF (Y> = Tree [P * 2 + . 1 ] II.A) 
    Find (X, Y, P * 2 + . 1 ); 
    
} 
int main () 
{ 
    int n-, m; 
    Scanf ( "%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    make_tree(1,1,n);
    input(1);
    int A,B,C;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&A);
        if(A==1)
        {
            scanf("%d%d",&B,&C);
            adds(1,B,C); 
        }
        else
        {
            ans=0;
            scanf("%d%d",&B,&C);
            find(B,C,1);
            printf("%d\n",ans);
        }
    }
    return 0;
} 

 

 

Guess you like

Origin www.cnblogs.com/Daz-Os0619/p/11469694.html