Cod sursa(job #2071238)

Utilizator vladm98Munteanu Vlad vladm98 Data 20 noiembrie 2017 15:01:45
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.14 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;
};
Reader fin("datorii.in");
ofstream fout("datorii.out");


int aib[15005],n;

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

void add(int pozitie, int cantitate)
{
    int cnt = 0;
    while (pozitie <= n)
    {
        cnt ++;
        aib[pozitie]+=cantitate;
        assert (0 <= aib[pozitie]);
        pozitie += zeros(pozitie);
        assert (cnt <= 20);
    }
}
int compute1(int pozitie2)
{
    int cnt = 0;
    int rez=0;
    while (pozitie2)
    {
        rez+=aib[pozitie2];
        assert (0 <= aib[pozitie2]);
        pozitie2 -= zeros(pozitie2);
        cnt ++;
        assert (cnt <= 20);
    }
    return rez;
}
int compute (int poz1, int poz2)
{
    return compute1(poz2) - compute1(poz1 - 1);
}
int main()
{
    // return 0;
    int m,i,x,y,z,j;
    fin>>n>>m;
    assert (1 <= n && n <= 15000);
    assert (1 <= m && m <= 100000);
    for(i=1;i<=n;i++)
    {
        fin>>x;
        assert (0 <= x && x <= 1000);
        add(i,x);
    }
    for(i=1;i<=m;i++)
    {
        fin>>x>>y>>z;
        assert (x == 0 || x == 1);
        if(x==0)
        {
            assert (1 <= y && y <= n);
            assert (0 <= z && z <= 1000);
            add(y,-z);
        }
        if(x==1)
        {
            fout<<compute(y,z)<<endl;
            assert (1 <= y && y <= z && z <= n);
        }
    }
}