Cod sursa(job #1758834)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 17 septembrie 2016 22:47:42
Problema Hvrays Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

ifstream cin("hvrays.in");
ofstream cout("hvrays.out");

const int MAXN = 100000;

struct Point {
    int x;
    int y;

    bool operator < (const Point &other) const {
        if (x == other.x)
            return y < other.y;
        return x < other.x;
    }
};

Point horizontal[1 + MAXN], vertical[1 + MAXN];

int main() {
    int tests;
    cin >> tests;
    for (int test = 1; test <= tests; test++) {
        int n, m;
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
            cin >> horizontal[i].x >> horizontal[i].y;
        for (int i = 1; i <= m; i++)
            cin >> vertical[i].x >> vertical[i].y;
        sort(horizontal + 1, horizontal + n + 1);
        sort(vertical + 1, vertical + m + 1);
        int answer = 0, limit = -1;
        int i = n, j = m;
        while (i) {
            if (horizontal[i].y > limit) {
                answer++;
                while (j && vertical[j].x >= horizontal[i].x) {
                    limit = max(limit, vertical[j].y);
                    j--;
                }
            }
            i--;
        }
        cout << answer << "\n";
    }
    return 0;
}