Cod sursa(job #1232456)

Utilizator catalincraciunCraciun Catalin catalincraciun Data 22 septembrie 2014 21:48:09
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
// Craciun Catalin
//  Datorii
//   Infoarena
#include <iostream>
#include <fstream>

#define NMax 15005

using namespace std;

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

int n,m;
int A[NMax];

int query (int lim) {

    int s = 0, c = 0;
    while (lim > 0) {
        s+= A[lim];
        while ( !(lim & (1<<c)) ) c++;
        lim -= (1<<c);
        c++;
    }

    return s;
}

void update (int pos, int val) {

    int c = 0;
    while (pos <= n) {
        A[pos] += val;
        while ( !(pos & (1<<c)) ) c++;
        pos += (1<<c);
        c++;
    }
}

int main() {

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

    for (int i=1;i<=m;i++) {
        int type;
        f>>type;
        if (type == 0) {
            int pos, val;
            f>>pos>>val;
            update(pos, -val);
        } else if (type == 1) {
            int left, right;
            f>>left>>right;
            g<<query(right) - query(left-1)<<'\n';
        }
    }

    f.close(); g.close();

    return 0;
}