Cod sursa(job #2738874)

Utilizator AlexandruGabrielAliciuc Alexandru AlexandruGabriel Data 6 aprilie 2021 14:41:36
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("datorii.in");
ofstream fout ("datorii.out");

const int N = 15005;
int n, m;
int aib[N];

inline int lowestBit(int x)
{
    return (x & -x);
}

void Update(int pos, int sum)
{
    for (int i = pos; i <= n; i += lowestBit(i))
        aib[i] += sum;
}

int Query(int pos)
{
    int ans = 0;
    for (int i = pos; i; i -= lowestBit(i))
        ans += aib[i];
    return ans;
}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        int val;
        fin >> val;

        Update(i, val);
    }

    while (m--)
    {
        int op, a, b;
        fin >> op >> a >> b;

        if (op == 0)
            Update(a, -b);
        
        else
            fout << 1LL * (Query(b) - Query(a - 1)) << "\n";
    }
    return 0;
}