Cod sursa(job #2338394)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 7 februarie 2019 13:44:32
Problema Ograzi Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
#include<bits/stdc++.h>
using namespace std;
ifstream f("ograzi.in");
ofstream g("ograzi.out");
int n, m, w, h;
struct puncte
{
    int x, y;
};
puncte rect[50002], points[100002];
bool cmp(puncte a, puncte b)
{
    if(a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}
int st, dr, b;
int ans = 0;
multiset<int>ms;
multiset<int> ::iterator it;
int sm(puncte a, puncte b)
{
    if(a.x < b.x)
        return 1;
    if(a.x > b.x)
        return 2;
    if(a.y <= b.y)
        return 1;
    return 2;
}
int main()
{
    f >> n >> m >> w >> h;
    for(int i = 1; i <= n; ++i)
        f >> rect[i].x >> rect[i].y;
    for(int i = 1; i <= m; ++i)
        f >> points[i].x >> points[i].y;
    sort(rect + 1, rect + n + 1, cmp);
    sort(points + 1, points + m + 1, cmp);
    rect[n+1] = {1000000000, 1000000000};
    points[m+1] = {1000000000, 1000000000};
    st = 1, dr = 1, b = 1;
    while(dr <= n || b <= m)
    {
        int rez = sm(rect[dr], points[b]);
        puncte x;
        if(rez == 1)
            x = rect[dr];
        else
            x = points[b];
        while(x.x - rect[st].x > h)
        {
            it = ms.lower_bound(rect[st].y);
            ms.erase(it);
            ++st;
        }
        if(rez == 1)
            ms.insert(rect[dr].y), ++dr;
        else
        {
            if(!ms.empty())
            {
                if(*ms.begin() > x.y);
                else
                {
                    it = ms.upper_bound(x.y);
                    --it;
                    if(x.y - *it <= w)
                        ++ans;
                }
            }
            ++b;
        }
    }
    g << ans;
    return 0;
}