Cod sursa(job #1398529)

Utilizator alex_HarryBabalau Alexandru alex_Harry Data 24 martie 2015 11:48:02
Problema Ograzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <fstream>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;
ifstream f("ograzi.in");
ofstream g("ograzi.out");
int N,M,W,H;
multiset <int> S;
const int NMax = 50005;
const int MMax = 100005;
const int SMax = 3000005;
const int P = 103;
const int MOD = 100000;
vector < pair<int,int> > Hash[MMax];
char Str[SMax];
int dx[]={0,0,-1,-1};
int dy[]={0,-1,0,-1};
void Insert(pair <int,int> Point)
{
    int List=((Point.first/W)*P+Point.second/H)%MOD;
    Hash[List].push_back(Point);
}

bool Find(pair <int,int> Point)
{
    int List=(Point.first*P+Point.second)%MOD;
    for(int i=0;i<Hash[List].size();i++)
        if(Hash[List][i]==Point)
            return 1;
    return 0;
}
inline bool Inside(pair <int,int> pointX,pair <int,int> pointY)
{
    return pointX.first<=pointY.first && pointX.first+W>=pointY.first && pointX.second<=pointY.second && pointX.second+H>=pointY.second;
}
void Process()
{
    f>>N>>M>>W>>H;
    pair <int,int> Point;
    char ch;
    f.get(ch);
    f.get(Str,SMax,EOF);
    int ind=0,x=0,y=0,after=0,result=0;
    for(int i=0;Str[i]!=0;i++)
    {
        if(Str[i]==' ')
            after=1;
        if(Str[i]=='\n')
        {
            ++ind;
            Point=make_pair(x,y);
            x=y=0;
            after=0;
            if(ind>N)
            {

                for(int j=0;j<4;j++)
                {
                    int List=((Point.first/W+dx[j])*P+(Point.second/H+dy[j]))%MOD;
                    if(Point.first/W+dx[j]<0 || Point.second/H+dy[j]<0)
                        continue;
                    for(int k=0;k<Hash[List].size();k++)
                    {
                        if(Inside(Hash[List][k],Point)==1)
                        {
                            ++result;
                            break;
                        }
                    }
                }
            }
            else
                Insert(Point);
        }
        if(Str[i]>='0' && Str[i]<='9')
        {
            if(after==1)
                y=y*10+Str[i]-'0';
            else
                x=x*10+Str[i]-'0';
        }
    }
    g<<result<<"\n";

}

int main()
{
    Process();
    return 0;
}