Cod sursa(job #3248484)

Utilizator RobertCNMBrobertM RobertCNMB Data 11 octombrie 2024 21:52:15
Problema Arbori de intervale Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.85 kb
#include <fstream>
#include <vector>
#include <cmath>

using namespace std;
ifstream cin ("arbint.in");
ofstream cout ("arbint.out");

const int NMAX = 1e5;
const int INT_MIN = 0;

int n, q, sqrtn, nrbuckets;
vector<vector<int>> buckets;
vector<int> max_interv;

void citire(){
    cin >> n >> q;

    sqrtn = sqrt(n);
    nrbuckets = sqrtn;
    if (sqrtn * sqrtn != n)
        ++nrbuckets;
    buckets.resize(nrbuckets + 1);      // fiindca folosim indexare de la 1

    for (int i = 1; i <= n; ++i){
        int x;
        cin >> x;

        int ind_bucket = (i - 1) / sqrtn + 1;
        buckets[ind_bucket].push_back(x);
    }
}

void update_interv(int ind){
    max_interv[ind] = INT_MIN;
    for (auto x : buckets[ind]){
        if (x > max_interv[ind])
            max_interv[ind] = x;
    }
}

void update_pos(int pos, int val){
    int ind_bucket = (pos - 1) / sqrtn + 1;
    buckets[ind_bucket][(pos - 1) % sqrtn] = val;
    update_interv(ind_bucket);
}

int query(int l, int r){
    int ans = INT_MIN;

    for (int i = l; i <= r; ++i){
        int ind_bucket = (i - 1) / sqrtn + 1;
        int pos_in_bucket = (i - 1) % sqrtn;

        if (pos_in_bucket == 0 and i + sqrtn <= r){
            if (max_interv[ind_bucket] > ans)
                ans = max_interv[ind_bucket];
            i += sqrtn - 1;
        } else {
            if (buckets[ind_bucket][pos_in_bucket] > ans)
                ans = buckets[ind_bucket][pos_in_bucket];
        }
    }

    return ans;
}

void init(){
    max_interv.resize(nrbuckets + 1);
    for (int i = 1; i <= nrbuckets; ++i){
        update_interv(i);
    }
}

void rezolvare(){
    for (int i = 1; i <= q; ++i){
        bool op;
        int a,b;
        cin >> op >> a >> b;
        if (op == 0){
            cout << query(a, b) << '\n';
        } else update_pos(a, b);
    }
}

int main(){
    citire();
    init();
    rezolvare();
    return 0;
}