Cod sursa(job #2607619)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 29 aprilie 2020 22:12:32
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.15 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)};
    }
};

class SegTree:public obj{

private:
    int sz; vector <obj> seg;

    void update_pos(obj & val, int & 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& stf, int& drf, int poz)
    {
        //cout<<st<<" "<<dr<<" ; "<<stf<<" "<<drf<<": "<<poz<<"\n";
        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];
            //cout<<st<<" "<<dr<<":"<<seg[poz].x1<<"\n";
            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)];
        //cout<<st<<" "<<dr<<":"<<seg[poz].x1<<"\n";
    }
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 & poz, obj & val)
    {
        update_pos(val, poz, 1, sz, 1);
    }

    obj return_range_value(int st, int dr)
    {
        return ret_rng(1, sz, st, dr, 1);
    }

};
ifstream in ("arbint.in");
ofstream out("arbint.out");

int n, m, x, op, y;
vector <obj> val;


int main()
{
    in>>n>>m;
    val.push_back({0});
    for(int i=1; i<=n; i++)
    {
        in>>x;
        val.push_back({x});
    }

    SegTree arb(val);

    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;
}