Cod sursa(job #3139157)

Utilizator CrobertCCampeanu Robert CrobertC Data 25 iunie 2023 18:13:47
Problema Arbori de intervale Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.77 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

ifstream fin("arbint.in");
ofstream fout("arbint.out");

struct tree{
    tree* left;
    tree* right;
    long long mx;
    long long mn;
};

long n, m;
map<long , map<long, tree*> > nodes;

tree* build(long l, long r){
    if(nodes[l][r] != NULL) return nodes[l][r];
    else{
        tree* curt = new tree;
        tree* lt= build(l, r-1);
        tree* rt= build(l+1, r);
        curt->mx = max(lt->mx, rt->mx);
        curt->mn = min(lt->mx, rt->mx);
        lt->right = curt;
        rt->left = curt;
        nodes[l][r]=curt;
        return curt;
    }
}


void update(tree* curnod, long long rep, long long repx){
    if(curnod->mx == repx){
        long long mns = curnod->mn;
        long long mxs = curnod->mx;
        if(rep > curnod->mn){
            curnod->mx = rep;
            if(curnod->right != NULL) update(curnod->right, rep, mxs);
            if(curnod->left != NULL) update(curnod->left, rep, mxs);
        }
        else if(rep < curnod->mn){
            curnod->mx = mns;
            curnod->mn = rep;
            if(curnod->right != NULL) update(curnod->right, mns, mxs);
            if(curnod->left != NULL) update(curnod->left, mns, mxs);
        }
        else{
            curnod->mx = rep;
            if(curnod->right != NULL) update(curnod->right, rep, mxs);
            if(curnod->left != NULL) update(curnod->left, rep, mxs);
        }
    }
    else if(curnod->mn == repx){
        long long mns = curnod->mn;
        long long mxs = curnod->mx;
        if(rep > curnod->mx){
            curnod->mx = rep;
            curnod->mn = mxs;
            if(curnod->right != NULL) update(curnod->right, rep, mxs);
            if(curnod->left != NULL) update(curnod->left, rep, mxs);
        }
        else if(rep < curnod->mx){
            curnod->mn = rep;
        }
        else{
            curnod->mn = rep;
            if(curnod->right != NULL) update(curnod->right, rep, mxs);
            if(curnod->left != NULL) update(curnod->left, rep, mxs);
        }
    }
}

int main()
{
    fin>>n>>m;
    for(long i=1;i<=n;i++){
        nodes[i][i] = new tree;
        fin>>nodes[i][i]->mx;
        nodes[i][i]->mn = nodes[i][i]->mx;
    }
    build(1, n);
    int q, l, r;
    for(long i=1;i<=m;i++){
        fin>>q>>l>>r;
        if(q==0) fout<<nodes[l][r]->mx<<'\n';
        else if(q==1){
            tree* curnod=nodes[l][l];
            long long mxs = curnod->mx;
            curnod->mn = r;
            curnod->mx = r;
            if(curnod->right != NULL) update(curnod->right, r, mxs);
            if(curnod->left != NULL) update(curnod->left, r, mxs);
        }
    }
    return 0;
}