Cod sursa(job #184809)

Utilizator DorinOltean Dorin Dorin Data 24 aprilie 2008 12:45:53
Problema Hvrays Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
# include <cstdio>
# include <vector>
# include <fstream>

# define input "hvrays.in"
# define output "hvrays.out"

using namespace std;

struct cmp
{
    bool operator() (pair <int,int> x, pair <int,int> y)
    {
        if(x.first < y.first) return 0;
        if(x.first > y.first) return 1;
        if(x.second < y.second) return 0;
        return 1;
    }
};

vector < pair <int, int> > h,v;
int n,m,i,x,y,j;

void solve()
{
    sort(h.begin(),h.end(),cmp());
    sort(v.begin(),v.end(),cmp());
    int ret = 0;
    int hmax = -1;
    
    for(i = 0, j = 0; i < n && j < m; i++)
    {
        if(h[i].second > hmax)
        {
            ret++;
            while(j < m && h[i].first <= v[j].first) 
                hmax = hmax > v[j].second ? hmax : v[j].second,
                j++;
        }
    }
    
    printf("%d\n",ret); 
}

int main()
{
    freopen(input, "r", stdin);
    freopen(output,"w", stdout);
    int T;
    scanf("%d",&T);
    
    while(T--)
    {
        scanf("%d %d",&n,&m);
        for(i = 0; i<n; i++)
        {
            scanf("%d %d",&x,&y);
            h.push_back(make_pair(x,y));
        }
        
        for(i = 0; i<m; i++)
        {
            scanf("%d %d",&x,&y);
            v.push_back(make_pair(x,y));
        }
        solve();
        h.clear();
        v.clear();
    }
    
    return 0;
}