Cod sursa(job #2034235)

Utilizator vasilicamoldovanVasilica Moldovan vasilicamoldovan Data 7 octombrie 2017 17:15:19
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
#include <algorithm>
using namespace std;
const int NMAX = 100000;
int aint[4 * NMAX + 5];
int n, m, x, a, b, q, ans, poz;


void update(int node, int st, int dr);
void query(int node, int st, int dr);


int main()
{
    ifstream cin("arbint.in");
    ofstream cout("arbint.out");
    cin >> n >> m;
    for(poz = 1; poz <= n; ++poz)
    {
        cin >> x;
        update(1, 1, n);
    }


    for(int i = 1; i <= m; ++i)
    {
        cin >> q >> a >> b;
        if(q == 0)
        {
            ans = 0;
            query(1, 1, n);
            cout << ans << "\n";
        }
        else
        {
			poz = a, x = b;
            update(1, 1, n);
        }
    }
    return 0;
}


void update(int node, int st, int dr)
{
    if(st == dr) {
        aint[node] = x;
        return;
    }
    int med = (st + dr) / 2;
    if(poz <= med)
        update(node * 2, st, med);
    else
        update(node * 2 + 1, med + 1, dr);

    aint[node] = max(aint[node * 2], 1);
}

void query(int node, int st, int dr)
{
    if(a <= st && dr <= b)
    {
        ans = max(ans, aint[node]);
        return;
    }
    if(st == dr)
        return;
    int med = (st + dr) / 2;
    if(a <= med)
        query(node * 2, st, med);
    if(b > med)
        query(node * 2 + 1, med + 1, dr);

}