Cod sursa(job #2575167)

Utilizator alisavaAlin Sava alisava Data 6 martie 2020 11:57:30
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.81 kb
#include <bits/stdc++.h>
#define Lson(x) (x * 2)
#define Rson(x) (x * 2 + 1)


using namespace std;

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

int n, m, answer;
int a[100005];
int aint[100005];

void GenArb(int left, int right, int node)
{
    if(left == right)
    {
        aint[node] = a[left];
        return;
    }
    int mij = (left + right) / 2;
    GenArb(left, mij, Lson(node));
    GenArb(mij + 1, right, Rson(node));
    aint[node] = max(aint[Lson(node)], aint[Rson(node)]);
}

void Update(int left, int right, int node, const int &val, const int &poz)
{
    if(left == right)
    {
        a[poz] = val;
        aint[node] = val;
        return;
    }
    int mij = (left + right) / 2;
    if(poz <= mij)
        Update(left, mij, Lson(node), val, poz);
    else
        Update(mij + 1, right, Rson(node), val, poz);
    aint[node] = max(aint[Lson(node)], aint[Rson(node)]);
}
void Query(int left, int right, int node, const int &LeftQ, const int &RightQ)
{
    if(LeftQ <= left && right <= RightQ)
    {
        answer = max(answer, aint[node]);
        return;
    }
    //cout << left << " " << right <<"\n";
    int mij = (left + right) / 2;
    if(LeftQ <= mij)
        Query(left, mij, Lson(node), LeftQ, RightQ);
    if(mij + 1 <= RightQ)
        Query(mij + 1, right, Rson(node), LeftQ, RightQ);
}




int main()
{
    int op, x, y;
    fin >> n >> m;
    for(int i = 1; i <= n; i++)
        fin >> a[i];
    GenArb(1, n, 1);
    for(int i = 1 ; i <= m; i++)
    {
        fin >> op >> x >> y;
        if(op == 0)
        {
            answer = 0;
            Query(1, n, 1, x, y);
            fout << answer << "\n";
        }
        else if(op == 1)
        {
            Update(1, n, 1, y, x);
        }
    }



    return 0;
}