Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Solution {
- Scanner in = new Scanner(new InputStreamReader(System.in));
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
- private class Point implements Comparable<Point> {
- int x;
- int y;
- int id;
- public Point(int x, int y) {
- this(x, y, 0);
- }
- public Point(int x, int y, int id) {
- this.x = x;
- this.y = y;
- this.id = id;
- }
- @Override
- public int compareTo(Point other) {
- if (x != other.x) {
- return x - other.x;
- }
- return y - other.y;
- }
- @Override
- public String toString() {
- return "Pair [id=" + id + ", x=" + x + ", y=" + y + "]";
- }
- }
- private final int MAX = 100000;
- private void solution() throws IOException {
- int n = in.nextInt();
- Point[] points = new Point[n];
- for (int i = 0; i < n; ++i) {
- points[i] = new Point(in.nextInt(), in.nextInt(), i);
- }
- int[] from = initDistances(points);
- int[] to = initDistances(inv(points));
- int maxLength = maxLength(from, to);
- int[] count = new int[MAX + 1];
- for (int i = 0; i < to.length; ++i) {
- if (from[i] + to[i] == maxLength) {
- ++count[to[i]];
- }
- }
- List<Integer> A = new ArrayList<Integer>();
- List<Integer> B = new ArrayList<Integer>();
- for (int i = 0; i < to.length; ++i) {
- if (from[i] + to[i] == maxLength) {
- A.add(i);
- if (count[to[i]] == 1) {
- B.add(i);
- }
- }
- }
- out(A);
- out(B);
- out.flush();
- }
- private void out(List<Integer> a) {
- out.print(a.size());
- for (int i = 0; i < a.size(); ++i) {
- out.print(" " + (a.get(i) + 1));
- }
- out.println();
- }
- private int[] initDistances(Point[] points) {
- Point[] p = compress(points);
- int[] res = new int[points.length];
- int size = Integer.highestOneBit(maxY(p)) * 2;
- int[] max = new int[size << 1];
- int i = p.length - 1;
- while (i >= 0) {
- List<Point> c = new ArrayList<Point>();
- c.add(p[i--]);
- while (i >= 0 && p[i].x == c.get(c.size() - 1).x) {
- c.add(p[i--]);
- }
- for (Point next : c) {
- res[next.id] = get(next.y + 1, size - 1, max) + 1;
- }
- for (Point next : c) {
- update(next.y, res[next.id], max);
- }
- }
- return res;
- }
- private void update(int i, int dx, int[] max) {
- i += max.length >> 1;
- while (i > 0) {
- max[i] = Math.max(max[i], dx);
- i >>= 1;
- }
- }
- private int get(int i, int j, int[] max) {
- i += max.length >> 1;
- j += max.length >> 1;
- int res = Integer.MIN_VALUE;
- while (i <= j) {
- if ((i & 1) == 1) {
- res = Math.max(res, max[i++]);
- }
- if ((j & 1) == 0) {
- res = Math.max(res, max[j--]);
- }
- i >>= 1;
- j >>= 1;
- }
- return res;
- }
- private int maxY(Point[] p) {
- int max = Integer.MIN_VALUE;
- for (int i = 0; i < p.length; ++i) {
- max = Math.max(max, p[i].y);
- }
- return max;
- }
- private Point[] compress(Point[] points) {
- return res;
- }
- private int maxLength(int[] from, int[] to) {
- int max = 0;
- for (int i = 0; i < from.length; ++i) {
- if (from[i] + to[i] > max) {
- max = from[i] + to[i];
- }
- }
- return max;
- }
- private Point[] inv(Point[] points) {
- for (Point p : points) {
- p.x = -p.x;
- p.y = -p.y;
- }
- return points;
- }
- private class Scanner {
- BufferedReader reader;
- StringTokenizer tokenizer;
- public Scanner(Reader reader) {
- this.reader = new BufferedReader(reader);
- tokenizer = new StringTokenizer("");
- }
- public boolean hasNext() throws IOException {
- while (!tokenizer.hasMoreTokens()) {
- String line = reader.readLine();
- if (line == null) {
- return false;
- }
- tokenizer = new StringTokenizer(line);
- }
- return true;
- }
- public String nextLine() throws IOException {
- tokenizer = new StringTokenizer("");
- return reader.readLine();
- }
- public String next() throws IOException {
- hasNext();
- return tokenizer.nextToken();
- }
- public int nextInt() throws IOException, NumberFormatException {
- return Integer.parseInt(next());
- }
- }
- public static void main(String[] args) throws IOException {
- new Solution().solution();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement