Cod sursa(job #542734)

Utilizator dacyanMujdar Dacian dacyan Data 26 februarie 2011 21:42:23
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <fstream>
#define DIM 15010
using namespace std;

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

int n, m;
int maxarb[4*DIM];
int poz, val, x, y, sum;

void Update(int,int,int);
void Update2(int,int,int);
void Query(int,int,int);

int main()
{
    fin >> n >> m;
    for (poz = 1; poz <= n; ++poz)
    {
        fin >> val;
        Update2(1,1,n);
    }
    
    int s;
    
    for (int i = 1; i <= m; ++i)
    {
        fin >> s >> x >> y;
        if (s)
        {
            sum = 0;
            Query(1, 1, n);
            fout << sum << '\n';
        }
        else 
        {
            poz = x; val = y;
            Update(1,1,n);
        }
    }
    fin.close();
    fout.close();
    return 0;
}

void Update2(int nod, int st, int dr)
{
    if (st == dr)
    {
        maxarb[nod] = val;
        return;
    }
    int mij = (st + dr) / 2;
    if (poz <= mij) Update2(2*nod, st, mij);
    else Update2(2*nod+1, mij+1, dr);
    maxarb[nod] = maxarb[2*nod] +  maxarb[2*nod+1];
}

void Update(int nod, int st, int dr)
{
    if (st == dr)
    {
        maxarb[nod] -= val;
        return;
    }
    int mij = (st + dr) / 2;
    if (poz <= mij) Update(2*nod, st, mij);
    else Update(2*nod+1, mij+1, dr);
    maxarb[nod] = maxarb[2*nod] + maxarb[2*nod+1];
}

void Query(int nod, int st, int dr)
{
    if (st >= x && dr <= y)
    {
       // if (maxarb[nod] > maxim) maxim = maxarb[nod];
        sum += maxarb[nod];
        return;         
    }
    int mij = (st+dr) / 2;
    if (x <= mij) Query(2*nod, st, mij);
    if (y > mij) Query(2*nod+1, mij+1, dr);
}