Pagini recente » Cod sursa (job #2090846) | Cod sursa (job #172645) | Cod sursa (job #2217096) | Cod sursa (job #1159635) | Cod sursa (job #216864)
Cod sursa(job #216864)
// Gropi.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include "fstream"
#include "iostream"
#include "algorithm"
#include "vector"
using namespace std;
int cmp(pair<int, bool> a, pair<int, bool> b)
{
return a.first > b.first;
}
int cauta1(int X, vector<pair<int, bool> > * const gropi)
{
int st = 0, dr = gropi->size() - 1, mij;
while(st < dr)
{
mij = (st + dr) / 2;
if(gropi->at(mij).first <= X)
st = mij + 1;
else
dr = mij;
}
return st;
}
int cauta2(int X, vector<pair<int, bool> > * const gropi)
{
int st = 0, dr = gropi->size() - 1, mij;
while(st < dr)
{
mij = (st + dr + 1) / 2;
if(gropi->at(mij).first <= X)
st = mij;
else
dr = mij - 1;
}
return st;
}
int nrGropi(int x, int y, vector<pair<int, bool> > * const gropi, bool pozInit, bool pozFin)
{
int i, nr = 0;
for(i = x; i <= y; i++)
if(gropi->at(i).second == pozInit)
{
nr++;
pozInit = !pozInit;
}
if(pozInit != pozFin)
nr++;
return nr;
}
int main(void)
{
fstream in, out;
in.open("gropi.in", ios_base::in);
if(!in.is_open())
{
cout << "Eroare la deschiderea fisierului de intrare!";
return 1;
}
out.open("gropi.out", ios_base::out);
if(!out.is_open())
{
cout << "Eroare la deschiderea fisierului de iesire!";
return 1;
}
int C, N, M, i;
int x, y;
vector<pair<int, bool> > gropi;
pair<int, bool> A, B;
in >> C >> N;
for(i = 0; i < N; i++)
{
in >> x >> y;
if(x == 1)
gropi.push_back(make_pair(y, false));
else
gropi.push_back(make_pair(y, true));
}
std::sort(gropi.begin(), gropi.end(), cmp);
/*for(i = 0; i < N; i++)
cout << gropi.at(i).first << endl;*/
in >> M;
while(M--)
{
in >> x >> y;
if(x == 1)
A = make_pair(y, false);
else
A = make_pair(y, true);
in >> x >> y;
if(x == 1)
B = make_pair(y, false);
else
B = make_pair(y, true);
if(cmp(A, B))
swap(A, B);
x = cauta1(A.first, &gropi);
y = cauta2(B.first, &gropi);
int aa = nrGropi(x, y, &gropi, A.second, B.second);
int rez = aa + B.first - A.first + 1;
/*cout << "X = " << x << " Y = " << y << " Nr gropi: " << rez << endl;*/
out << rez << endl;
}
in.close();
out.close();
system("PAUSE");
return 0;
}