Cod sursa(job #1551695)

Utilizator Vele_GeorgeVele George Vele_George Data 16 decembrie 2015 12:46:52
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <iostream>
#include <fstream>
#define nmax 15005
using namespace std;

int aib[nmax], n, m, x, y, act;

inline int bit(int x)
{
    return (x &(-x));
}
void update(int poz, int val)
{
    for(int i=poz; i<=n; i+=bit(i))
    {
        aib[i]+=val;
    }
    return;
}

int find(int poz)
{
    int sum = 0;
    for(int i = poz; i>=1; i-=bit(i))
    {
        sum+=aib[i];
    }
    return sum;
}
int querry(int x, int y)
{
    x = find(x-1);
    y = find(y);
    return (y-x);
}

int main()
{
    ifstream f("datorii.in");
    ofstream g("datorii.out");

    f >> n >> m;
    for(int i=1; i<=n; i++)
    {
        f >> y;
        update(i, y);
    }

    for(int i=1; i<=m; i++)
    {
        f >> act >> x >> y;
        if (act == 0)
        {
            update(x, -y);
        }else{
            g << querry(x, y)<<"\n";
        }
    }

    f.close();
    g.close();
    return 0;
}