Cod sursa(job #1547843)

Utilizator emanuel_rRamneantu Emanuel emanuel_r Data 9 decembrie 2015 23:06:15
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include<iostream>
#include<fstream>

using namespace std;

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

int AIB[15005];
int n, m;

void Update(int val, int pos)
{
    while(pos <= n){
        AIB[pos] -= val;
        pos += pos&(-pos);
    }
}

void citire()
{
    int i, x;
    f>>n>>m;
    for(i=1; i<=n; i++){
        f>>x;
        Update(-x, i);
    }
}

int Query(int pos)
{
    int sum = 0;
    while(pos){
        sum += AIB[pos];
        pos -= pos&(-pos);
    }
    return sum;
}

int main()
{
    int op, tag, val, x, y;
    citire();
    while(m--){
        f>>op;
        if(op==0){
            f>>tag>>val;
            Update(val, tag);
        }
        if(op==1){
            f>>x>>y;
            g<<Query(y) - Query(x-1)<<"\n";
        }
    }

    return 0;
}