Cod sursa(job #1801984)

Utilizator jason2013Andronache Riccardo jason2013 Data 9 noiembrie 2016 19:17:54
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include<bits/stdc++.h>
using namespace std;

ifstream f("datorii.in");
ofstream g("datorii.out");
const int NMax = 280000;
int n, m, mij, pos, o, v[NMax], sum, a, b;

void update(int low, int high, int nod)
{
    v[nod] += b;
    if(low == high) return;
    int mij = ( low + high ) / 2;
    if(a <= mij)
        update(low, mij, nod*2);
    else
        update(mij+1, high, nod*2+1);

}

void query(int low, int high, int nod)
{
    if(a <= low && b >= high)
    {
        sum += v[nod];
        return;
    }
    int mij = ( low + high ) / 2;
    if(a <= mij)
        query(low, mij, nod*2);
    if(b > mij)
    {
        query(mij+1, high, nod*2+1);
    }
}

int main()
{
    f>>n>>m;
    for(a = 1; a <= n; a++)
    {
        f>>b;
        update(1, n, 1);
    }

    while(m--)
    {
        f>>o>>a>>b;
        if(o == 0)
        {
            b =- b;
            update(1, n, 1);
        }
        else{
            sum = 0;
            query(1, n, 1);
            g<<sum<<"\n";
        }
    }

    return 0;
}