Cod sursa(job #1899628)

Utilizator medicinedoctoralexandru medicinedoctor Data 2 martie 2017 20:44:54
Problema Arbori indexati binar Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <algorithm>
#include <vector>
//#include <iostream>

using namespace std;

int s = 0, j;

ifstream fin ("aib.in" );
ofstream cout("aib.out");

/*void f(int x)
{
    s += x;
}*/

int main()
{
    int n, m, q, x, y;
    vector <int> a;
    fin >> n >> m;
    a.resize(n);

    for (int i = 0; i < a.size(); i++)
        fin >> a[i];

    for (int i = 0; i < m; i++)
    {
        fin >> q >> x;
        if (q != 2) fin >> y;
        if (q == 0) a[--x] += y;
        if (q == 1)
        {
            //y--;
            //x--;
            s = 0;
            for (int i = x - 1; i < y; i++) s += a[i];
            //for_each(a.begin() + x, a.begin() + y + 1, f);
            cout << s << '\n';
        }
        if (q == 2)
        {
            s = 0;
            for (j = 0; s != x; j++)
            {
                s += a[j];
                if (s > x)
                {
                    j = -1;
                    break;
                }
            }
            cout << j << '\n';
        }
    }
}