Cod sursa(job #1340471)

Utilizator LycrsTrifan Tamara Lycrs Data 11 februarie 2015 20:41:11
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;

ifstream cin("datorii.in");
ofstream cout("datorii.out");


int i, n, m, x, k, y, j, t[15000*4+100], f, r, u, o,a;

void update(int nod, int st, int dr, int poz, int val)
{
	if (st==dr) t[nod]+=val;
	else
	{
	int mij=(st+dr)/2;
	if (mij>=poz) update(nod*2, st, mij, poz, val);
	else update(nod*2+1, mij+1, dr, poz, val);
	
	t[nod]=t[nod*2] + t[nod*2+1];
	}
	
}

int query(int nod, int st, int dr, int a, int b)
{
    if (a==st && b==dr)
        return t[nod];
    int m=(st+dr)/2;
    if (b<=m)
        return query(2*nod,st,m,a,b);
    if (a>=m+1)
        return query(2*nod+1,m+1,dr,a,b);
    return
        query(2*nod,st,m,a,m)+query(2*nod+1,m+1,dr,m+1,b);
}


int main()
{
	
	cin>>n>>m;
	
	for (i=1; i<=n; ++i)
	{
		cin>>x;
		update (1, 1, n, i, x);
	}
	
	while (m--)
	{
		cin>>o>>x>>y;
		if (o==0)
		{
			y*=-1;
			update(1, 1, n, x, y);
			
		}
		else
			cout<<query(1, 1, n, x, y)<<'\n';

	}

	
    return 0;
}