Cod sursa(job #2484700)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 31 octombrie 2019 14:24:40
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("datorii.in");
ofstream fout ("datorii.out");

const int N = 15005;

int a[N], arb[4 * N];

void build(int st, int dr, int nod)
{
    if(st == dr) {
        arb[nod] = a[st];
        return ;
    }

    int mid = (st + dr) >> 1;

    build(st, mid, nod * 2);
    build(mid + 1, dr, nod * 2 + 1);

    arb[nod] = arb[nod * 2] + arb[nod * 2 + 1];
}

void update(int st, int dr, int nod, int position, int val)
{
    if(st == dr) {
        arb[nod] = max(0, arb[nod] - val);
        return ;
    }

    int mid = (st + dr) >> 1;

    if(position <= mid) update(st, mid, nod * 2, position, val);
    else update(mid + 1, dr, nod * 2 + 1, position, val);

    arb[nod] = arb[nod * 2] + arb[nod * 2 + 1];
}

void query(int st, int dr, int nod, int x, int y, long long & sum)
{
    if(st >= x && dr <= y) {
        sum += arb[nod];
        return ;
    }

    int mid = (st + dr) >> 1;

    if(x <= mid) query(st, mid, nod * 2, x, y, sum);
    if(mid + 1 <= y) query(mid + 1, dr, nod * 2 + 1, x, y, sum);
}

void usain_bolt()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
}

int main()
{
    usain_bolt();

    int n, m;

    fin >> n >> m;
    for(int i = 1; i <= n; ++i) fin >> a[i];

    build(1, n, 1);

    for(int i = 1; i <= m; ++i) {
        int type, x, y;
        fin >> type >> x >> y;
        if(type == 1) {
            long long sum = 0;

            query(1, n, 1, x, y, sum);
            fout << sum << "\n";
        }
        else {
            update(1, n, 1, x, y);
        }
    }
    return 0;
}