Los template Valley P3368 [2] Fenwick tree (tree line)

Title Description

If that is known to a number of columns, you need to perform the following two operations:

1. Each section plus a few number x

Obtaining a value of 2. The number of

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 2 or 4, it indicates an operation as follows:

Operation 1: Format: 1 XYK Meaning: the interval [x, y] k each number plus

Operation 2: Format: 2 x Meaning: the number of x-value output

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
1 2 4 2
2 3
1 1 5 -1
1 3 5 7
2 4
Output # 1
6
10

Description / Tips

Constraints of time: 1000ms, 128M

Data Scale:

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

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

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

Sample Description:

Therefore, output is 6,10

#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;

#define ll long long
#define eps 1e-9

const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;

int n, m, ans, input[500000+8];

struct Node 
{ 
    int left, right, NUM; 
} Tree [ 5000000 + . 8 ]; 

void Build ( int left, int right, int index) /// contribution 
{ 
    Tree [index] .num = 0 ; 
    Tree [index] .left = left; 
    Tree [index] .right = right;
     IF (left == right) /// If the node is a leaf, the value returned his 
        return ;
     int MID = (left + right) / 2 ; 
    Build (left, MID , index *2 ); /// from the left start assigned section 
    Build (MID + . 1 , right, index * 2 + . 1 ); /// from the right section start assignment 
} 

void PLS ( int index, int L, int R & lt, int K) / // interval k plus value [L, R & lt] 
{
     IF (Tree [index] .left> Tree && L = [index] .right <= R & lt) /// If the object range within the interval 
    { 
        Tree [index ] .num + = k;
         return ; 
    } 
    IF (Tree [index * 2 ] .right> = L) /// so that the left plus interval k
        PLS (index * 2 , L, R & lt, K);
     IF (Tree [index * 2 + . 1 ] .left <= R & lt) /// so that the right section plus K 
        PLS (index * 2 + . 1 , L, R & lt, K); 
} 

void Search ( int index, int DIS) /// find that point, and the value of his recorded 
{ 
    ANS + Tree = [index] .num; /// the increase / decrease value after the recording operation 
    IF ( Tree [index] == .left Tree [index] .right)
         return ;
     IF (DIS <= Tree [index * 2 ] .right) /// If this number is less than the index point right son, then to the left to find
        Search (index * 2 , DIS);
     IF (DIS> = Tree [index * 2 + . 1 ] .left) /// If this number is greater than the index of the left son point, then to the right look for 
        Search (index * 2 + . 1 , DIS); 
} 

int main () 
{ 
    Scanf ( " % D% D " , & n-, & m); 
    Build ( . 1 , n-, . 1 ); /// initialization, the value assigned to each of the start. 1 
    for ( int I = . 1 ; I <= n-; I ++ ) 
        Scanf ( " % D " , &input[i]);
    for(int i = 1; i <= m; i++)
    {
        int a;
        scanf("%d", &a);
        if(a == 1)
        {
            int x, y, z;
            scanf("%d%d%d", &x, &y, &z);
            pls(1, x, y, z);
        }
        else if(a == 2)
        {
            ans = 0;
            intx; 
            Scanf ( " % D " , & x); 
            Search ( . 1 , x); /// starting from the root to find this location x 
            the printf ( " % D \ n- " , ANS + INPUT [x]); 
        } 
    } 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/RootVount/p/11278631.html