P3374 Fenwick tree 1 (single-point modification summed)

P3374 [template] Fenwick tree 1

Topic links: https://www.luogu.org/problem/P3374

topic:

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

A number x plus 1. The

2. obtaining a number of sections and each

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 three integers, it represents one operation, as follows:

Operation 1: Format: 1 XK Meaning: the number of x plus k

Operation 2: Format: 2 XY Meaning: the number of outputs of each interval [x, y] and the

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 1 3
2 2 5
1 3 -1
1 4 2
2 1 4
Output # 1
14
16

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:

 

 

14 and 16 so that the output

//
// Created by hanjinyu on 19-9-2.
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
int n,m;
int ans;
int input[maxn];
struct Node{
 int left;
 int right;
 int num;
}tree[maxn*4];

void PushUp(int index)
{
    tree[index].num = tree[index<<1].num+tree[index<<1|1].num;
}

void build(int l,int r,int index)
{
    tree[index].left=l;
    tree[index].right=r;
    if(l==r)
    {
        tree[index].num = input[l];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,index<<1);
    build(mid+1,r,index<<1|1);
    PushUp(index);
}

void change(int index,int dis,int k)
{
    if(tree[index].left==tree[index].right)
    {
        tree[index].num += k;
        return;
    }
    if(dis<=tree[index<<1].right)
        change(index<<1,dis,k);
    else
        change(index<<1|1, dis, k);
    PushUp(index);
}
void search(int index,int l,int r)
{
    if(l<=tree[index].left&&r>=tree[index].right)
    {
        ans+=tree[index].num;
        return;
    }
    if(tree[index<<1].right>=l)
        search(index<<1,l,r);
    if(tree[index<<1|1].left<=r)
        search(index<<1|1,l,r);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&input[i]);
    }
    build(1,n,1);
    int x,y,z;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        if(x==1)
            change(1,y,z);
        else if(x==2)
        {
            ans=0;
            search(1,y,z);
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Vampire6/p/11456583.html