Cod sursa(job #2816727)

Utilizator teodortatomirTeodor Tatomir teodortatomir Data 11 decembrie 2021 22:45:12
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
// am recitit mesajul cu sugestii si am obsevrat ca mi-ati sugerat sa las spatii pe verticala si orizontala, de acum voi incerca sa imi aerisesc codul
#include <stdio.h>
#define MAXX 100000

int v[MAXX+1], a[MAXX*2];
void build(int st, int dr, int nod){
  int mij, cst, cdr;

  if(st == dr) {
    a[nod] = v[st];
    return;
  }


  mij = (st + dr) / 2;
  cst = nod + 1;
  cdr = nod + 2 * (mij - st + 1);
  build(st, mij, cst);
  build(mij + 1, dr, cdr);
  a[nod] = a[cst] + a[cdr];
}
int rez(int st, int dr, int cst, int cdr, int nod) {
  int mij, modif;

  if(cdr < st || cst > dr)
    return 0;
  if(st >= cst && dr <= cdr)
    return a[nod];

  mij = (st + dr) / 2;
  modif = 0;
  if(mij < cdr)
    modif = modif + rez(mij+1, dr, cst, cdr, nod + 2 * (mij - st + 1));
  if(cst <= mij)
    modif = modif + rez(st, mij, cst, cdr, nod+1);

  return modif;
}
void update(int st, int dr, int nod, int p, int x) {
  int mij;

  if(st == dr) {
    a[nod] -= x;
    return ;
  }

  mij = (st + dr) / 2;
  if(p <= mij)
    update(st, mij, nod + 1, p, x);
  else
    update(mij+1, dr, nod + 2 * (mij - st + 1), p, x);
  a[nod] = a[nod + 1] + a[nod + 2 * (mij - st + 1)];
}
int main(){
  FILE *fin,*fout;
  int n, m, i, x, y, cer;

  fin=fopen("datorii.in", "r");
  fout=fopen("datorii.out", "w");

  fscanf(fin, "%d%d", &n, &m);
  for(i = 1; i <= n; i++)
    fscanf(fin, "%d", &v[i]);

  build(1, n, 1);

  for(i = 0; i<m; i++) {
    fscanf(fin, "%d%d%d", &cer, &x, &y);
    if(cer == 1)
      fprintf(fout, "%d\n", rez(1, n, x, y, 1));
    else
      update(1, n, 1, x, y);
  }

  fclose(fin);
  fclose(fout);
  return 0;
}