Cod sursa(job #1749188)

Utilizator delia_ioanaCeapa Delia Ioana delia_ioana Data 28 august 2016 00:40:29
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <climits>
#include <math.h>
#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() {
	infile >> n >> m;

	for (int i = 0; i < n; i ++) {
		infile >> init;
		pos = i;
		val = init;
		constructTree (0, n - 1, 0);
	}
	for (int i = 0; i < 11; i ++)
			cout << arbore[i] << " ";
		cout << endl;
	infile >> aux;
	while (aux == 0) {
		infile >> p1 >> p2 >> aux;
		pos = p1 - 1;
		val = p2;
		constructTree (0, n - 1, 0);
		start ++;
	}
	
	for (int i = start; i < m; i ++) {
		infile >> p1 >> p2;
		a = p1 - 1;
		b = p2 - 1;
		if (aux == 1) {
			sumOwed(0, n - 1, 0);
			outfile << sum << endl;	
			sum = 0;
		}
		else {
			pos = p1 - 1;
			val = p2;
			constructTree (0, n - 1, 0);
		}
		if (i != m - 1)
			infile >> aux;
	}

	return 0;
}