Cod sursa(job #2107907)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 17 ianuarie 2018 19:45:37
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <bits/stdc++.h>
using namespace std;

class Reader {
public:
    Reader(const string& filename):
            m_stream(filename),
            m_pos(kBufferSize - 1),
            m_buffer(new char[kBufferSize]) {
        next();
    }

    Reader& operator>>(int& value) {
        value = 0;
        while (current() < '0' || current() > '9')
            next();
        while (current() >= '0' && current() <= '9') {
            value = value * 10 + current() - '0';
            next();
        }
        return *this;
    }

private:
    const int kBufferSize = 32768;

    char current() {
        return m_buffer[m_pos];
    }

    void next() {
        if (!(++m_pos != kBufferSize)) {
            m_stream.read(m_buffer.get(), kBufferSize);
            m_pos = 0;
        }
    }

    ifstream m_stream;
    int m_pos;
    unique_ptr<char[]> m_buffer;
} fin("datorii.in");

const int dim = 100005;
int bit[dim], N;

int lsb(int x) {
    return x&-x;
}

void update(int pos, int val) {
    for(; pos <= N; pos += lsb(pos))
        bit[pos]+=val;
}
long long query(int pos) {
    long long res = 0;
    for(; pos; pos -= lsb(pos))
        res += bit[pos];
    return res;
}

void solve() {
    int M;
    fin >> N >> M;

    for (int i = 1; i <= N; ++i) {
        int x;
        fin >> x;
        update(i, x);
    }

    for (; M; --M) {
        int op;
        fin >> op;

        if (op == 0) {
            int pos, val;
            fin >> pos >> val;
            update(pos, -val);
        }
        else {
            int left, right;
            fin >> left >> right;
            cout << query(right) - query(left - 1) << '\n';
        }
    }
}

int main() {
    assert(freopen("datorii.out", "w", stdout));

    solve();

    return 0;
}