Cod sursa(job #1749252)

Utilizator delia_ioanaCeapa Delia Ioana delia_ioana Data 28 august 2016 10:15:17
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <climits>
#include <math.h>
#include <cstdio>

#define dim 100001
  
using namespace std;
int m, n, aux = -1, start = 0, p1, p2, arb_size, a, b, pos, val, arbore[dim * 4 + 66], sum = 0, init;
//ifstream infile("datorii.in"); 
//ofstream outfile("datorii.out"); 
 
void constructTree (int start, int end, int poz) {
    if (start == end) {
        if (aux == 0)
            arbore[poz] -= val;
        else arbore[poz] = val;
        return;
    }
    int mid = (start + end) / 2;
    if (pos <= mid) 
        constructTree(start, mid, poz * 2 + 1);
    else constructTree(mid + 1, end, poz * 2 + 2);
    arbore[poz] = arbore[poz * 2 + 1] + arbore[poz * 2 + 2];
} 
 
void sumOwed (int start, int end, int poz) {
    if (start >= a && b >= end){
        sum += arbore[poz];
        return;
    }
    if (b < start || a > end)
        return;
     
    int mid = (start + end) / 2;
    sumOwed(start, mid, poz * 2 + 1);
    sumOwed(mid + 1, end, poz * 2 + 2);
}
 
int main() {
    freopen("datorii.in", "r", stdin);
    freopen("datorii.out", "w", stdout);
//    infile >> n >> m;
    scanf("%d %d", &n, &m);
 
    for (int i = 0; i < n; i ++) {
	scanf("%d", &init);
//        infile >> init;
        pos = i;
        val = init;
        constructTree (0, n - 1, 0);
    }
     
    for (int i = 0; i < m; i ++) {
	scanf("%d %d %d", &aux, &p1, &p2);
//        infile >> aux >> p1 >> p2;
        a = p1 - 1;
        b = p2 - 1;
        if (aux == 1) {
            sumOwed(0, n - 1, 0);
	    printf("%d\n", sum);
//            outfile << sum << endl; 
            sum = 0;
        }
        else {
            pos = p1 - 1;
            val = p2;
            constructTree (0, n - 1, 0);
        }
    }
 
    return 0;
}