Cod sursa(job #1161101)

Utilizator omerOmer Cerrahoglu omer Data 31 martie 2014 00:06:18
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.52 kb
#include<stdio.h>
#include<math.h>
#include<algorithm>
const int N=100001;
using namespace std;
FILE *f,*g;

struct nod{

    int st,dr,maxim;
    int ok;
}ai[3*N];

int n,q;

void read(void){

    f=fopen("hotel.in","r");
    g=fopen("hotel.out","w");

    fscanf(f,"%d%d",&n,&q);
    int i;
    for(i=1;i<=3*n;i++) ai[i].ok=0;
    ai[1].ok=2; ai[1].st=n; ai[1].dr=n;ai[1].maxim=n;
}

void rebuild(int poz,int st, int dr,int ok){

    int mid=(st+dr)/2;
    if (ok == 1){
        ai[2*poz].st=0;
        ai[2*poz].dr=0;
        ai[2*poz].ok=1;
        ai[2*poz].maxim=0;

        ai[2*poz+1].st=0;
        ai[2*poz+1].dr=0;
        ai[2*poz+1].ok=1;
        ai[2*poz+1].maxim=0;
    }
    else if (ok == 2){
        ai[2*poz].st=mid-st+1;
        ai[2*poz].dr=mid-st+1;
        ai[2*poz].ok=2;
        ai[2*poz].maxim=mid-st+1;

        ai[2*poz+1].st=dr-mid;
        ai[2*poz+1].dr=dr-mid;
        ai[2*poz+1].ok=2;
        ai[2*poz+1].maxim=dr-mid;
    }
}

void update(int poz, int st, int dr, int lf, int ri,int ok){
    
    if (lf == st && ri == dr){
        
        if (ok == 1){
            
            ai[poz].st=0;
            ai[poz].dr=0;
            ai[poz].ok=ok;
            ai[poz].maxim=0;
        }
        else{
            
            ai[poz].st=dr-st+1;
            ai[poz].dr=dr-st+1;
            ai[poz].ok=ok;
            ai[poz].maxim=dr-st+1;
        }
    }
    else{

        rebuild(poz,st,dr,ai[poz].ok);
        
        int mid=(st+dr)/2, poz1=2*poz, poz2=2*poz+1;

        if (ri<=mid) update(poz1,st,mid,lf,ri,ok);
        else if (lf>=mid+1) update(poz2,mid+1,dr,lf,ri,ok);
            else{
                update(poz1,st,mid,lf,mid,ok);
                update(poz2,mid+1,dr,mid+1,ri,ok);
            }
        ai[poz].ok=0;
        if (ai[poz1].dr==mid-st+1) ai[poz].st=ai[poz1].st+ai[poz2].st;
        else ai[poz].st=ai[poz1].st;
        if (ai[poz2].st==dr-mid) ai[poz].dr=ai[poz2].dr+ai[poz1].dr;
        else ai[poz].dr=ai[poz2].dr;
        ai[poz].maxim= max(ai[poz1].maxim, ai[poz2].maxim);
        ai[poz].maxim= max(ai[poz].maxim, ai[poz1].dr+ai[poz2].st);
        ai[poz].maxim=max(ai[poz].maxim,ai[poz].dr);
        ai[poz].maxim=max(ai[poz].maxim,ai[poz].st);
    }
}

int query(void){

    return ai[1].maxim;
}

void solve(void){

    int i; int x; int a,b;
    for(i=1;i<=q;i++){
        fscanf(f,"%d",&x);
        if (x == 3) fprintf(g,"%d\n",query());
        else {
            fscanf(f,"%d%d",&a,&b);
            update(1,1,n,a,a+b-1,x);
        }
    }
}

int main(void){

    read();
    solve();
 
 
    return 0;
}