Advertisement
Lesnic

DSA 4

Mar 27th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.17 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.util.Arrays;
  4.  
  5. //Gorozhankin Egor BS19-01
  6. // http://codeforces.com/group/3ZU2JJw8vQ/contest/272963/submission/74514355
  7.  
  8. public class Sweep_line {
  9.  
  10.     static Boolean finished = false;
  11.     static Node<Segment> right = null, left = null;
  12.  
  13.     public static void main(String[] args) {
  14.         Scan scanner = new Scan(System.in);
  15.         int n = (int) scanner.nextLong();
  16.  
  17.         ControlPoint[] controlPoints = new ControlPoint[n * 2];
  18.  
  19.         AVL_Tree<Segment> temp = new AVL_Tree<Segment>();
  20.         Segment[] segments = new Segment[n];
  21.        
  22.         for (int i = 0; i < n; i++) {
  23.             segments[i] = new Segment(scanner.nextLong(), scanner.nextLong(), scanner.nextLong(), scanner.nextLong());
  24.             if (segments[i].beginX < segments[i].endX) {
  25.                 controlPoints[2 * i] = new ControlPoint(segments[i].beginX, true, segments[i]);
  26.                 controlPoints[2 * i + 1] = new ControlPoint(segments[i].endX, false, segments[i]);
  27.             } else {
  28.                 controlPoints[2 * i] = new ControlPoint(segments[i].endX, true, segments[i]);
  29.                 controlPoints[2 * i + 1] = new ControlPoint(segments[i].beginX, false, segments[i]);
  30.             }
  31.         }
  32.        
  33.         controlPoints = MergeSort.mergeSort(controlPoints);
  34.        
  35.         int count = 0;
  36.         Segment res = null;
  37.  
  38.         for (int i = 0; i < controlPoints.length && count < n; i++) {
  39.             if (!controlPoints[i].begin) {
  40.                 if (temp.remove(controlPoints[i].key)) {
  41.                     finished = true;
  42.                     System.out.println("INTERSECTION");
  43.                     System.out.println(Sweep_line.left.key);
  44.                     System.out.println(Sweep_line.right.key);
  45.                     break;
  46.                 }
  47.             } else {
  48.                 ++count;
  49.                 res = temp.add(controlPoints[i].key, true);
  50.  
  51.                 if (res != null) {
  52.                     finished = true;
  53.                     System.out.println("INTERSECTION");
  54.                     System.out.println(res);
  55.                     System.out.println(controlPoints[i].key);
  56.                     break;
  57.                 }
  58.             }
  59.         }
  60.         if (!finished)
  61.             System.out.println("NO INTERSECTIONS");
  62.     }
  63.  
  64.     static public <T> boolean checkIntersection(T aT, T bT) {
  65.         if (aT == null || bT == null)
  66.             return false;
  67.         Segment b = (Segment) aT, a = (Segment) bT;
  68.  
  69.         double denominator = (a.beginX - a.endX) * (b.beginY - b.endY) - (a.beginY - a.endY) * (b.beginX - b.endX);
  70.         if (denominator == 0)
  71.             return false;
  72.         double x = (a.beginX * a.endY - a.beginY * a.endX) * (b.beginX - b.endX)
  73.                 - (b.beginX * b.endY - b.beginY * b.endX) * (a.beginX - a.endX);
  74.         double y = (a.beginX * a.endY - a.beginY * a.endX) * (b.beginY - b.endY)
  75.                 - (b.beginX * b.endY - b.beginY * b.endX) * (a.beginY - a.endY);
  76.         x /= denominator;
  77.         y /= denominator;
  78.  
  79.         boolean checkArea = ((x > a.beginX && x < a.endX) || (x < a.beginX && x > a.endX))
  80.                 && ((y > a.beginY && y < a.endY) || (y < a.beginY && y > a.endY))
  81.                 && ((x > b.beginX && x < b.endX) || (x < b.beginX && x > b.endX))
  82.                 && ((y > b.beginY && y < b.endY) || (y < b.beginY && y > b.endY));
  83.         return checkArea;
  84.     }
  85. }
  86.  
  87. class ControlPoint implements Comparable<ControlPoint> {
  88.     public Long x;
  89.     public Boolean begin;
  90.     public Segment key;
  91.  
  92.     public ControlPoint(Long x, boolean begin, Segment key) {
  93.         this.x = x;
  94.         this.begin = begin;
  95.         this.key = key;
  96.     }
  97.  
  98.     @Override
  99.     public int compareTo(ControlPoint o) {
  100.         if (x.equals(o.x))
  101.             if (this.begin)
  102.                 return -1;
  103.             else if (o.begin)
  104.                 return 1;
  105.         return x.compareTo(o.x);
  106.     }
  107. }
  108.  
  109. class Segment implements Comparable<Segment> {
  110.     public Long beginX, beginY, endX, endY;
  111.  
  112.     public Segment(Long xBegin, Long yBegin, Long xEnd, Long yEnd) {
  113.         this.beginX = xBegin;
  114.         this.beginY = yBegin;
  115.         this.endX = xEnd;
  116.         this.endY = yEnd;
  117.     }
  118.  
  119.     public String toString() {
  120.         return Long.toString(beginX) + " " + Long.toString(beginY) + " " + Long.toString(endX) + " "
  121.                 + Long.toString(endY);
  122.     }
  123.  
  124.     public boolean isEqual(Segment o) {
  125.         return ((beginX.equals(o.beginX) && endX.equals(o.endX)) || (beginX.equals(o.endX) && endX.equals(o.beginX)))
  126.                 && ((beginX.equals(o.beginX) && endX.equals(o.endX))
  127.                         || (beginX.equals(o.endX) && endX.equals(o.beginX)));
  128.     }
  129.  
  130.     @Override
  131.     public int compareTo(Segment o) {
  132.         return beginY.compareTo(o.beginY);
  133.     }
  134. }
  135.  
  136. class MergeSort<T> implements Comparable<T> {
  137.     public static <T> T[] mergeSort(T[] array) {
  138.         return mergeSort(array, 0, array.length - 1);
  139.     }
  140.    
  141.     @SuppressWarnings("unchecked")
  142.     public static <T> T[] mergeSort(T[] array, int left, int right) {
  143.         if (left == right)
  144.             return array;
  145.  
  146.         int mid = (left + right) / 2;
  147.         mergeSort(array, left, mid);
  148.         mergeSort(array, mid + 1, right);
  149.  
  150.         int i = left, j = mid + 1;
  151.         T[] temp = Arrays.copyOf(array, right - left + 1);
  152.  
  153.         for (int k = 0; k < temp.length; k++) {
  154.             if (i > mid)
  155.                 temp[k] = array[j++];
  156.             else if (j > right)
  157.                 temp[k] = array[i++];
  158.             else if (((Comparable<T>) array[i]).compareTo(array[j]) > 0)
  159.                 temp[k] = array[j++];
  160.             else
  161.                 temp[k] = array[i++];
  162.         }
  163.  
  164.         for (int k = 0; k < temp.length; k++)
  165.             array[k + left] = temp[k];
  166.  
  167.         return array;
  168.     }
  169.  
  170.     @Override
  171.     public int compareTo(T o) {
  172.         return this.compareTo(o);
  173.     }
  174. }
  175.  
  176. class AVL_Tree<T> implements Comparable<T> {
  177.     private Node<T> top;
  178.  
  179.     public static int nodeHeight(Node<?> node) {
  180.         if (node == null)
  181.             return 0;
  182.         return node.height;
  183.     }
  184.  
  185.     public static int balanceFactor(Node<?> node) {
  186.         if (node == null)
  187.             return 0;
  188.         return nodeHeight(node.left) - nodeHeight(node.right);
  189.     }
  190.  
  191.     public static void calculateNodeHeight(Node<?> node) {
  192.         if (nodeHeight(node.left) > nodeHeight(node.right))
  193.             node.height = nodeHeight(node.left) + 1;
  194.         else
  195.             node.height = nodeHeight(node.right) + 1;
  196.     }
  197.  
  198.     public void add(T key) {
  199.         top = add(top, key);
  200.     }
  201.  
  202.     public Segment add(T key, boolean find) {
  203.         Sweep_line.right = null;
  204.         Sweep_line.left = null;
  205.         add(key);
  206.         if (Sweep_line.right != null && Sweep_line.checkIntersection(key, Sweep_line.right.key))
  207.             return Sweep_line.right.key;
  208.         if (Sweep_line.left != null && Sweep_line.checkIntersection(key, Sweep_line.left.key))
  209.             return Sweep_line.left.key;
  210.         return null;
  211.     }
  212.  
  213.     @SuppressWarnings("unchecked")
  214.     private Node<T> add(Node<T> node, T key) {
  215.         if (node == null) {
  216.             return new Node<T>(key);
  217.         }
  218.  
  219.         if (((Comparable<T>) key).compareTo(node.key) < 0) {
  220.             Sweep_line.right = (Node<Segment>) node;
  221.             node.left = add(node.left, key);
  222.         } else {
  223.             Sweep_line.left = (Node<Segment>) node;
  224.             node.right = add(node.right, key);
  225.         }
  226.         node.height = 1
  227.                 + (nodeHeight(node.left) > nodeHeight(node.right) ? nodeHeight(node.left) : nodeHeight(node.right));
  228.  
  229.         return nodeBalancing(node);
  230.     }
  231.  
  232.     public boolean remove(T key) {
  233.         Sweep_line.left = null;
  234.         Sweep_line.right = null;
  235.         top = remove(top, key);
  236.         if (Sweep_line.right != null && Sweep_line.left != null
  237.                 && Sweep_line.checkIntersection(Sweep_line.left.key, Sweep_line.right.key))
  238.             return true;
  239.         return false;
  240.     }
  241.  
  242.     @SuppressWarnings("unchecked")
  243.     private Node<T> remove(Node<T> node, T key) {
  244.         if (node == null)
  245.             return node;
  246.  
  247.         if (((Comparable<T>) key).compareTo(node.key) < 0) {
  248.             Sweep_line.right = (Node<Segment>) node;
  249.             node.left = remove(node.left, key);
  250.         } else if (((Comparable<T>) key).compareTo(node.key) > 0) {
  251.             Sweep_line.left = (Node<Segment>) node;
  252.             node.right = remove(node.right, key);
  253.         } else {
  254.             Node<T> tempLeft = node.left, tempRight = node.right;
  255.             while (tempLeft != null && tempLeft.right != null)
  256.                 tempLeft = tempLeft.right;
  257.             while (tempRight != null && tempRight.left != null)
  258.                 tempRight = tempRight.left;
  259.  
  260.             if (tempLeft != null)
  261.                 Sweep_line.left = (Node<Segment>) tempLeft;
  262.             if (tempRight != null)
  263.                 Sweep_line.right = (Node<Segment>) tempRight;
  264.  
  265.             if (node.left == null && node.right == null)
  266.                 return null;
  267.  
  268.             if (node.left == null) {
  269.                 Node<T> temp = node.right;
  270.                 node = null;
  271.                 return temp;
  272.             }
  273.  
  274.             if (node.right == null) {
  275.                 Node<T> temp = node.left;
  276.                 node = null;
  277.                 return temp;
  278.             }
  279.  
  280.             Node<T> temp = node.left;
  281.             while (temp != null && temp.right != null)
  282.                 temp = temp.right;
  283.             node.key = temp.key;
  284.             node.left = remove(node.left, temp.key);
  285.         }
  286.  
  287.         calculateNodeHeight(node);
  288.         return nodeBalancing(node);
  289.     }
  290.  
  291.     private Node<T> nodeBalancing(Node<T> node) {
  292.         int balance = balanceFactor(node);
  293.  
  294.         if (balance > 1) {
  295.             if (balanceFactor(node.left) < 0) {
  296.                 node.left = leftRotation(node.left);
  297.             }
  298.             return rightRotation(node);
  299.         }
  300.  
  301.         if (balance < -1) {
  302.             if (balanceFactor(node.right) > 0) {
  303.                 node.right = rightRotation(node.right);
  304.             }
  305.             return leftRotation(node);
  306.         }
  307.  
  308.         return node;
  309.     }
  310.  
  311.     private Node<T> rightRotation(Node<T> node) {
  312.         Node<T> temp = node.left;
  313.         node.left = temp.right;
  314.         temp.right = node;
  315.         calculateNodeHeight(node);
  316.         calculateNodeHeight(temp);
  317.         return temp;
  318.     }
  319.  
  320.     private Node<T> leftRotation(Node<T> node) {
  321.         Node<T> temp = node.right;
  322.         node.right = temp.left;
  323.         temp.left = node;
  324.         calculateNodeHeight(node);
  325.         calculateNodeHeight(temp);
  326.         return temp;
  327.     }
  328.  
  329.     @Override
  330.     public int compareTo(T o) {
  331.         return this.compareTo(o);
  332.     }
  333. }
  334.  
  335. class Node<T> implements Comparable<T> {
  336.     public T key;
  337.     public Node<T> left, right;
  338.     public int height;
  339.  
  340.     Node(T key) {
  341.         this.key = key;
  342.         left = null;
  343.         right = null;
  344.         height = 1;
  345.     }
  346.  
  347.     Node(Node<T> node) {
  348.         this.key = node.key;
  349.         this.left = node.left;
  350.         this.right = node.right;
  351.     }
  352.  
  353.     @Override
  354.     public int compareTo(T o) {
  355.         this.compareTo(o);
  356.         return 0;
  357.     }
  358. }
  359.  
  360. class Scan {
  361.     InputStream in;
  362.     char c;
  363.     Scan(InputStream in) {
  364.         this.in = in;
  365.         nextChar();
  366.     }
  367.  
  368.     void asserT(boolean e) {
  369.        if (!e) {
  370.           throw new Error();
  371.        }
  372.     }
  373.  
  374.     void nextChar() {
  375.        try {
  376.           c = (char)in.read();
  377.        } catch (IOException e) {
  378.             throw new Error(e);
  379.        }
  380.     }
  381.  
  382.     long nextLong() {
  383.        while (true) {
  384.           if ('0' <= c && c <= '9' || c == '-') {
  385.              break;
  386.           }
  387.           asserT(c != -1);
  388.           nextChar();
  389.        }
  390.        long sign=1;
  391.        if(c == '-'){
  392.            sign=-1;
  393.            nextChar();
  394.        }
  395.        long value = c - '0';
  396.        nextChar();
  397.        while ('0' <= c && c <= '9') {
  398.           value *= 10;
  399.           value += c - '0';
  400.           nextChar();
  401.        }
  402.        value*=sign;
  403.        return value;
  404.     }
  405.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement