Cod sursa(job #2610067)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 4 mai 2020 11:59:49
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.05 kb
#include <iostream>
#include <fstream>
#include <vector>
#define Flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;
class obj{
public:
    int x1;
    obj(){ ///don't forget about the neutral element
        x1=0;
    }
    obj(int const & val){ ///modify for every kind of object
        x1=val;
    }
    inline obj operator + (obj const & vlll2) ///modify for every kind of operation
    const{
        return {max(this->x1 , vlll2.x1)};
    }
};

/**
*   ^^^Object^^^
*   __SegTree__
**/
template <class T>
class SegTree{
private:
    int sz; vector <T> seg;

    void update_position_pv(T const & val, int const & pozfin, int st, int dr, int poz) { ///overwrites the existent value, modify to update current value
        if(st==dr)
            {seg[poz]=val; return;}
        int mij=(st+dr)/2;
        if(pozfin<=mij)
            update_position_pv(val, pozfin, st, mij, poz+1);
        else
            update_position_pv(val, pozfin, mij+1, dr, poz+2*(mij-st+1));
        seg[poz]=seg[poz+1]+seg[poz+2*(mij-st+1)];
    }

    T return_range_value_pv(int st, int dr, int const & stf, int const & drf, int poz) {
        if(st>=stf&&dr<=drf)
            return seg[poz];
        int mij=(st+dr)/2;
        if(stf<=mij&&drf>=mij+1)
            return return_range_value_pv(st, mij, stf, drf, poz+1)+return_range_value_pv(mij+1, dr, stf, drf, poz+2*(mij-st+1));
        else if(stf<=mij)
            return return_range_value_pv(st, mij, stf, drf, poz+1);
        else        //(drf>=mij+1)
            return return_range_value_pv(mij+1, dr, stf, drf, poz+2*(mij-st+1));
    }
    void construct_seg(vector<T> & vec, int st, int dr, int poz) { ///vector should start from 1, modify segtree to start from 0 otherwise
        if(st==dr)
            {seg[poz]=vec[st]; return;}
        int mij=(st+dr)/2;
        construct_seg(vec, st, mij, poz+1);
        construct_seg(vec, mij+1, dr, poz+2*(mij-st+1));
        seg[poz]=seg[poz+1]+seg[poz+2*(mij-st+1)];
    }
public:
    SegTree(int const & n) {
        seg.resize(2*n);
        sz=n;
    }

    SegTree(vector <T> & vec) {
        seg.resize(2*vec.size());
        sz=(vec.size()-1); ///vector should start from 1, modify segtree to start from 0 otherwise
        construct_seg(vec, 1, sz, 1);
    }

    void update_position(int const & poz, T const & val) {
        update_position_pv(val, poz, 1, sz, 1);
    }

    T return_range_value(int const & st, int const & dr) {
        return return_range_value_pv(1, sz, st, dr, 1);
    }
};
ifstream in ("arbint.in");
ofstream out("arbint.out");

int n, m, x, op, y;

int main()
{
    in>>n>>m;
    SegTree <obj> arb(n);
    for(int i=1; i<=n; i++)
    {
        in>>x;
        obj temp(x);
        arb.update_position(i, temp);
    }

    while(m--)
    {
        in>>op>>x>>y;
        if(op)
        {
            obj temp(y);
            arb.update_position(x, temp);
        }
        else
            out<<arb.return_range_value(x, y).x1<<"\n";
    }
    return 0;
}