Fenwick tree 2-- update interval, a single point of query template

Topic Link

Meaning of the questions:

Given 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

answer:

Fenwick tree maintenance difference array, the array function is to find a tree and prefixes

A section for updates and a single point of inquiry

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
int n,m;
int c[maxn],a[maxn];

void updata(int pos,int x)
{
    for(;pos<=n;pos+=pos&-pos)c[pos]+=x;
}
int query(int pos)
{
    int ans=0;
    for(;pos;pos-=pos&-pos)ans+=c[pos];
    return ans;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        updata(i,a[i]-a[i-1]);
    }
    while(m--)
    {
        int op;
        scanf("%d",&op);
        if(op==1)
        {
            int x,y,k;
            scanf("%d%d%d",&x,&y,&to);
            updata(x,k);
            updata(y+1,-k);
        }
        else
        {
            int x;
            scanf("%d",&x);
            int ans=query(x);
            printf("%d\n",ans);
        }
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/j666/p/11734405.html