Cod sursa(job #3226590)

Utilizator petric_mariaPetric Maria petric_maria Data 22 aprilie 2024 10:10:07
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("datorii.in");
ofstream g("datorii.out");
int n,m,i,c,x,y,a[500005],p=1;

void update(int poz, int val){
    a[poz]-=val;
    while(poz>=1){
        poz/=2;
        a[poz] = a[2*poz]+a[2*poz+1];
    }
}

int suma(int stq, int drq, int stn, int drn, int ind){
    if(stq<=stn && drn<=drq) return a[ind];
    int s=0;
    if(stq <= (stn+drn)/2)
        s = suma(stq, drq, stn, (stn+drn)/2, ind*2);
    if((stn+drn)/2 < drq)
        s += suma(stq, drq, (stn+drn)/2+1, drn, ind*2+1);
    return s;
}

int main()
{
    f>>n>>m;
    p=1;
    while(p<n) p*=2;
    for(i=1; i<=n; ++i){
        f>>x;
        a[p+i-1]=x;
    }
    a[0]=0;
    for(i=p-1; i>=1; --i) a[i] = a[2*i]+a[2*i+1];
    for(i=1; i<=m; ++i){
        f>>c>>x>>y;
        if(c==0){
            x+=(p-1);
            update(x,y);
        }
        else{
            g<<suma(x, y, 1, p-1, 1)<<'\n';
        }
    }
    return 0;
}