Pagini recente » Cod sursa (job #1788505) | Cod sursa (job #2773541) | Cod sursa (job #1100343) | Cod sursa (job #1324308) | Cod sursa (job #659258)
Cod sursa(job #659258)
#include <cstdio>
#include <algorithm>
using namespace std;
struct pct {long x; long y; long z;} a[100002];
struct pct2 {long x; long y;} st, fn, aux;
long N, K;
long i, j, t, nr, step;
bool compare (pct i, pct j) { return (i.y < j.y); }
int main() {
freopen("gropi.in","r",stdin);
freopen("gropi.out","w",stdout);
scanf("%ld %ld", &N, &K);
for (i = 1; i <= K; ++i)
scanf("%ld %ld", &a[i].x, &a[i].y);
sort (a, a+K+1, compare);
j = 1;
for (i = 2; i <= K+1; ++i)
if (a[j].x != a[i].x) {
for (t = j; t < i; ++t)
a[t].z = i - 1;
j = i;
}
scanf("%ld", &t);
while (t) {
scanf("%ld %ld %ld %ld", &st.x, &st.y, &fn.x, &fn.y);
if (st.y > fn.y)
aux = st, st = fn, fn = aux;
nr = fn.y - st.y + 1;
for (step = 1; step < K; step <<= 1);
for (j = 0; step; step >>= 1)
if (j + step < K && a[j + step].y <= st.y)
j += step;
++j;
if (a[j].x != st.x) j = a[j].z + 1;
for (i = j; i <= K && a[i].y <= fn.y; ++i)
++nr, st.x = 3 - st.x, i = a[i].z;
if (st.x != fn.x)
++nr;
printf("%ld\n", nr);
--t;
}
return 0;
}