Cod sursa(job #2639180)

Utilizator Gliumarin negai Gliu Data 31 iulie 2020 18:27:05
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <iostream>
#include <fstream>

using namespace std;
typedef long long ll; 
ifstream in("datorii.in");
ofstream out("datorii.out");
const int Nmax=100099;
ll n,m,s[Nmax],k,a,b;

ll sum(int x){
	ll result=0;
	int i=x;
	while(i>=0){
	 result+=s[i];
	 i=(i & (i+1))-1;
	}
	return result;
}

void add(int pos,ll diff){
	while(pos <=n){
		s[pos]+=diff;
		pos = pos | (pos+1);
	}
}
void red(int pos,ll diff){
	while(pos <=n){
		s[pos]-=diff;
		pos = pos | (pos+1);
	}
}
 
int main(){
in >>n>>m;
for(int i=1;i<=n;i++){
	in >>k;
	add(i,k);
}
while(m--){
 in >>k;
 	if(k){
 		in >>a>>b;
 		out <<sum(b)-sum(a-1)<<"\n";
	 }else{
	 	in >>a>>b;
	 	red(a,b);
	 }
}
return 0;
}