Cod sursa(job #2816015)

Utilizator teochess2017Togan Teodor-Bogdan teochess2017 Data 10 decembrie 2021 21:05:13
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <iostream>
#include <stdio.h>

using namespace std;

#define MAXN 16384

int aint[2 * MAXN];///sunt prost , uitasem sa o schimb la loc

void update(int &put, int nrzi, int dif){
  int nod = nrzi - 1 + put;
  while(1 <= nod){
    aint[nod] -= dif;
    nod /= 2;
  }
}
void query(int &st, int &dr, int &s, int nod, int nodst, int noddr){
  if((st <= nodst) && (noddr <= dr)){
    s += aint[nod];
  }else{
    if(st <= (nodst + (noddr - nodst) / 2)){
      query(st, dr, s, nod * 2, nodst, nodst + (noddr - nodst) / 2);
    }
    if((nodst + (noddr - nodst) / 2 + 1) <= dr){
      query(st, dr, s, nod * 2 + 1, nodst + (noddr - nodst) / 2 + 1, noddr);
    }
  }
}

int main()
{
    FILE *fin, *fout;
    int n, m, i, put, op, a, b, s, j;
    fin = fopen("datorii.in", "r");
    fscanf(fin, "%d%d", &n, &m);

    for(put = 1; put <= n; put *= 2);

    for(i = 0; i < n; i++){
      fscanf(fin, "%d", &aint[put + i]);
    }

    ///crearea arborelui de intervale

    put *= 2;
    for(i = put / 2; i < put; i++){
      j = i / 2;
      while(1 <= j){
        aint[j] += aint[i];
        j /= 2;
      }
    }

    put /= 2;
    fout = fopen("datorii.out", "w");
    for(i = 0; i < m; i++){
      fscanf(fin, "%d%d%d", &op, &a, &b);
      if(op == 0){
        update(put, a, b);
      }else{
        s = 0;
        query(a, b, s, 1, 1, put);
        fprintf(fout, "%d\n", s);
      }
    }
    fclose(fin);
    fclose(fout);
    return 0;
}