Cod sursa(job #2708957)

Utilizator modulopaulModulopaul modulopaul Data 19 februarie 2021 16:38:50
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
#include <iostream>
#define MAXN 100000

using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int ai[MAXN], n, m, c, x, y, cer;
int maxx(int a, int b)
{
    int c;
    while(b){
        c = a%b;
        a = b;
        b = c;
    }

    return a;
}
void update(int nod, int st, int dr, int poz, int val)
{
    if (st == dr){
        ai[nod] = val;
        return;
    }
    int mij = (st+dr)/2;
    if (poz <= mij)
        update(2*nod, st, mij, poz, val);
    else
        update(2*nod+1, mij+1, dr, poz, val);
    ai[nod] = maxx(ai[2*nod], ai[2*nod+1]);
}
void query(int nod, int st, int dr, int a, int b)
{
    if (a <= st && dr <= b)
    {
        c = maxx(c, ai[nod]);
        return;
    }
    int mij = (st+dr)/2;
    if (a <= mij)
        query(2*nod, st, mij, a, b);
    if (mij < b)
        query(2*nod+1, mij+1, dr, a, b);
}
int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++)
    {
        fin >> x;
        update(1, 1, n, i, x);
    }
    fin >> m;
    for (int i = 1; i <= m; i++)
    {
        fin >> cer >> x >> y;
        if (cer == 0)
        {
            c = 0;
            query(1, 1, n, x, y);
            fout << c << '\n';
        }
        else
            update(1, 1, n, x, y);
    }
    return 0;
}