Cod sursa(job #2097447)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 31 decembrie 2017 14:01:29
Problema Gropi Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <bits/stdc++.h>

using namespace std;

const int INF = (int) 2e9 + 5;
const int MAXN = (int) 1e5;

vector < pair <int, int> > coord;

vector < pair <int, int> > :: iterator it;

struct Data {
    int a, b;
    char ind;
};

vector < Data > v;

inline int check(int val, int sz) {
    int rez = -1;
    for(int pas = 1 << 16; pas; pas >>= 1)
        if(rez + pas < sz && v[rez + pas].b < val)
            rez += pas;
    return rez + 1;
}

int main() {
    FILE *fi, *fout;
    int i, j, n, l, c, m;
    fi = fopen("gropi.in" ,"r");
    fout = fopen("gropi.out" ,"w");
    fscanf(fi,"%d %d " ,&c,&n);
    for(i = 1; i <= n; i++) {
        fscanf(fi,"%d %d " ,&l,&c);
        coord.push_back({c, l});
    }
    sort(coord.begin(), coord.end());
    coord.push_back({INF, coord.back().second});
    i = 0;
    while(i <= n) {
        j = i;
        while(j <= n && coord[i].second == coord[j].second) {
            j++;
        }
        v.push_back({coord[i].first, coord[j - 1].first, coord[i].second});
        i = j;
    }
    int sz = v.size();
    fscanf(fi,"%d " ,&m);
    while(m > 0) {
        m--;
        int l1, c1, l2, c2;
        fscanf(fi,"%d %d %d %d " ,&l1,&c1,&l2,&c2);
        if(c1 > c2) {
            swap(l1, l2);
            swap(c1, c2);
        }
        int ans = c2 - c1 + 1;
        int a = check(c1, sz), b = check(c2, sz);
        ans += b - a;
        if(a == b) {
            if(l1 != l2)
                ans++;
            else if(l1 == v[a].ind) {
                int rez = -1;
                for(int pas = 1 << 16; pas; pas >>= 1) {
                   if(rez + pas <= n && coord[rez + pas].first <= c1)
                      rez += pas;
                }
                rez++;
                if(coord[rez].first < c2)
                    ans += 2;
            }
        }
        else {
            if(l1 == v[a].ind && l2 == v[b].ind && c2 > v[b].a)
                ans += 2;
            if(l1 == v[a].ind && l2 != v[b].ind)
                ans++;
            if(l1 != v[a].ind && l2 == v[b].ind && c2 <= v[b].a)
                ans--;
        }
        fprintf(fout,"%d\n" ,ans);
    }
    fclose(fi);
    fclose(fout);
    return 0;
}