Pagini recente » Cod sursa (job #788806) | Cod sursa (job #2816578) | Cod sursa (job #131135) | Cod sursa (job #2535157) | Cod sursa (job #1263878)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("hotel.in");
ofstream g ("hotel.out");
const int NMAX = 100000 + 1;
int n, p;
int a, b;
int lg[8 * NMAX], lg_a[8 * NMAX], lg_b[8 * NMAX], lazy[8 * NMAX];
void build(int nod, int st, int dr) {
if (st > dr) return;
if (st == dr) {
lg[nod] = lg_a[nod] = lg_b[nod] = 1;
}
else {
int m = (st + dr) / 2;
build(nod * 2, st, m);
build(nod * 2 + 1, m + 1, dr);
lg[nod] = lg[2 * nod] + lg[2 * nod + 1];
lg_a[nod] = lg[nod];
lg_b[nod] = lg[nod];
}
}
void update(int nod, int st, int dr) {
if (lazy[nod] == 0) {
lg[2 * nod] = 0; lg[2 * nod + 1] = 0;
lg_a[2 * nod] = 0; lg_a[2 * nod + 1] = 0;
lg_b[2 * nod] = 0; lg_b[2 * nod + 1] = 0;
lazy[2 * nod] = 0;
lazy[2 * nod + 1] = 0;
lazy[nod] = -1;
}
if(lazy[nod] == 1) {
int m = (st + dr) / 2;
lg[2 * nod] = lg_a[2 * nod] = lg_b[2 * nod] = m - st + 1;
lg[2 * nod + 1] = lg_a[2 * nod + 1] = lg_b[2 * nod + 1] = dr - m;
lazy[2 * nod] = 1;
lazy[2 * nod + 1] = 1;
lazy[nod] = -1;
}
if (st > dr || st > b || dr < a) return;
if (a <= st && dr <= b) {
lg[nod] = 0;
lg_a[nod] = 0;
lg_b[nod] = 0;
lazy[nod] = 0;
return;
}
int m = (st + dr) / 2;
update(2 * nod, st, m);
update(2 * nod + 1, m + 1, dr);
lg[nod] = max(lg[2 * nod], lg[2 * nod + 1]);
lg[nod] = max(lg[nod], lg_b[2 * nod] + lg_a[2 * nod + 1]);
if (lg[2 * nod] == m - st + 1) lg_a[nod] = lg[2 * nod] + lg_a[2 * nod + 1];
else lg_a[nod] = lg_a[2 * nod];
if (lg[2 * nod + 1] == dr - m) lg_b[nod] = lg[2 * nod + 1] + lg_b[2 * nod];
else lg_b[nod] = lg_b[2 * nod + 1];
}
void sterge(int nod, int st, int dr) {
if (lazy[nod] == 0) {
lg[2 * nod] = 0; lg[2 * nod + 1] = 0;
lg_a[2 * nod] = 0; lg_a[2 * nod + 1] = 0;
lg_b[2 * nod] = 0; lg_b[2 * nod + 1] = 0;
lazy[2 * nod] = 0;
lazy[2 * nod + 1] = 0;
lazy[nod] = -1;
}
if(lazy[nod] == 1) {
int m = (st + dr) / 2;
lg[2 * nod] = lg_a[2 * nod] = lg_b[2 * nod] = m - st + 1;
lg[2 * nod + 1] = lg_a[2 * nod + 1] = lg_b[2 * nod + 1] = dr - m;
lazy[2 * nod] = 1;
lazy[2 * nod + 1] = 1;
lazy[nod] = -1;
}
if (st > dr || st > b || dr < a) return;
if (a <= st && dr <= b) {
lg[nod] = dr - st + 1;
lg_a[nod] = dr - st + 1;
lg_b[nod] = dr - st + 1;
lazy[nod] = 1;
return;
}
int m = (st + dr) / 2;
sterge(2 * nod, st, m);
sterge(2 * nod + 1, m + 1, dr);
lg[nod] = max(lg[2 * nod], lg[2 * nod + 1]);
lg[nod] = max(lg[2 * nod], lg_b[2 * nod] + lg_a[2 * nod + 1]);
if (lg[nod * 2] == m - st + 1) lg_a[nod] = lg[2 * nod] + lg_a[2 * nod + 1];
else lg_a[nod] = lg_a[2 * nod];
if (lg[2 * nod + 1] == dr - m) lg_b[nod] = lg[2 * nod + 1] + lg_b[2 * nod];
else lg_b[nod] = lg_b[2 * nod + 1];
}
void scrie_arb() {
for (int i = 1; i <= 4 * n; i++) g << lg[i] << ' ';
g << endl;
for (int i = 1; i <= 4 * n; i++) g << lg_a[i] << ' ';
g << endl;
for (int i = 1; i <= 4 * n; i++) g << lg_b[i] << ' ';
g << endl << endl;
}
void rezolva() {
build(1, 1, n);
for (int i = 1; i <= 4 * n; i++) lazy[i] = -1;
int q;
for (int i = 1; i <= p; i++) {
f >> q;
if (q == 1) {
f >> a >> b;
b = a + b - 1;
update(1, 1, n);
}
else if (q == 2) {
f >> a >> b;
b = a + b - 1;
sterge(1, 1, n);
}
else g << lg[1] << '\n';
}
}
int main() {
f >> n >> p;
rezolva();
}