Pagini recente » Cod sursa (job #2946246) | Cod sursa (job #2620772) | Cod sursa (job #1050167) | Cod sursa (job #1093671) | Cod sursa (job #3233500)
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#define NMAX 100005
#define NLOGMAX 18
using namespace std;
const string nume_fisier = "hotel";
ifstream fin(nume_fisier + ".in");
ofstream fout(nume_fisier + ".out");
int pos, val, start, finish, maxi;
int n, m;
struct nod {
int maxi, maxdr, maxst;
};
vector<nod> arb;
void update(int nod, int left, int right)
{
if (left == right)
{
arb[nod].maxi = val;
arb[nod].maxdr = val;
arb[nod].maxst = val;
return;
}
int m = left + right;
m /= 2;
if (pos <= m)
update(2 * nod, left, m);
else
update(2 * nod + 1, m + 1, right);
if (arb[2 * nod].maxdr + arb[2 * nod + 1].maxst > max(arb[2 * nod].maxi, arb[2 * nod + 1].maxi))
{
arb[nod].maxi = arb[2 * nod].maxdr + arb[2 * nod + 1].maxst;
if (arb[2 * nod].maxst == arb[2 * nod].maxdr && arb[2 * nod].maxst == arb[2 * nod].maxi)
{
arb[nod].maxst = arb[2 * nod].maxst + arb[2 * nod + 1].maxst;
}
else {
arb[nod].maxst = arb[2 * nod].maxst;
}
if (arb[2 * nod+1].maxst == arb[2 * nod+1].maxdr && arb[2 * nod+1].maxst == arb[2 * nod+1].maxi)
{
arb[nod].maxdr = arb[2 * nod+1].maxdr + arb[2 * nod].maxdr;
}
else {
arb[nod].maxdr = arb[2 * nod+1].maxdr;
}
}
else {
arb[nod].maxi = max(arb[2 * nod].maxi, arb[2 * nod + 1].maxi);
arb[nod].maxst = arb[2 * nod].maxst;
arb[nod].maxdr = arb[2 * nod + 1].maxdr;
}
}
void afisare()
{
for (int i = 0; i < arb.size(); ++i)
{
cout << arb[i].maxi << " ";
}
cout << '\n';
}
int main()
{
fin >> n >> m;
int arbsize;
arbsize = 1;
while (arbsize < n) {
arbsize *= 2;
}
arb.resize(2 * arbsize);
val = 1;
for (pos = 1; pos <= n; ++pos)
update(1, 1, n);
for (int i = 0; i < m; ++i)
{
int c, a, b;
fin >> c;
if (c == 1)
{
fin >> a >> b;
val = 0;
for (pos = a; pos < a + b; ++pos)
update(1, 1, n);
//afisare();
}
else if (c == 2)
{
fin >> a >> b;
val = 1;
for (pos = a; pos < a + b; ++pos)
update(1, 1, n);
//afisare();
}
else
fout << arb[1].maxi << '\n';
}
}