Cod sursa(job #2663631)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 26 octombrie 2020 22:10:13
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.12 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <type_traits>
#include <random>
#include <bitset>
#include <cstring>
#include <stack>
#include <stdlib.h>
#include <map>
#include <set>
using namespace std;
#define SERVER 0
const string nameFile="arbint";
ifstream in (nameFile+".in");
ofstream out(nameFile+".out");
typedef long long ll;
typedef pair <int, int> pii;
#if (SERVER)
    #define in cin
    #define out cout
#endif
struct segNode{
    int data;
    segNode * left, * right;
    segNode(int val=0){
        left=right=nullptr;
        data=val;
    }
    inline friend int getData(segNode * nod){
        return (nod==nullptr) ? 0 : nod->data;
    }
    inline bool ifErase(){
        return (data==0);
    }
    friend segNode combina(segNode * s1, segNode * s2){
        segNode temp(max(getData(s1), getData(s2)));
        temp.left=s1;
        temp.right=s2;
        return temp;
    }
};
class segTree{
public:
    segNode * root;
    int segSize;
    segTree(int n){
        segSize=n;
        root=nullptr;
    }
    void pointUpdate(int poz, int val){
        root=updateUtil(root, 0, segSize-1, poz, val);
    }
    int rangeQuery(int ll, int rr){
        return queryUtil(root, 0, segSize-1, ll, rr);
    }
private:
    segNode * updateUtil(segNode * nodAct, int ll, int rr, int pozQQ, int valQQ){
        if(pozQQ>rr||pozQQ<ll)
            return nodAct;

        if(nodAct==nullptr)
            nodAct=new segNode(0);
        if(ll==rr){
            nodAct->data=valQQ;
            return nodAct;
        }
        int mij=(ll+rr)/2;
        nodAct->left=updateUtil(nodAct->left, ll, mij, pozQQ, valQQ);
        nodAct->right=updateUtil(nodAct->right, mij+1, rr, pozQQ, valQQ);
        *nodAct=combina(nodAct->left, nodAct->right);
        /*if(nodAct->ifErase())
            delete nodAct, nodAct=nullptr;*/
        return nodAct;
    }
    int queryUtil(segNode * nodAct, int ll, int rr, int llQ, int rrQ){
        if(nodAct==nullptr)
            return 0;
        if(ll==rr)
            return nodAct->data;
        int mij=(ll+rr)/2;
        if(llQ<=mij&&mij+1<=rrQ)
            return max(queryUtil(nodAct->left, ll, mij, llQ, rrQ), queryUtil(nodAct->right, mij+1, rr, llQ, rrQ));
        else if(llQ<=mij)
            return queryUtil(nodAct->left, ll, mij, llQ, rrQ);
        else if(mij+1<=rrQ)
            return queryUtil(nodAct->right, mij+1, rr, llQ, rrQ);
    }
};


/*const int nmax=1e5;
int n, x;
int a[nmax+2];
vector <int> nr[nmax+2];*/
int n, qq, x, y, z;
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    /*in>>n;
    for(int i=1; i<=n; i++){
        in>>a[i];
        nr[a[i]].push_back(i);
    }*/
    in>>n>>qq;
    segTree arb(n+1);
    for(int i=1; i<=n; i++){
        in>>x;
        arb.pointUpdate(i, x);
    }
    for(int i=1; i<=qq; i++){
        in>>x>>y>>z;
        if(x==0)
            out<<arb.rangeQuery(y, z)<<"\n";
        else
            arb.pointUpdate(y, z);
    }
    return 0;
}