Cod sursa(job #2607628)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 29 aprilie 2020 22:25:16
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.93 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__
**/

class SegTree:public obj{

private:
    int sz; vector <obj> seg;

    void update_pos(obj 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_pos(val, pozfin, st, mij, poz+1);
        else
            update_pos(val, pozfin, mij+1, dr, poz+2*(mij-st+1));
        seg[poz]=seg[poz+1]+seg[poz+2*(mij-st+1)];
    }

    obj ret_rng(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 ret_rng(st, mij, stf, drf, poz+1)+ret_rng(mij+1, dr, stf, drf, poz+2*(mij-st+1));
        else if(stf<=mij)
            return ret_rng(st, mij, stf, drf, poz+1);
        else if(drf>=mij+1)
            return ret_rng(mij+1, dr, stf, drf, poz+2*(mij-st+1));
    }
    void construct_seg(vector<obj> & 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 <obj> & 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, obj const & val) {
        update_pos(val, poz, 1, sz, 1);
    }

    obj return_range_value(int const & st, int const & dr) {
        return ret_rng(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 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;
}