Cod sursa(job #208545)

Utilizator vlad_DVlad Dumitriu vlad_D Data 17 septembrie 2008 09:21:34
Problema Gropi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
//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));
     int p1, p2;
     p1 = 0; p2 = V.size()-1;     
     p1 = lower_bound(ALL(V), P[0]) - V.begin();
     p2 = upper_bound(ALL(V), P[1]) - V.begin();
     p2--;  
     if (p1 > p2) return cal_dist(P[0], P[1]);
     return cal_dist(P[0], V[p1]) + D[p2+1] - D[p1+1] + cal_dist(V[p2], P[1]);
     
     }
void cal_dist() {for (int i = 2; i <= V.size(); ++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));}
    V.ADD(MP(0, 0));
    V.ADD(MP(C+1, 0));
    sort(ALL(V));
    }