Cod sursa(job #961314)

Utilizator primulDarie Sergiu primul Data 11 iunie 2013 21:12:39
Problema Gropi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <cstdio>
#include <algorithm>
using namespace std;
 
#define mp make_pair
#define x first
#define y second
 
const int MAX_N = 100005;
 
int N, C;
pair<int, int> v[MAX_N];
int S[MAX_N];
 
int main() {
    freopen("gropi.in", "r", stdin);
    freopen("gropi.out", "w", stdout);
 
    scanf("%d %d", &C, &N);
    for (int i = 0; i < N; ++i)
        scanf("%d %d", &v[i].y, &v[i].x);
    //v[N++] = mp(0, 0);
    v[N++] = mp(C+1, 0);
    sort(v, v+N);
 
    S[0] = v[0].y != v[1].y;
    for (int i = 1; i + 1 < N; ++i)
        S[i] = S[i-1] + (v[i].y != v[i+1].y);
 
    int M;
    for (scanf("%d", &M); M; --M) {
        pair<int, int> A, B;
        scanf("%d %d %d %d", &A.y, &A.x, &B.y, &B.x);
        if (B < A) swap(A, B);
 
        int p1 = upper_bound(v,v+N,mp(A.x,0)) - v;
        if (v[p1].x >= B.x)
            printf("%d\n", B.x-A.x + 1 + (A.y != B.y));
        else {
            int p2 = upper_bound(v,v+N,mp(B.x,0)) - v - 1;
            int ret = S[p2-1] - S[p1-1];
            if (A.y == v[p1].y) ++ret;
            if (B.y == v[p2].y) ++ret;
            ret += B.x-A.x+1;
            printf("%d\n", ret);
        }
    }
}