Cod sursa(job #2321838)

Utilizator alextodoranTodoran Alexandru Raul alextodoran Data 16 ianuarie 2019 18:04:01
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.49 kb
#include <bits/stdc++.h>

#define NM 300002

using namespace std;

int n, p;

struct arbint
{
    int maxpref, maxsuf, maxsub;
    int lazy;
};

arbint ai[NM];

void split(int node, int left, int right)
{
    if(ai[node].lazy == -1)
        return;
    int mid = (left + right) / 2, leftSon = node * 2, rightSon = leftSon + 1;
    if(ai[node].lazy)
        ai[leftSon].maxpref = ai[leftSon].maxsuf = ai[leftSon].maxsub = 0;
    else
        ai[leftSon].maxpref = ai[leftSon].maxsuf = ai[leftSon].maxsub = (mid - left + 1);
    ai[leftSon].lazy = ai[node].lazy;
    if(ai[node].lazy)
        ai[rightSon].maxpref = ai[rightSon].maxsuf = ai[rightSon].maxsub = 0;
    else
        ai[rightSon].maxpref = ai[rightSon].maxsuf = ai[rightSon].maxsub = (right - mid);
    ai[rightSon].lazy = ai[node].lazy;
    ai[node].lazy = -1;
}

arbint join(int left, int right, arbint a, arbint b)
{
    int mid = (left + right) / 2;
    arbint ans;
    ans.maxpref = a.maxpref;
    if(a.maxpref == mid - left + 1)
        ans.maxpref += b.maxpref;
    ans.maxsuf = b.maxsuf;
    if(b.maxsuf == right - mid)
        ans.maxsuf += a.maxsuf;
    ans.maxsub = max(max(max(a.maxsub, b.maxsub), max(ans.maxpref, ans.maxsuf)), a.maxsuf + b.maxpref);
    ans.lazy = -1;
    return ans;
}

void update(int node, int left, int right, int a, int b, bool val)
{
    if(a <= left && right <= b)
    {
        if(val)
            ai[node].maxpref = ai[node].maxsuf = ai[node].maxsub = 0;
        else
            ai[node].maxpref = ai[node].maxsuf = ai[node].maxsub = (right - left + 1);
        ai[node].lazy = val;
        return;
    }
    int mid = (left + right) / 2, leftSon = node * 2, rightSon = leftSon + 1;
    split(node, left, right);
    if(a <= mid)
        update(leftSon, left, mid, a, b, val);
    if(b > mid)
        update(rightSon, mid + 1, right, a, b, val);
    ai[node] = join(left, right, ai[leftSon], ai[rightSon]);
}

int main()
{
    ifstream fin ("hotel.in");
    ofstream fout ("hotel.out");
    fin >> n >> p;
    for(int i = 1; i <= n; i++)
        ai[i].lazy = -1;
    update(1, 1, n, 1, n, 0);
    while(p--)
    {
        int type;
        fin >> type;
        if(type == 3)
        {
            fout << ai[1].maxsub << "\n";
        }
        else
        {
            int left, right, cnt;
            fin >> left >> cnt;
            right = left + cnt - 1;
            update(1, 1, n, left, right, (type == 1));
        }
    }
    return 0;
}