Pagini recente » Cod sursa (job #398783) | Cod sursa (job #1266364) | Cod sursa (job #828304) | Cod sursa (job #2828592) | Cod sursa (job #744810)
Cod sursa(job #744810)
#include <stdio.h>
#include <algorithm>
using namespace std;
struct groapa
{
int x, y;
} x[100100];
inline bool comp(groapa A, groapa B)
{
return A.y < B.y;
}
int zone[100100], last[100100], cul[100100];
inline int search(int val)
{
int st = 1, dr = zone[0], med;
while (st <= dr)
{
med = (st + dr) / 2;
if (zone[med] == val)
return med;
if (zone[med] < val)
st = med + 1;
else
dr = med - 1;
}
return st;
}
inline void schimba(int &A, int &B)
{
int aux = A;
A = B;
B = aux;
}
inline int changeCol(int col)
{
if (col == 1)
return 2;
return 1;
}
int main()
{
int C, N, Q, x0, y0, x1, y1, i, sol, zoneX0, zoneX1;
freopen("gropi.in", "r", stdin);
freopen("gropi.out", "w", stdout);
scanf("%d%d", &C, &N);
for (i = 1; i <= N; i ++)
scanf("%d%d", &x[i].x, &x[i].y);
sort(x + 1, x + N + 1, comp);
x[0].x = changeCol(x[1].x);
x[N + 1].x = changeCol(x[N].x); x[N + 1].y = C + 1;
for (i = 1; i <= N + 1; i ++)
if (x[i].x != x[i - 1].x)
{
zone[++ zone[0]] = x[i].y - 1;
cul[zone[0]] = x[i - 1].x;
last[zone[0]] = x[i - 1].y;
}
scanf("%d", &Q);
while (Q --)
{
scanf("%d%d%d%d", &x0, &y0, &x1, &y1);
if (y0 > y1)
{
schimba(x0, x1);
schimba(y0, y1);
}
sol = y1 - y0 + 1;
zoneX0 = search(y0);
zoneX1 = search(y1);
//cazuri particulare... bleah
if (y0 < last[zoneX0] && x0 == cul[zoneX0])
sol ++;
if (x1 == cul[zoneX1])
sol ++;
if (zoneX0 == zoneX1 && y0 >= last[zoneX0] && y1 >= last[zoneX1] && x0 == x1)
sol --;
sol += zoneX1 - zoneX0;
if (zoneX0 < zoneX1 && y0 >= last[zoneX0] && x0 == cul[zoneX0])
sol --;
printf("%d\n", sol);
}
return 0;
}