Cod sursa(job #2381195)

Utilizator razvanradulescuRadulescu Razvan razvanradulescu Data 16 martie 2019 11:14:07
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <iostream>
#include <cstdio>
using namespace std;

int a[400001];
int n, m, x, y, nnou = 1;
int pb;

void upd(int poz)
{
    if(poz != 1)
    {
        poz/=2;
        a[poz] = a[poz*2] + a[poz*2+1];
        upd(poz);
    }
}

int afis(int st, int dr, int poz)
{
    if(x <= st && y >= dr)
        return a[poz];
    if(x > dr || y < st)
        return 0;
    else
    {
        return afis(st, (st + dr)/2, poz*2) + afis((st + dr)/2+1, dr, poz*2+1);
    }
}

void rez()
{
    scanf("%d %d", &n, &m);
    while(nnou<n)
        nnou*=2;
    for(int i = 1; i<=n; i++)
    {
        scanf("%d", &a[nnou+i-1]);
        upd(nnou+i-1);
    }
    for(int i = 0; i<m; i++)
    {
        scanf("%d %d %d", &pb, &x, &y);
        if(pb == 1)
        {
            printf("%d\n", afis(1, nnou, 1));
        }
        else
        {
            a[nnou + x - 1] -= y;
            upd(nnou + x - 1);
        }
    }
}

int main()
{
    freopen("datorii.in", "r", stdin);
    freopen("datorii.out", "w", stdout);
    rez();
    return 0;
}