Cod sursa(job #3364352)

Utilizator Andreea1501013Andreea Andreea1501013 Data 2 septembrie 2026 01:47:42
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.23 kb
/// problema datorii infoarena

#include <bits/stdc++.h>

using namespace std;

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

int N, M, v[15005], AIB[15005];

void citire()
{
    fin>>N>>M;
    for(int i = 1; i <= N; i++)
    {
        fin>>v[i];
    }
}

void construireAIB()
{
    for(int i = 1; i <= N; i++)
    {
        AIB[i] += v[i];
        int tata = i + (i & (-i));

        if(tata <= N)
        {
           AIB[tata] += AIB[i];
        }
    }
}

void update(int T, int V)
{
    for(int x = T; x <= N; x += (x & (-x)))
    {
        AIB[x] += V;
    }
}

int getSum(int poz)
{
    /// calculeaza suma din v de la 1 la poz
    int sum = 0;
    for(int x = poz; x > 0; x -= (x & (-x)))
    {
        sum += AIB[x];
    }

    return sum;
}

void operatii()
{
    bool tip;
    int x,y;

    while(M--)
    {
        fin>>tip>>x>>y;
        if(tip == 0)
        {
            /// operatie de tip v[x] -= y
            update(x, -y);
        }
        else
        {
            fout<< getSum(y) - getSum(x - 1)<<'\n';
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);

    citire();
    construireAIB();
    operatii();

    return 0;
}