Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- #define fs first
- #define sc second
- #define mp make_pair
- using namespace std;
- typedef pair<int, int> ii;
- const int N = 100001;
- const int inf = (int) 2e9;
- struct point {
- int x, y, i;
- bool friend operator< (point a, point b) {
- if (a.x == b.x) {
- return a.y < b.y;
- } else {
- return a.x < b.x;
- }
- }
- };
- int n;
- int cl[N];
- int cld[N];
- int ans[N];
- point a[N];
- ii tp[4 * N];
- ii c (ii a, ii b) {
- if (a.fs == b.fs) {
- if (a.sc < b.sc) {
- return a;
- } else {
- return b;
- }
- } else {
- if (a.fs > b.fs) {
- return a;
- } else {
- return b;
- }
- }
- }
- void build (int v, int tl, int tr) {
- if (tl == tr) {
- tp[v].fs = -inf;
- tp[v].sc = inf;
- } else {
- int tm = (tl + tr) >> 1;
- build (v + v, tl, tm);
- build (v + v + 1, tm + 1, tr);
- tp[v] = c (tp[v + v], tp[v + v + 1]);
- }
- }
- void up (int v, int tl, int tr, int y, int x, int i) {
- if (tl == tr) {
- if (tp[v].fs < x + y) {
- tp[v].fs = x + y;
- tp[v].sc = i;
- }
- } else {
- int tm = (tl + tr) >> 1;
- if (y <= tm) {
- up (v + v, tl, tm, y, x, i);
- } else {
- up (v + v + 1, tm + 1, tr, y, x, i);
- }
- tp[v] = c (tp[v + v], tp[v + v + 1]);
- }
- }
- ii gp (int v, int tl, int tr, int l, int r) {
- if (l == tl && r == tr) {
- return tp[v];
- } else {
- int tm = (tl + tr) >> 1;
- if (r <= tm) {
- return gp (v + v, tl, tm, l, r);
- } else if (l > tm) {
- return gp (v + v + 1, tm + 1, tr, l, r);
- } else {
- return c (gp (v + v, tl, tm, l, tm), gp (v + v + 1, tm + 1, tr, tm + 1, r));
- }
- }
- }
- void solve () {
- sort (a, a + n);
- build (1, -10000, 10000);
- for (int i = 0; i < n; i++) {
- if (i) {
- ii below = gp (1, -10000, 10000, -10000, a[i].y);
- int p = (a[i].x + a[i].y) - below.fs;
- if (cld[a[i].i] > p || (cld[a[i].i] == p && cl[a[i].i] > below.sc)) {
- cld[a[i].i] = p;
- cl[a[i].i] = below.sc;
- }
- }
- up (1, -10000, 10000, a[i].y, a[i].x, a[i].i);
- }
- }
- void rotat () {
- for (int i = 0; i < n; i++) {
- swap (a[i].x, a[i].y);
- a[i].x = -a[i].x;
- }
- }
- int main () {
- scanf ("%d", &n);
- for (int i = 0; i < n; i++) {
- cl[i] = -1;
- cld[i] = inf;
- }
- for (int i = 0; i < n; i++) {
- scanf ("%d %d", &a[i].x, &a[i].y);
- a[i].i = i;
- }
- solve ();
- for (int i = 0; i < 3; i++) {
- rotat ();
- solve ();
- }
- for (int i = 0; i < n; i++) {
- printf ("%d%c", 1 + cl[i], " \n"[i + 1 == n]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment