A simple integer problems

Topic links: https://www.acwing.com/problem/content/248/

 

Thinking: a new array B, initially all 0 for each "C l r d", may b [l] + = d, b [r + 1] - = d;

          Such prefix array and b is the corresponding increase in the value of the array a, b as long as the maintenance of the array can be an array in a tree

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
int a[200005],b[200005];
int n;
void add(int x,int y)
{
    for(;x<=n;x+=x&-x)
        b[x]+=y;
    return;
}
int ask(int x)
{
    int ans=0;
    for(;x;x-=x&-x)
        ans+=b[x];
    return ans;
}
int main()
{
    int m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    while(m--)
    {
        char c;
        getchar();
        scanf("%c",&c);
        if(c=='Q')
        {
            int w;
            scanf("%d",&w);
            printf("%d\n",ask(w)+a[w]);
        }
        else
        {
            int l,r,w;
            scanf("%d %d %d",&l,&r,&w);
            add(l,w);
            add(r+1,-w);
        }
    }
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/zcb123456789/p/11370509.html