Cod sursa(job #2738118)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 5 aprilie 2021 14:37:27
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include <bits/stdc++.h>
using namespace std;

const string FILENAME = "arbint";
ifstream fin(FILENAME + ".in");
ofstream fout(FILENAME + ".out");

#define pb push_back
#define mp make_pair

using ll = long long;

const int nmax = 100000;

int n, m;
int a[4* nmax + 1];
void build(int nod, int st, int dr)
{
    if(st == dr)
    {
        fin >> a[nod];
    }
    else
    {
        int mid = (st + dr) >> 1;
        build(nod << 1, st, mid);
        build(nod << 1 | 1, mid + 1, dr);
        a[nod] = max(a[nod << 1], a[nod << 1 | 1]);
    }
}

void upd(int nod, int st, int dr, int p, int x)
{
    if(st == dr){
        a[nod] = x;
        return;
    }
    int mid = (st + dr) >> 1;
    if(p <= mid)
        upd(nod << 1, st, mid, p , x);
    else upd(nod << 1 | 1, mid + 1, dr, p, x);
    a[nod] = max(a[nod << 1], a[nod << 1 | 1]);
}

int qry(int nod, int st, int dr, int x, int y)
{
    if(st > dr || st > y || dr < x)
        return -1;
    if(st >= x && dr <= y)
        return a[nod];
    int mid = (st + dr) >> 1;
    int q1 = qry(nod << 1, st, mid, x, y);
    int q2 = qry(nod << 1 | 1, mid + 1, dr, x, y);
    return max(q1, q2);
}

int main()
{
    int op, x, y;
    fin >> n >> m;
    build(1, 1, n);
    while(m--)
    {
        fin >> op >> x >> y;
        if(op == 1)
            upd(1, 1, n, x, y);
        else fout << qry(1, 1, n, x, y) << "\n";
    }
    fin.close();
    fout.close();
	return 0;
}