Cod sursa(job #2287263)

Utilizator marcudanfDaniel Marcu marcudanf Data 21 noiembrie 2018 18:25:55
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <iostream>
#include <fstream>
#define mid (left + right) / 2
#define next 2 * pos

const int NMAX = 15e3 + 5;

using namespace std;

ifstream fin("datorii.in");
ofstream fout("datorii.out");

int n, m;
int a[3 * NMAX];
int v[NMAX];

void add(int pos, int val, int left, int right){
	if(left == right){
		a[pos] = val;
		v[left] = pos;
		return;
	}
	add(next, val, left, mid);
	add(next + 1, val, mid + 1, right);
	a[pos] = a[next] + a[next + 1];
}

void update(int pos){
	if(!pos)
		return;
	a[pos] = a[next] + a[next + 1];
	update(pos/2);
}

int sum(int pos, int left, int right, int x, int y){
	if(right < x or left > y)
		return 0;
	if(x <= left and right <= y)
		return a[pos];
	return sum(next, left, mid, x, y) + sum(next + 1, mid + 1, right, x, y);
}

int main(){
	fin >> n >> m;
	for(int i = 1; i <= n; i++){
		int x;
		fin >>  x;
		add(i, x, 1, n);
	}
	while(m--){
		int task, x, y;
		fin >> task >> x >> y;
		if(!task){
			a[v[x]] -= y;
			update(v[x] / 2);
		}else{
			fout << sum(1, 1, n, x, y) << '\n';
		}
	}
}