Cod sursa(job #2932752)
Utilizator | Data | 3 noiembrie 2022 20:50:38 | |
---|---|---|---|
Problema | Hotel | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva de probleme | Marime | 10.39 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in("hotel.in");
ofstream out("hotel.out");
struct bucket
{
int lenst,lendr,lenmax,lng;
bool proc = false;
};
int n,p;
int a[100005],sq,m,last;
bucket b[505];
int main()
{
in >> n >> p;
sq = sqrt(n);
m = n / sq;
if (n % sq != 0)
m++;
for (int i = 1; i < m; i++)
b[i].lenst = b[i].lendr = b[i].lenmax = sq,b[i].lng = sq;
last = n - (m - 1) * sq;
b[m].lenst = b[m].lendr = b[m].lenmax = last,b[m].lng = last;
for (int qq = 1; qq <= p; qq++)
{
int tip,x,y,lg;
in >> tip;
if (tip == 3)
{
int lftt = b[1].lendr,maxim = b[1].lenmax;
for (int i = 2; i <= m; i++)
{
if (b[i].lenmax == b[i].lng)
{
lftt += b[i].lenmax;
maxim = max(maxim,lftt);
}
else
{
lftt += b[i].lenst;
maxim = max(maxim,lftt);
lftt = b[i].lendr;
maxim = max(maxim,b[i].lenmax);
}
}
out << maxim << '\n';
}
else
{
in >> x >> lg;
int y = x + lg - 1;
int bx = x / sq,by = y / sq;
if (x % sq != 0)
bx++;
if (y % sq != 0)
by++;
if (tip == 1)
{
for (int i = bx + 1; i < by; i++)
b[i].lenst = b[i].lendr = b[i].lenmax = 0,b[i].proc = true;
if (bx == by)
{
if (b[bx].proc == true)
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
a[i] = 0;
b[bx].proc = false;
for (int i = x; i <= y; i++)
a[i] = 1;
int pref = 0,suf = 0,lgmaxx = 0;
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
{
if (a[i] != 0)
break;
pref++;
}
for (int i = sq * (bx - 1) + b[bx].lng; i >= sq * (bx - 1) + 1; i--)
{
if (a[i] != 0)
break;
suf++;
}
int curr = 0;
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
{
if (a[i] != 0)
{
lgmaxx = max(lgmaxx,curr);
curr = 0;
}
else
curr++;
}
lgmaxx = max(lgmaxx,curr);
b[bx].lenst = pref,b[bx].lendr = suf,b[bx].lenmax = lgmaxx;
}
else
{
if (b[bx].proc == true)
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
a[i] = 0;
b[bx].proc = false;
for (int i = x; i <= sq * (bx - 1) + b[bx].lng; i++)
a[i] = 1;
int pref = 0,suf = 0,lgmaxx = 0;
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
{
if (a[i] != 0)
break;
pref++;
}
for (int i = sq * (bx - 1) + b[bx].lng; i >= sq * (bx - 1) + 1; i--)
{
if (a[i] != 0)
break;
suf++;
}
int curr = 0;
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
{
if (a[i] != 0)
{
lgmaxx = max(lgmaxx,curr);
curr = 0;
}
else
curr++;
}
lgmaxx = max(lgmaxx,curr);
b[bx].lenst = pref,b[bx].lendr = suf,b[bx].lenmax = lgmaxx;
if (b[by].proc == true)
for (int i = sq * (by - 1) + 1; i <= sq * (by - 1) + b[by].lng; i++)
a[i] = 0;
b[by].proc = false;
for (int i = (by - 1) * sq + 1; i <= y; i++)
a[i] = 1;
pref = 0,suf = 0,lgmaxx = 0;
for (int i = sq * (by - 1) + 1; i <= sq * (by - 1) + b[by].lng; i++)
{
if (a[i] != 0)
break;
pref++;
}
for (int i = sq * (by - 1) + b[by].lng; i >= sq * (by - 1) + 1; i--)
{
if (a[i] != 0)
break;
suf++;
}
curr = 0;
for (int i = sq * (by - 1) + 1; i <= sq * (by - 1) + b[by].lng; i++)
{
if (a[i] != 0)
{
lgmaxx = max(lgmaxx,curr);
curr = 0;
}
else
curr++;
}
lgmaxx = max(lgmaxx,curr);
b[by].lenst = pref,b[by].lendr = suf,b[by].lenmax = lgmaxx;
}
}
else
{
for (int i = bx + 1; i < by; i++)
b[i].lenst = b[i].lendr = b[i].lenmax = sq,b[i].proc = true;
if (bx == by)
{
if (b[bx].proc == true)
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
a[i] = 1;
b[bx].proc = false;
for (int i = x; i <= y; i++)
a[i] = 0;
int pref = 0,suf = 0,lgmaxx = 0;
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
{
if (a[i] != 0)
break;
pref++;
}
for (int i = sq * (bx - 1) + b[bx].lng; i >= sq * (bx - 1) + 1; i--)
{
if (a[i] != 0)
break;
suf++;
}
int curr = 0;
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
{
if (a[i] != 0)
{
lgmaxx = max(lgmaxx,curr);
curr = 0;
}
else
curr++;
}
lgmaxx = max(lgmaxx,curr);
b[bx].lenst = pref,b[bx].lendr = suf,b[bx].lenmax = lgmaxx;
}
else
{
if (b[bx].proc == true)
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
a[i] = 1;
b[bx].proc = false;
for (int i = x; i <= sq * (bx - 1) + b[bx].lng; i++)
a[i] = 0;
int pref = 0,suf = 0,lgmaxx = 0;
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
{
if (a[i] != 0)
break;
pref++;
}
for (int i = sq * (bx - 1) + b[bx].lng; i >= sq * (bx - 1) + 1; i--)
{
if (a[i] != 0)
break;
suf++;
}
int curr = 0;
for (int i = sq * (bx - 1) + 1; i <= sq * (bx - 1) + b[bx].lng; i++)
{
if (a[i] != 0)
{
lgmaxx = max(lgmaxx,curr);
curr = 0;
}
else
curr++;
}
lgmaxx = max(lgmaxx,curr);
b[bx].lenst = pref,b[bx].lendr = suf,b[bx].lenmax = lgmaxx;
if (b[by].proc == true)
for (int i = sq * (by - 1) + 1; i <= sq * (by - 1) + b[by].lng; i++)
a[i] = 1;
b[by].proc = false;
for (int i = (by - 1) * sq + 1; i <= y; i++)
a[i] = 0;
pref = 0,suf = 0,lgmaxx = 0;
for (int i = sq * (by - 1) + 1; i <= sq * (by - 1) + b[by].lng; i++)
{
if (a[i] != 0)
break;
pref++;
}
for (int i = sq * (by - 1) + b[by].lng; i >= sq * (by - 1) + 1; i--)
{
if (a[i] != 0)
break;
suf++;
}
curr = 0;
for (int i = sq * (by - 1) + 1; i <= sq * (by - 1) + b[by].lng; i++)
{
if (a[i] != 0)
{
lgmaxx = max(lgmaxx,curr);
curr = 0;
}
else
curr++;
}
lgmaxx = max(lgmaxx,curr);
b[by].lenst = pref,b[by].lendr = suf,b[by].lenmax = lgmaxx;
}
}
}
}
return 0;
}