Cod sursa(job #3037712)

Utilizator AdrianRosuRosu Adrian Andrei AdrianRosu Data 26 martie 2023 11:33:05
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.45 kb
#include<bits/stdc++.h>
#define DIM 334
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
unordered_multiset <int> bucket[DIM];
unordered_multiset <int> :: iterator it;
int v[DIM * DIM], min_bucket[DIM], bucket_len[DIM], bucket_identifier[DIM * DIM];
int n, query, i, j, task, poz, val, st, dr, curr_bucket;
int Update(int buffer){
    int Max = 0;
    for(it = bucket[buffer].begin(); it != bucket[buffer].end();it++)
        Max = max(Max, *it);
    return Max;
}
char c;
bool GetInt(int &x){
    if(c == -1)
        return false;
    while((c = getchar()) && c == ' ');
    if(c == -1)
        return false;
    int sign = (c == '-' ? -1 : 1);
    if(isdigit(c))
        x = c - '0';
    else x = 0;
    while((c = getchar()) && isdigit(c))
        x = x * 10 + c - '0';
    x *= sign;
    return true;
}
int Query(int st, int dr){
    int Max = 0;
    if(bucket_identifier[st] == bucket_identifier[dr]){
        for(int i=st;i<=dr;i++)
            Max = max(Max, v[i]);
        return Max;
    }
    int l = st;
    while(bucket_identifier[l] == bucket_identifier[st])
        Max = max(Max, v[l++]);
    int r = dr;
    while(bucket_identifier[r] == bucket_identifier[dr])
        Max = max(Max, v[r--]);
    for(int i=bucket_identifier[l];i<=bucket_identifier[r];i++)
        Max = max(Max, min_bucket[i]);
    return Max;
}
int main(){
    freopen("arbint.in", "r", stdin);
    GetInt(n);
    GetInt(query);
    for(i=1;i<=n;i++){
        GetInt(v[i]);
        if(!curr_bucket || bucket_len[curr_bucket] == DIM){
            curr_bucket++;
            bucket_len[curr_bucket] = 1;
            bucket[curr_bucket].insert(v[i]);
            min_bucket[curr_bucket] = v[i];
            bucket_identifier[i] = curr_bucket;
        }
        else {
            bucket_len[curr_bucket]++;
            bucket[curr_bucket].insert(v[i]);
            min_bucket[curr_bucket] = max(min_bucket[curr_bucket], v[i]);
            bucket_identifier[i] = curr_bucket;
        }
    }
    while(query--){
        GetInt(task);
        GetInt(poz);
        GetInt(val);
        if(task == 1){
            bucket[bucket_identifier[poz]].erase(bucket[bucket_identifier[poz]].find(v[poz]));
            bucket[bucket_identifier[poz]].insert(val);
            v[poz] = val;
            min_bucket[bucket_identifier[poz]] = Update(bucket_identifier[poz]);
        }
        else fout << Query(poz, val) << "\n";
    }
}