Cod sursa(job #2112914)

Utilizator Y0da1NUME JMECHER Y0da1 Data 23 ianuarie 2018 22:49:38
Problema Hotel Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.91 kb
#include <iostream>
#include <fstream>
#include <algorithm>

const int64_t minim = -1e18;
const int ocupat = -100005;
using namespace std;

class nod{
public:
    int64_t s;  //suma elementelor
    int64_t l; //secv s max care incepe la stanga
    int64_t r; //secv s max care se termina la dreapta
    int64_t m; //secv s max care este continuta in interval
};

int A [100005]; //camere
nod ST[400020];

int N, P;
int M;
int ql, qr;

void update(int st, int dr, int cur, int x, int pos)
{
    if(st == dr)
    {
        ST[cur].s = x;
        ST[cur].l = x;
        ST[cur].r = x;
        ST[cur].m = x;
        return;
    }
    int mij = (st + dr)/2;  //divide
    int ls = 2 * cur;
    int rs = (2 * cur) | 1;
    if (mij < pos)          //impera
        update(mij+1 ,dr ,rs, x, pos);
    else
        update(st, mij, ls, x, pos);
    //etapa combina
    ST[cur].s = ST[ls].s + ST[rs].s;
    ST[cur].l = max(ST[ls].l, ST[ls].s + ST[rs].l);
    ST[cur].r = max(ST[rs].r, ST[rs].s + ST[ls].r);
    ST[cur].m = max(max(ST[ls].m, ST[rs].m), ST[ls].r + ST[rs].l);
}

int main()
{
    int c, M, ceva;
    ifstream in ("hotel.in");
    ofstream out ("hotel.out");
    in>>N>>P;
    for(int i = 1; i <= N; ++i)
        update(1, N, 1, 1, i);  //marcam camerele ca fiind libere
    for(int i = 1; i <= P; ++i)
    {
        in>>c;
        switch (c)
            {
            case 1:
                in>>ceva>>M;
                for(int i = 0; i < M; ++i)
                    update(1, N, 1, ocupat, ceva + i);
                //cout<<ST[1].m<<"\n";
                break;
            case 2:
                in>>ceva>>M;
                for(int i = 0; i < M; ++i)
                    update(1, N, 1, 1, ceva + i);
                //cout<<ST[1].m<<"\n";
                break;
            case 3:
                out<<ST[1].m<<"\n";
                break;
            }
    }
}