Cod sursa(job #3222599)

Utilizator UpgradeStrikeMurgilas Mihai-Rares UpgradeStrike Data 10 aprilie 2024 22:51:06
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <fstream>

using namespace std;

const int N = 15000;
const int L = 17;

int v[15001], aint[1<<(L+1)];

void constructie(int p, int st, int dr)
{
    if (st == dr)
    {
        aint[p] = v[st];
        return;
    }
    int m = (st + dr) / 2, fs = 2 * p, fd = 2 * p + 1;
    constructie(fs, st, m);
    constructie(fd, m + 1, dr);
    aint[p] = aint[fs]+aint[fd];
}

int interogare(int p, int st, int dr, int a, int b)
{
    if (a <= st && dr <= b)
    {
        return aint[p];
    }
    int m = (st + dr) / 2, fs = 2 * p, fd = 2 * p + 1;
    int rez_st = 0, rez_dr = 0;
    if (a <= m)
    {
        rez_st = interogare(fs, st, m, a, b);
    }
    if (m + 1 <= b)
    {
        rez_dr = interogare(fd, m + 1, dr, a, b);
    }
    return rez_st+rez_dr;
}

void actualizare(int p, int st, int dr, int poz, int val)
{
    if (st == dr)
    {
        aint[p] -= val;
        return;
    }
    int m = (st + dr) / 2, fs = 2 * p, fd = 2 * p + 1;
    if (poz <= m)
    {
        actualizare(fs, st, m, poz, val);
    }
    else
    {
        actualizare(fd, m + 1, dr, poz, val);
    }
    aint[p] = aint[fs]+aint[fd];
}

int main()
{
    ifstream in("datorii.in");
    ofstream out("datorii.out");
    int n, nr_op;
    in >> n >> nr_op;
    for (int i = 1; i <= n; i++)
    {
        in >> v[i];
    }
    constructie(1, 1, n);
    for (int i = 0; i < nr_op; i++)
    {
        int tip;
        in >> tip;
        if (tip == 1)
        {
            int a, b;
            in >> a >> b;
            out << interogare(1, 1, n, a, b) << "\n";
        }
        else
        {
            int poz, val;
            in >> poz >> val;
            actualizare(1, 1, n, poz, val);
        }
    }
    in.close();
    out.close();
    return 0;
}