Cod sursa(job #1796917)

Utilizator elffikkVasile Ermicioi elffikk Data 3 noiembrie 2016 21:26:15
Problema Arbori de intervale Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

main() {
    ifstream cin("arbint.in");
    ofstream cout("arbint.out");
    int n, m, k;
    cin>>n>>m;
    k = ceil(sqrt(n));
    vector<int> a(n), a2(n/k+1);
    fill(a2.begin(), a2.end(), 0);
    for (int i = 0; i < n; i++) {
        cin>>a[i];
        a2[i/k] = max(a2[i/k], a[i]);
    }
    for(int j = 0; j < m; j++) {
        int x, y, z;
        cin>>z>>x>>y;
        x--;
        if (z == 1) {
            if (a[x] == a2[x/k]) {
                a[x] = y;
                int l = x/k;
                a2[l] = a[x];
                for (int i = l*k; i < l*k+k && i < a.size(); i++) {
                    a2[l] = max(a2[l], a[i]);
                }
            } else {
                a[x] = y;
            }

        } else {
            int i = x, t = a[x];
            while (i < y && i%k > 0) {
                t = max(t, a[i]);
                i++;
            }
            while (i + k <= y) {
                t = max(t, a2[i/k]);
                i+=k;
            }
            while (i < y) {
                t = max(t, a[i]);
                i++;
            }
            cout<<t<<"\n";
        }
    }
}