Cod sursa(job #2423535)

Utilizator lucametehauDart Monkey lucametehau Data 21 mai 2019 17:37:39
Problema Gropi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>
#include <algorithm>
#define x first
#define y second
#define pii pair <int, int>

using namespace std;

ifstream cin ("gropi.in");
ofstream cout ("gropi.out");

int c, n, m;
pii a, b;

int changes[100005];
pii v[100005];

int main() {
  cin >> c >> n;
  for(int i = 0; i < n; i++)
    cin >> v[i].y >> v[i].x;
  v[n++] = {c + 1, 0};
  sort(v, v + n);
  changes[0] = (v[0].y != v[1].y);
  for(int i = 1; i < n - 1; i++)
    changes[i] = changes[i - 1] + (v[i].y != v[i + 1].y);
  cin >> m;
  for(; m; m--) {
    cin >> a.y >> a.x >> b.y >> b.x;
    if(a > b)
      swap(a, b);
    int poz1 = upper_bound(v, v + n, make_pair(a.x, 0)) - v;
    if(b.x <= v[poz1].x)
      cout << b.x - a.x + 1 + (a.y != b.y) << "\n";
    else {
      int poz2 = upper_bound(v, v + n, make_pair(b.x, 0)) - v - 1;
      cout << b.x - a.x + 1 + changes[poz2 - 1] - changes[poz1 - 1] + (a.y == v[poz1].y) + (b.y == v[poz2].y) << "\n";
    }
  }
  return 0;
}