Cod sursa(job #1899563)

Utilizator medicinedoctoralexandru medicinedoctor Data 2 martie 2017 20:10:44
Problema Arbori de intervale Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
//#include <iostream>

using namespace std;

int n, m, b, x, y, s;

vector <pair <int, vector <int> > > a; // .first max pe .second

int main()
{
    ifstream fin ("arbint.in" );
    ofstream cout("arbint.out");
    fin >> n >> m;
    s = sqrt(n);
    a.resize(s + 1);

    for (int i = 0; i < n; i++)
    {
        fin >> x;
        a[i / s].second.push_back(x);
    }

    for (int i = 0; i < a.size(); i++)
        a[i].first = *(max_element(a[i].second.begin(), a[i].second.end() ) );

    for (int i = 0; i < m; i++)
    {
        fin >> b >> x >> y;
        x--;
        if (!(b)) y--;
        if (b)
        { //atribuirea
            a[x / s].second[x % s] = y;
            a[x / s].first = *(max_element(a[x / s].second.begin(), a[x / s].second.end()));
        }
        else
        { //afisarea
            vector<int>::iterator it;
            it = a[x / s].second.end();
            if (x / s == y / s) it = a[x / s].second.begin() + (y % s);
            int t = *(max_element( a[x / s].second.begin() + (x % s), it));


            it = a[y / s].second.begin();
            if (x / s == y / s) it = a[y / s].second.begin() + (x % s);
            t = max(t, *(max_element( it, a[y / s].second.begin() + (y % s) + 1 )) );

            if ((y - x) / s) for (int i = 1 + x / s; i < y / s; i++) t = max(t, a[i].first);
            cout << t << '\n';
        }
    }

    return 0;
}