Cod sursa(job #2281764)

Utilizator petrea1551Petrea Calin petrea1551 Data 12 noiembrie 2018 18:34:38
Problema Hotel Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include<bits/stdc++.h>
using namespace std;
 
int n,m;
long long ans,best;
 
struct{
    long long l,r,m,s;
}a[800010];
 
void update(int nod,int l,int r,int pos,int val)
{
    if(l==r)
	{
        a[nod].l=val;
        a[nod].r=val;
        a[nod].m=val;
        a[nod].s=val;
        return;
    } 
    int m=(l+r)/2;
    int ls = 2 * nod, rs = 2 * nod + 1;
    if(pos <= m)
        update(ls, l, m, pos, val);
    else
        update(rs, m + 1, r, pos, val);
    a[nod].s=a[ls].s + a[rs].s;
    a[nod].l=max(a[ls].l, a[ls].s + a[rs].l);
    a[nod].r=max(a[rs].r, a[rs].s + a[ls].r);
    a[nod].m=max(max(a[rs].m, a[ls].m), a[ls].r + a[rs].l);
}
 
void query(int nod,int l,int r,int ql,int qr)
{
    if(qr < l || ql > r)
        return;
    if(qr >= r && ql <= l)
	{
        ans = max(ans, max(a[nod].m, best + a[nod].l));
        best = max(best + a[nod].s, a[nod].r);
        return;
    }
    int m=(l+r) / 2;
    int ls = 2 * nod, rs = 2 * nod + 1;
    query(ls, l, m, ql, qr);
    query(rs, m + 1, r, ql, qr);
}
 
int main()
{
	ifstream cin("hotel.in");
	ofstream cout("hotel.out");
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        update(1, 1, n, i, 1);
    for(int i = 1; i <= m; i++)
	{
        int x, y, z;
        cin >> x;
        if (x == 1)
        {
        	cin >> y >> z;
			for (int j = y; j < y + z; j++)
				update(1, 1, n, j, -100001);
		}
		if (x == 2)
		{
			cin >> y >> z;
			for (int j = y; j < y + z; j++)
				update(1, 1, n, j, 1);
		}
		if (x == 3)
		{
			ans = 0;
   			best = 0;
        	query(1, 1, n, 1, n);
        	cout << ans << '\n';
        }
    }
    return 0;
}