Mai intai trebuie sa te autentifici.
Cod sursa(job #1346084)
Utilizator | Data | 18 februarie 2015 00:26:59 | |
---|---|---|---|
Problema | Hotel | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 2.36 kb |
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("hotel.in");
ofstream fout("hotel.out");
int N, P;
int type, x, y, z;
struct arbore
{
int val, lt, rt, add;
bool viz;
};
arbore AINT[1 << 18];
void init(int nod, int lt, int rt)
{
if (lt == rt)
{
AINT[nod].val = 1;
AINT[nod].lt = 1;
AINT[nod].rt = 1;
return;
}
int mid = (lt + rt) / 2;
init(nod * 2, lt, mid);
init(nod * 2 + 1, mid + 1, rt);
AINT[nod].val = AINT[nod * 2].val + AINT[nod * 2 + 1].val;
AINT[nod].lt = AINT[nod * 2].lt + AINT[nod * 2 + 1].lt;
AINT[nod].rt = AINT[nod * 2].rt + AINT[nod * 2 + 1].rt;
}
void update(int nod, int lt, int rt, int start, int finish, int val)
{
if (start <= lt && rt <= finish)
{
AINT[nod].val = val * (rt - lt + 1);
AINT[nod].lt = val * (rt - lt + 1);
AINT[nod].rt = val * (rt - lt + 1);
AINT[nod].add = val;
AINT[nod].viz = true;
return;
}
int mid = (lt + rt) / 2;
if (AINT[nod].viz == true)
{
update(nod * 2, lt, mid, lt, mid, AINT[nod].add);
update(nod * 2 + 1, mid + 1, rt, mid + 1, rt, AINT[nod].add);
AINT[nod].viz = false;
}
if (start <= mid)
update(nod * 2, lt, mid, start, finish, val);
if (mid < finish)
update(nod * 2 + 1, mid + 1, rt, start, finish, val);
AINT[nod].val = max(AINT[nod * 2].rt + AINT[nod * 2 + 1].lt, max(AINT[nod * 2].val, AINT[nod * 2 + 1].val));
if (AINT[nod * 2].lt == mid - lt + 1)
AINT[nod].lt = AINT[nod * 2].lt + AINT[nod * 2 + 1].lt;
else
AINT[nod].lt = AINT[nod * 2].lt;
if (AINT[nod * 2 + 1].rt == rt - mid)
AINT[nod].rt = AINT[nod * 2 + 1].rt + AINT[nod * 2].rt;
else
AINT[nod].rt = AINT[nod * 2 + 1].rt;
}
int main()
{
fin >> N >> P;
init(1, 1, N);
while (P--)
{
fin >> type;
if (type == 1)
{
fin >> x >> y;
y += x - 1;
update(1, 1, N, x, y, 0);
}
else if (type == 2)
{
fin >> x >> y;
y += x - 1;
update(1, 1, N, x, y, 1);
}
else
fout << AINT[1].val << '\n';
}
fin.close();
fout.close();
return 0;
}