Pagini recente » Cod sursa (job #1293316) | Cod sursa (job #1346706) | Cod sursa (job #1973048) | Cod sursa (job #151336) | Cod sursa (job #208531)
Cod sursa(job #208531)
//Salut, vad ca chiar.. te intereseaza sursa mea..
//aha, lasa ca-i bine :) numa'bine!
//vladut
#include <cstdio>
#include <algorithm>
#include <vector>
#define ADD(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define ALL(a) a.begin(), a.end()
#define pii pair<int, int>
#define nmax 100001
using namespace std;
int C, N, D[nmax];
vector<pair<int, int> > V;
inline void read_in(), cal_dist();
inline int solve();
int main() {
read_in();
cal_dist();
int M;
scanf("%d", &M);
while (M--) {printf("%d\n", solve()+1);}
return 0;
}
int cal_dist(pii a, pii b) {return abs(a.first-b.first) + abs(a.second-b.second);}
int solve() {
vector<pii > P(2);
scanf("%d%d%d%d", &P[0].second, &P[0].first, &P[1].second, &P[1].first);
sort(ALL(P));
//calc distantza
int p1, p2;
p1 = lower_bound(ALL(V), P[0])-V.begin();//leftmost
p2 = lower_bound(ALL(V), P[1])-V.begin();//rightmost
if (p2 < V.size() && V[p2] == P[1]) p2++;
p2--;
if (p1 < 0 || p2 >= V.size()) return cal_dist(P[0], P[1]);
return cal_dist(P[0], V[p1]) + D[p2+1] - D[p1] + cal_dist(V[p2], P[1]);
}
void cal_dist() {for (int i = 2; i <= N; ++i) D[i] = D[i-1] + cal_dist(V[i-1], V[i-2]);}
void read_in() {
freopen("gropi.in", "r", stdin);
freopen("gropi.out", "w", stdout);
scanf("%d%d", &C, &N);
for (int i = 0; i < N; ++i) {
int a, b; scanf("%d%d", &a, &b);
V.ADD(MP(b, a%2+1));
}
sort(ALL(V));
}