Cod sursa(job #2887349)

Utilizator petru-robuRobu Petru petru-robu Data 9 aprilie 2022 13:45:37
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <bits/stdc++.h>
#define N 15002
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");

int A[N], aint[2*N], m, n, res=0;

void citire()
{
  fin>>n>>m;
  for(int i=1; i<=n; i++)
    fin>>A[i];
}

void build(int nod, int st, int dr)
{
  if(st==dr)
    aint[nod]=A[st];
  else
  {
    int m = st/2 + dr/2;
    build(2*nod, st, m);
    build(2*nod+1, m+1, dr);
    aint[nod]=aint[2*nod]+aint[2*nod+1];
  }
}

void update(int nod, int st, int dr, int poz, int sum)
{
  if(st==dr)
    aint[nod] -= sum;
  else
  {
    int m = st/2 + dr/2;
    if(poz<=m)
      update(2*nod, st, m, poz, sum);
    else
      update(2*nod+1, m+1, dr, poz, sum);

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

void interogare(int nod, int st, int dr, int stq, int drq)
{
  if(st>=stq && dr<=drq)
  {
    res+=aint[nod];
    return;
  }
  else
  {
    int m = st/2+dr/2;
    if(stq<=m)
      interogare(2*nod, st, m, stq, drq);
    if(drq>m)
      interogare(2*nod+1, m+1, dr, stq, drq);
  }
}


int main()
{
  citire();
  build(1, 1, n);

  for(int i=0; i<m; i++)
  {
    int t, a, b;
    fin>>t>>a>>b;
    if(t)
      {
        interogare(1,1,n,a,b);
        fout<<res<<'\n';
        res=0;
      }
    else
      update(1,1,n,a,b);
  }

  fin.close();fout.close();
  return 0;
}