Cod sursa(job #1892356)

Utilizator NoSwearFlorea Marian NoSwear Data 24 februarie 2017 22:05:49
Problema Arbori de intervale Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include<fstream>
#define NMAX 1205
using namespace std;
int A[4 * NMAX], qa, n, m, ay[NMAX], i, op, x, y, poz;
ifstream cin("arbint.in");
ofstream cout("arbint.out");
void build(int st, int dr, int nod)
{
    if(st == dr)
    {
        A[nod] = ay[st];
        return;
    }
    int mij = st + (dr - st) / 2;

    build(st, mij, 2 * nod);
    build(mij + 1, dr, 2 * nod + 1);

    A[nod] = max(A[2 * nod], A[2 * nod + 1]);
}
void query(int st, int dr, int nod, int a, int b)
{
    if(a <= st && dr <= b)
    {
        qa = max(A[nod], qa);
        return;
    }
    int mij = st + (dr - st) / 2;
    if(a <= mij)
    {
        query(st, mij, 2 * nod, a, b);
    }
    if(b > mij)
    {
        query(mij + 1, dr, 2 * nod + 1, a, b);
    }
}
void update(int st, int dr, int nod, int poz, int x)
{
    if(st == dr)
    {
        A[nod] = x;
        return;
    }
    int mij = st + (dr - st) / 2;
    if(poz <= mij)
    {
        update(st, mij, 2 * nod, poz, x);
    }
    else
    {
        update(mij + 1, dr, 2 * nod + 1, poz, x);
    }
    A[nod] = max(A[2 * nod], A[2 * nod + 1]);
}
int main()
{
    cin >> n >> m;
    for(i = 1; i <= n; i++)
    {
        cin >> ay[i];
    }
    build(1, n, 1);
    while(m--)
    {
        cin >> op;
        if(op == 0)
        {
            cin >> x >> y;
            qa = 0;
            query(1, n, 1, x, y);
            cout << qa << "\n";
        }else
        {
            cin >> poz >> x;
            update(1, n, 1, poz, x);
        }
    }
    return 0;
}