Cod sursa(job #2339406)

Utilizator PaulTPaul Tirlisan PaulT Data 8 februarie 2019 20:23:36
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.23 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

ifstream fin("hotel.in");
ofstream fout("hotel.out");

struct Term {
    int lmax, l_left, l_right;
    bool lazy;
};

int n, p, t;
vector<Term> arb;

void Update(int node, int l, int r, int a, int b);

int main()
{
    fin >> n >> p;
    arb = vector<Term>(4 * n + 5, {0, 0, 0, false});
    Update(1, 1, n, 1, n);
    int a, b;
    while (p--)
    {
        fin >> t;
        if (t == 3)
            fout << arb[1].lmax << '\n';
        else
        {
            fin >> a >> b;
            Update(1, 1, n, a, a + b - 1);
        }
    }

    fin.close();
    fout.close();
}

void Update(int node, int l, int r, int a, int b)
{
    int mid = (l + r) / 2;
    if (arb[node].lazy && (l != r))
    {
        if (arb[node].lmax == 0)
        {
            arb[2 * node].l_left = arb[2 * node].l_right = arb[2 * node].lmax = 0;
            arb[2 * node + 1].l_left = arb[2 * node + 1].l_right = arb[2 * node + 1].lmax = 0;
        }
        else
        {
            arb[2 * node].l_left = arb[2 * node].l_right = arb[2 * node].lmax = mid - l + 1;
            arb[2 * node + 1].l_left = arb[2 * node + 1].l_right = arb[2 * node + 1].lmax = r - mid;
        }
        arb[2 * node].lazy = arb[2 * node + 1].lazy = true;
        arb[node].lazy = false;
    }
    if (a <= l && r <= b)
    {
        if (arb[node].lmax == 0)
            arb[node].lmax = arb[node].l_left = arb[node].l_right = r - l + 1;
        else
            arb[node].lmax = arb[node].l_left = arb[node].l_right = 0;
        arb[node].lazy = true;
        return;
    }
    if (a <= mid)
        Update(2 * node, l, mid, a, b);
    if (mid < b)
        Update(2 * node + 1, mid + 1, r, a, b);

    arb[node].lmax = max(max(arb[2 * node].lmax, arb[2 * node + 1].lmax), arb[2 * node].l_right + arb[2 * node + 1].l_left);
    if (arb[2 * node].lmax == mid - l + 1)
        arb[node].l_left = (mid - l + 1) + arb[2 * node + 1].l_left;
    else
        arb[node].l_left = arb[2 * node].l_left;
    if (arb[2 * node + 1].lmax == r - mid)
        arb[node].l_right = r - mid + arb[2 * node].l_right;
    else
        arb[node].l_right = arb[2 * node + 1].l_right;
}