Cod sursa(job #1605423)

Utilizator Athena99Anghel Anca Athena99 Data 18 februarie 2016 23:41:47
Problema Gropi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <algorithm>
#include <fstream>

using namespace std;

ifstream fin("gropi.in");
ofstream fout("gropi.out");

const int nmax= 100000;

int v[2][nmax+1], d[2];

int main(  ) {
    int c, n;
    fin>>c>>n;
    for ( int i= 1; i<=n; ++i ) {
        int x, y;
        fin>>x>>y;
        if ( x==1 ) {
            ++v[0][0];
            v[0][v[0][0]]= y;
        } else {
            ++v[1][0];
            v[1][v[1][0]]= y;
        }
    }
    ++v[0][0];
    v[0][v[0][0]]= c+1;
    ++v[1][0];
    v[1][v[1][0]]= c+1;
    sort( v[0]+1, v[0]+v[0][0]+1 );
    sort( v[1]+1, v[1]+v[1][0]+1 );
    for ( d[0]= 1; d[0]<=v[0][0]; d[0]*= 2 ) ;
    for ( d[1]= 1; d[1]<=v[1][0]; d[1]*= 2 ) ;

    int m;
    fin>>m;
    for ( int cnt= 1; cnt<=m; ++cnt ) {
        int x1, y1, x2, y2;
        fin>>x1>>y1>>x2>>y2;
        --x1, --x2;

        if ( y1>y2 ) {
            int aux= x1;
            x1= x2, x2= aux;
            aux= y1;
            y1= y2, y2= aux;
        }

        int sol= y2-y1+1;
        while ( y1<y2 ) {
            int aux= v[x1][0], y;
            for ( int step= d[x1]; step>1; step/= 2 ) {
                if ( aux-step>=1 && v[x1][aux-step]>y1 ) {
                    aux-= step;
                }
            }

            y= v[x1][aux];
            if ( y>=y2+1 ) {
                y1= y2;
            } else {
                y1= y-1;
                x1^= 1;
                ++sol;
            }
        }

        if ( x1!=x2 ) {
            ++sol;
        }
        fout<<sol<<"\n";
    }

    return 0;
}