Cod sursa(job #1619260)

Utilizator qwertyuiTudor-Stefan Berbinschi qwertyui Data 28 februarie 2016 14:29:25
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

vector <int> AIB;
int N, M;


void Form (int pos, int value)
{
    while (pos <= N)
	{
        AIB[pos] += value;
        pos += (pos&(-pos));
	}
}

void Update (int pos, int value)
{
    while (pos <= N)
	{
        AIB[pos] -= value;
        pos += (pos&(-pos));
	}
}

int Query (int pos)
{
    int s = 0;
    while (pos > 0)
	{
        s += AIB[pos];
        pos -= (pos&(-pos));
	}
	return s;
}

int main()
{
    fin >>N >>M;

    AIB.resize(N+1);

    for (int i = 1; i <= N; ++i)
	{
		int x;
		fin >>x;
		Form(i,x);
	}

	for (int i = 1; i <= M; ++i)
	{
        int type, x, y;
        fin >>type >>x >>y;
        if (type == 0)
			Update(x,y);
		else
			fout <<Query(y) - Query(x-1) <<'\n';
	}

    return 0;
}