Cod sursa(job #941541)

Utilizator Ionut228Ionut Calofir Ionut228 Data 18 aprilie 2013 23:53:47
Problema Gropi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.24 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

int c, n, m;
int zona[100002];
int poz1, poz2, x12, y12, x1, x2, y1, y2, d;

struct gropi
{
    int x, y;
};
gropi g[100002];

struct puncte
{
    int poz, nr;
};
puncte v[100002];

bool cmp(gropi a, gropi b)
{
    return (a.y < b.y);
}

int cautbin1(int x)
{
    int l = 0, r = zona[0] + 1;
    while (r - l > 1)
    {
        int mij = (l + r) / 2;
        if (zona[mij] < x)
            l = mij;
        else
            r = mij;
    }
    return r;
}

int cautbin2(int x)
{
    int l = 0, r = n + 1;
    while (r - l > 1)
    {
        int mij = (l + r ) / 2;
        if (g[mij].y < x)
            l = mij;
        else
            r = mij;
    }
    return r;
}

int main()
{
    fin >> c >> n;
    for (int i = 1; i <= n; ++i)
        fin >> g[i].x >> g[i].y;

    sort(g + 1, g + n + 1, cmp);

    if (g[1].x == 1)
        g[0].x = 2;
    else
        g[0].x = 1;

    if (g[n].x == 1)
        g[n + 1].x = 2;
    else
        g[n + 1].x = 1;
    g[n + 1].y = c + 1;

    zona[0] = 0;
    for (int i = 1; i <= n + 1; ++i)
    {
        if (g[i].x != g[i - 1].x)
        {
            zona[++zona[0]] = g[i].y - 1;
            v[zona[0]].poz = g[i - 1].x;
            v[zona[0]].nr = g[i- 1].y;
        }
    }

    fin >> m;
    for (int i = 1; i <= m; ++i)
    {
        fin >> x1 >> y1 >> x2 >> y2;
        if (y1 > y2)
        {
            x12 = x1;
            x1 = x2;
            x2 = x12;
            y12 = y1;
            y1 = y2;
            y2 = y12;
        }

        d = y2 - y1 + 1;

        poz1 = cautbin1(y1);
        poz2 = cautbin1(y2);

        if (poz1 == poz2 && (cautbin2(y1) == cautbin2(y2)))
        {
            if (x1 != x2)
                ++d;
            fout << d << "\n";
            continue;
        }
        if (y1 < v[poz1].nr && x1 == v[poz1].poz)
            ++d;
        if (x2 == v[poz2].poz)
            ++d;
        d += poz2 - poz1;
        if (poz1 < poz2 && y1 >= v[poz1].nr && x1 == v[poz1].poz)
            --d;
        fout <<d << "\n";
    }

    fin.close();
    fout.close();
    return 0;
}