Cod sursa(job #2902852)

Utilizator Iordache_AnaIordache Ana-Georgiana Iordache_Ana Data 16 mai 2022 21:05:18
Problema Datorii Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
int A[60005];
void update1(int node, int left, int right, int poz, int val)
{
    if(left==right)
    {
        A[node]=val;
        return;
    }
    int mid=(left+right)/2;
    if(poz<=mid)
        update1(2*node, left,mid, poz,val);
    else
        update1(2*node+1,mid+1,right,poz,val);
    A[node]=A[2*node]+A[2*node+1];
}
void update2(int node, int left, int right, int poz, int val)
{
    if(left==right)
    {
        A[node]=A[node]-val;
        return;
    }
    int mid=(left+right)/2;
    if(poz<=mid)
        update2(2*node,left,mid,poz,val);
    else
        update2(2*node+1,mid+1,right,poz,val);
    A[node]=A[2*node]+A[2*node+1];
}
int query(int node, int left, int right, int a,int b)
{
    if(a<=left && right<=b)
        return A[node];
    int mid =(left+right)/2, v1=0, v2=0;
    if(a<=mid)
        v1=query(2*node,left,mid,a,b);
    if(b>mid)
        v2=query(2*node+1, mid+1,right, a,b);
    return v1+v2;
}

int main()
{
    int n,m,op,a,b,x;
    fin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        fin>>x;
        update1(1,1,n,i,x);
    }
    for(int i=1; i<=m; i++)
    {
        fin>>op>>a>>b;
        if(op==0)
            update2(1,1,n,a,b);
        else
            fout<<query(1,1,n,a,b)<<'\n';
    }
    return 0;
}