Cod sursa(job #2086093)

Utilizator Vlad_CernoutanVlad Cernoutan Vlad_Cernoutan Data 11 decembrie 2017 13:29:22
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <bits/stdc++.h>
#define max 15001
using namespace std;

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

int h[max], n, m, x, y, st, dr, pos, val, type;

void update(int pos, int st, int dr, int idx, int val){
	if(st == dr){
		h[pos]=val;
	}
	int mid=(st+dr)/2;
	if(mid >= idx) update(2*pos, st, mid, idx, val);
	         else update(2*pos+1, mid+1, dr, idx, val);
	h[pos]=h[2*pos]+h[2*pos+1];
}

int query(int pos, int st, int dr, int x, int y){
	int left=0, right=0;
	int mid=(st+dr)/2;
	if(x <=st && dr<= y) return h[pos];
	if(mid >=x ) left=query(2*pos, st, mid, x, y);
	if(mid < y ) right=query(2*pos+1, mid+1, dr, x, y);
	return left+right;
}

int main(){
	fin>>n>>m;
	for(int i=1; i<=n; i++){
		fin>>x;
		update(1, 1, n, i, -x);
	}
	for(int i=1; i<=m; i++){
		fin>>type;
		if(type==0){
			fin>>pos>>x;
			update(1, 1, n, pos, x);
		}
		   else{
		   	fin>>x>>y;
		   	fout<<query(1, 1, n, x, y);
		   }
	}
	return 0;
}