Cod sursa(job #1251816)

Utilizator catalincraciunCraciun Catalin catalincraciun Data 29 octombrie 2014 21:59:07
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
/// Craciun Catalin
///  Datorii
#include <iostream>
#include <fstream>
#include <cmath>

const int NMax = 20000;

using namespace std;

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

int n, m;
int A[NMax];

void insertInBinaryTree(int position, int value) {

    while (position <= n) {
        A[position] += value;
        int zeros = 0; while (!(position & (1<<zeros))) zeros++;
        position += 1<<zeros;
    }
}

int queryBinaryTree(int right) {

    int sum = 0;
    while (right > 0) {
        sum += A[right];
        int zeros = 0; while (!(right & (1<<zeros))) zeros++;
        right -= 1<<zeros;
    }

    return sum;
}

int main() {

    f>>n>>m;
    for (int i=1;i<=n;i++) {
        int x; f>>x;
        insertInBinaryTree(i, x);
    }
    for (int i=1;i<=m;i++) {
        int type;
        f>>type;
        if (type == 0) {
            int t,v;
            f>>t>>v;
            insertInBinaryTree(t,-v);
        } else if (type == 1) {
            int left, right;
            f>>left>>right;
            g<<queryBinaryTree(right) - queryBinaryTree(left-1)<<'\n';
        }
    }

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

    return 0;
}