Pagini recente » Cod sursa (job #788093) | Cod sursa (job #2362373) | Cod sursa (job #2361208) | Cod sursa (job #1263875) | Cod sursa (job #198688)
Cod sursa(job #198688)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
class column {
public:
int x;
bool a, b;
bool operator < (const column &B) const {
const column &A = *this;
if(A.x != B.x)
return A.x < B.x;
return false;
}
};
ifstream in("gropi.in");
ofstream out("gropi.out");
int C, N;
vector <column> V;
vector <long long> dist;
void init() {
in >> C >> N;
V.resize(N);
dist.resize(N);
for(int i = 0, y; i < N; ++i) {
in >> y >> V[i].x;
if(y == 1)
V[i].a = false, V[i].b = true;
else
V[i].a = true, V[i].b = false;
}
sort(V.begin(), V.end());
dist[0] = 0;
for(int i = 1; i < N; ++i)
dist[i] = dist[i - 1] + (V[i].x - V[i - 1].x) + (V[i].a != V[i - 1].a);
}
int binary_search_after(const int &lo, const int &hi, const int &value) {
if(lo == hi)
return lo;
if(V[(lo + hi) / 2].x >= value)
return binary_search_after(lo, (lo + hi) / 2, value);
else
return binary_search_after((lo + hi) / 2 + 1, hi, value);
}
int binary_search_before(const int &lo, const int &hi, const int &value) {
if(lo == hi)
return lo;
if(V[(lo + hi) / 2 + 1].x <= value)
return binary_search_before((lo + hi) / 2 + 1, hi, value);
else
return binary_search_before(lo, (lo + hi) / 2, value);
}
int main() {
init();
int T, x1, y1, x2, y2, p1, p2;
for(in >> T; T--; ) {
in >> y1 >> x1 >> y2 >> x2;
if(x1 > x2)
swap(x1, x2), swap(y1, y2);
if(x1 >= V.back().x || x2 <= V.front().x) {
out << x2 - x1 + (y1 != y2) + 1 << "\n";
continue;
}
p1 = binary_search_after(0, N - 1, x1);
p2 = binary_search_before(0, N - 1, x2);
out << (unsigned int)(1 + (dist[p2] - dist[p1]) +
(V[p1].x - x1) + ((V[p1].a == true ? 1 : 2) != y1) +
(x2 - V[p2].x) + ((V[p2].a == true ? 1 : 2) != y2))
<< "\n";
}
return 0;
}