Pagini recente » Cod sursa (job #2010762) | Cod sursa (job #1042972) | Cod sursa (job #1260098) | Cod sursa (job #423746) | Cod sursa (job #659230)
Cod sursa(job #659230)
#include <cstdio>
#include <algorithm>
using namespace std;
struct pct {long x; long y; long z;} a[100001];
struct pct2 {long x; long y;} st, fn, aux;
long N, K;
long i, j, t, nr;
bool compare (pct i, pct j) { return (i.y < j.y); }
int binary_search(int val)
{
int i, step;
for (step = 1; step < K; step <<= 1);
for (i = 0; step; step >>= 1)
if (i + step < K && a[i + step].y <= val)
i += step;
return i;
}
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; ++i)
if (a[j].x == a[i].x) a[j].z = i;
else {
for (t = j+1; t < i; ++t)
a[t] = 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 (i = binary_search(st.y)+1; i <= K && a[i].y <= fn.y; ++i)
if (a[i].x == st.x)
++nr, st.x = 3 - st.x, i = (a[i].z!=0)?a[i].z:i;
if (st.x != fn.x)
++nr;
printf("%ld\n", nr);
--t;
}
return 0;
}