Pagini recente » Cod sursa (job #1074865) | Cod sursa (job #880834) | Cod sursa (job #2670057) | Cod sursa (job #1201529) | Cod sursa (job #659212)
Cod sursa(job #659212)
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
struct pct {long x; long y;} st, fn, aux;
long N, K;
long i, j, t, nr;
vector<pct> a;
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);
a.push_back(aux);
for (i = 1; i <= K; ++i) {
scanf("%ld %ld", &aux.x, &aux.y);
a.push_back(aux);
}
sort (a.begin(), a.end(), compare);
for (i = 3; i <= K; ++i) {
if(a[i-2].x == a[i-1].x && a[i-1].x == a[i].x) {
a.erase(a.begin()+i);
--K;
}
}
scanf("%ld", &t);
while (t) {
scanf("%ld %ld %ld %ld", &st.x, &st.y, &fn.x, &fn.y);
if (st.y > fn.y) swap(st, fn);
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;
if (st.x != fn.x)
++nr;
printf("%ld\n", nr);
--t;
}
return 0;
}