Cod sursa(job #686691)

Utilizator dushmiMihai-Alexandru Dusmanu dushmi Data 21 februarie 2012 19:42:28
Problema Zoo Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.96 kb
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 16500, M = 100500, INF = 2000000500;

struct point {
  int x, y;
};

struct rectangle {
  int x1, y1, x2, y2;
};

rectangle q[M];

point v[N], t[N];

int n, m, newm, ans[4 * M], vy[N], cory[N], aib[N];

void read() {
  assert(scanf("%d", &n));

  for (int i = 1; i <= n; ++i)
    assert(scanf("%d%d", &v[i].x, &v[i].y));

  assert(scanf("%d", &m));

  for (int i = 1; i <= m; ++i)
    assert(scanf("%d%d%d%d", &q[i].x1, &q[i].y1, &q[i].x2, &q[i].y2));
}

int cautb(int x, int *v, bool type) {
  int i = 0, pas = 1 << 18;

  for (i = 0; pas; pas >>= 1)
    if (i + pas <= v[0] && v[i + pas] < x)
      i += pas;

  if (type == 0)
    return i;
  
  if (type == 1) {
    if (v[i + 1] == x)
      return i + 1;

    return i;
  }

  return 0;  
}

void normalization() {
  vy[++vy[0]] = -INF;

  for (int i = 1; i <= n; ++i)
    vy[++vy[0]] = v[i].y;

  vy[++vy[0]] = INF;

  sort(vy + 1, vy + vy[0] + 1);

  for (int i = 1; i <= vy[0]; ++i) {
    cory[i] = cory[i - 1];

    if (vy[i] != vy[i - 1])
      ++cory[i];
  }

  for (int i = 1; i <= n; ++i)
    v[i].y = cory[cautb(v[i].y, vy, 1)];

  for (int i = 1; i <= m; ++i) {
    q[i].y1 = cory[cautb(q[i].y1, vy, 0)];
    q[i].y2 = cory[cautb(q[i].y2, vy, 1)];
  }
}

bool comp(point A, point B) {
  if (A.x < B.x)
    return 1;

  if (A.x > B.x)
    return 0;

  return A.y < B.y;
}

void init() {
  sort(v + 1, v + n + 1, comp);

  newm = 0;

  for (int i = 1; i <= m; ++i) {
    ++newm;
    t[newm].x = q[i].x2;
    t[newm].y = q[i].y2;

    ++newm;
    t[newm].x = q[i].x2;
    t[newm].y = q[i].y1;

    ++newm;
    t[newm].x = q[i].x1 - 1;
    t[newm].y = q[i].y2;

    ++newm;
    t[newm].x = q[i].x1 - 1;
    t[newm].y = q[i].y1;
  }

  sort(t + 1, t + newm + 1, comp);
}

inline int bit(int x) {
  return x & (-x);
}

inline int query(int poz) {
  int rez = 0;

  while (poz) {
    rez += aib[poz];
    poz -= bit(poz);
  }

  return rez;
}

inline void insert(int poz) {
  while (poz < N) {
    ++aib[poz];
    poz += bit(poz);
  }
}

int cb(int x, int y) {
  int pas = 1 << 18, i = 0;

  for (i = 0; pas; pas >>= 1)
    if (i + pas <= newm && (t[i + pas].x < x || (t[i + pas].x == x && t[i + pas].y < y)))
      i += pas;

  return i + 1;
}

void solve() {
  for (int i = 1, j = 1; i <= n || j <= newm;)
    if (i > n) {
      ans[j] = query(t[j].y);
      ++j;
    } else if (j > newm) {
      insert(v[i].y);
      ++i;
    } else if (v[i].x <= t[j].x) {
      insert(v[i].y);
      ++i;
    } else {
      ans[j] = query(t[j].y);
      ++j;
    }

  for (int i = 1; i <= m; ++i) {
      int r = ans[cb(q[i].x2, q[i].y2)];
      r -= ans[cb(q[i].x2, q[i].y1 - 1)];
      r -= ans[cb(q[i].x1 - 1, q[i].y2)];
      r += ans[cb(q[i].x1 - 1, q[i].y1 - 1)];
      printf("%d\n", r);
  }
}

int main() {
  assert(freopen("zoo.in", "r", stdin));
  assert(freopen("zoo.out", "w", stdout));

  read();

  normalization();

  init();

  solve();

  return 0;
}