Cod sursa(job #1807058)

Utilizator vladm98Munteanu Vlad vladm98 Data 15 noiembrie 2016 23:02:58
Problema Hotel Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.32 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin ("hotel.in");
ofstream fout ("hotel.out");
int n, i, j, st, dr, maxim, suma, p, t;
class SegmentTree{
    public:
    vector <int> Sum;
    vector <int> Lazy;
    void Reset ()
    {
        Sum.resize (n<<2);
        Lazy.resize(n<<2);
        fill (Lazy.begin(), Lazy.end(), 0);
    }
    void Reset1 (int nod, int st, int dr)
    {
        Sum[nod] = dr-st+1;
        if (st == dr)
            return;
        int mijloc = (st+dr)>>1;
        Reset1 (nod<<1, st, mijloc);
        Reset1 (nod<<1|1, mijloc+1, dr);
    }
    void Push (int nod, int st, int dr, int level)
    {
        if (level == 2)
            return;
        if (Lazy[nod])
        {
            Sum[nod] = Sum[nod] + Lazy[nod] * (dr-st+1);
            if (st<dr)
            {
                Lazy[nod<<1] += Lazy[nod];
                Lazy[nod<<1|1] += Lazy[nod];
            }
            Lazy[nod] = 0;
            int mijloc = (st+dr)>>1;
            if (st<dr)
            {
                Push(nod<<1, st, mijloc, level+1);
                Push(nod<<1|1, mijloc+1, dr, level+1);
            }
        }
    }
    void Build_Lazy (int nod, int st, int dr, int a, int b, int val)
    {
        if (a<=st && dr<=b)
        {
            Lazy[nod] += val;
            Push(nod, st, dr, 0);
            return;
        }
        //Sum[nod] = Sum[nod] + val*(b-a+1);
        int mijloc = (st+dr) >>1;
        if (a<=mijloc)
             Build_Lazy(nod<<1, st, mijloc, a, b, val);
            //else Build_Lazy(nod<<1, st, mijloc, a, mijloc, val);
        if (mijloc<b)
             Build_Lazy(nod<<1|1, mijloc+1, dr, a, b, val);
            //else Build_Lazy(nod<<1|1, mijloc+1, dr, mijloc+1, b, val);
        Sum[nod] = Sum[nod<<1] + Sum[nod<<1|1];
    }
    int RangeSumQuery (int nod, int st, int dr, int a, int b)
    {
        Push(nod, st, dr, 0);
        if (a<=st && dr<=b)
            return Sum[nod];
        int mijloc = (st+dr)>>1;
        int LeftSum = 0, RightSum = 0;
        if (a<=mijloc)
            LeftSum = RangeSumQuery(nod<<1, st, mijloc, a, b);
        if (mijloc<b)
            RightSum = RangeSumQuery(nod << 1|1, mijloc+1, dr, a, b);
        Sum[nod] = Sum[nod<<1] + Sum[nod<<1|1];
        return LeftSum + RightSum;
    }
};
int main()
{
    SegmentTree AInt;
    fin >> n >> p;
    AInt.Reset();
    AInt.Reset1(1, 1, n);
    for (i=0; i<p; ++i)
    {

        fin >> t;
        if (t == 1)
        {
            fin >> st >> dr;
            dr = st+dr-1;
            AInt.Build_Lazy(1, 1, n, st, dr, (-1));
        }
        if (t == 2)
        {
            fin >> st >> dr;
            dr = st+dr-1;
            AInt.Build_Lazy(1, 1, n, st, dr, 1);
        }
        if (t == 3)
        {
            maxim = 0;
            st = dr = 1;
            while (dr<=n)
            {
                suma = AInt.RangeSumQuery(1, 1, n, st, dr);
                if (suma == (dr-st+1))
                {
                    if (maxim < (dr-st+1))
                        maxim = dr-st+1;
                    ++dr;
                }
                else
                {
                    ++dr;
                    st=dr;
                }
            }
            fout << maxim << '\n';
        }
    }
    return 0;
}