Cod sursa(job #1151752)

Utilizator andreiagAndrei Galusca andreiag Data 24 martie 2014 12:44:35
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <fstream>
#include <string.h>

using namespace std;
const int Nmax = 4*100005;

int A[Nmax];
int maxim, pw=1, a, b;
inline int min (int x, int y) { return x < y ? x : y; }
inline int max (int x, int y) { return x > y ? x : y; }

void update(int pos, int num)
{
    int t = pw + pos - 1;
    A[t] = num;
    while(t /= 2)
    {
        A[t] = max(A[2*t], A[2*t + 1]);
    }
}

void querry(int nod, int st, int dr)
{
    if (a <= st && dr <= b)
    {
        maxim = max(maxim, A[nod]);
        return;
    }
    int mid = (st + dr) /2;
    if (a <= mid) querry(2*nod, st, mid);
    if (mid < b)  querry(2*nod+1, mid+1, dr);
}

int main()
{
    ifstream f ("arbint.in");
    ofstream g ("arbint.out");

    int N, M, q;
    f >> N >> M;
    while (pw < N) pw *= 2;
    memset(A, 0, sizeof(A));

    for (int i = 0; i < N; i++)
    {
        f >> A[i + pw];
    }
    
    for (int i = pw -1; i > 0; i--)
    {
        A[i] = max(A[2*i], A[2*i+1]);
    }

    while(M--)
    {
        f >> q >> a >> b;
        if (q == 1)
            update(a, b);
        else if (q == 0)
        {
            maxim = 0;
            querry(1, 1, pw);
            g << maxim << '\n';
        }
    }

    return 0;
}