Cod sursa(job #3167203)

Utilizator poparobertpoparobert poparobert Data 10 noiembrie 2023 12:45:04
Problema Farfurii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
typedef int ll;
typedef pair<ll,ll> pll;
class AINT
{
    ll n;
    vector<ll> v;
public:
    AINT(ll x=0){n=x,v.resize(2*n);}
    void build(vector<ll>&a)
    {
        ll i;
        for(i=0;i<n;i++)v[n+i]=a[i];
        for(i=n-1;i>0;i--)v[i]=v[2*i]+v[2*i+1];
    }
    void add(ll p,ll x)
    {
        for(p+=n;p>0;p>>=1)v[p]+=x;
    }
    ll ask(ll l,ll r)
    {
        ll rez=0;
        for(l+=n,r+=n;l<r;l>>=1,r>>=1)
        {
            if(l&1)rez+=v[l++];
            if(r&1)rez+=v[--r];
        }
        return rez;
    }
};
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);
    ll n,m,c,x,y;
    cin>>n>>m;
    vector<ll>a(n);
    for(ll i=0;i<n;i++)cin>>a[i];
    AINT alex(n);
    alex.build(a);
    while(m--)
    {
        cin>>c>>x>>y;
        if(c==0)
        {
            alex.add(x-1,-y);
        }
        else
        {
            cout<<alex.ask(x-1,y)<<'\n';
        }
    }
	return 0;
}