Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- static Node root;
- static int currentValue = 0;
- static int size = 0;
- public static void main(String[] args) throws IOException {
- InputStreamReader reader = new InputStreamReader(System.in);
- BufferedReader in = new BufferedReader(reader);
- OutputStream out = new BufferedOutputStream(System.out);
- String line = in.readLine();
- while (in.ready()) {
- line = in.readLine();
- String[] keyWords = line.split(" ");
- long value = Long.parseLong(keyWords[1]);
- switch(keyWords[0]) {
- case "+1":
- insert(value);
- // update(root);
- break;
- case "1":
- insert(value);
- break;
- case "0":
- Node current = root;
- out.write(Integer.toString((int)findNumberMax(root, size - value + 1)).getBytes());
- out.flush();
- break;
- case "-1":
- delete(value);
- break;
- }
- Node current = root;
- }
- in.close();
- out.close();
- }
- public static class Node {
- long dataFirst;
- long dataSecond;
- long sum = 0;
- Node left = null;
- Node right = null;
- Node parent = null;
- public Node (long dataFirst, long dataSecond, Node parent) {
- this.dataFirst = dataFirst;
- this.dataSecond = dataSecond;
- this.parent = parent;
- }
- public Node (long dataFirst, Node parent) {
- this.dataFirst = dataFirst;
- this.dataSecond = currentValue;
- currentValue++;
- this.parent = parent;
- }
- }
- public static Pair split(Node root, long value) {
- Pair pair;
- if (root == null) {
- return new Pair(null, null);
- } else {
- if (root.dataFirst < value) {
- pair = split(root.right, value);
- root.right = pair.first;
- pair.first = root;
- } else {
- pair = split(root.left, value);
- root.left = pair.second;
- pair.second = root;
- }
- }
- update(pair.first);
- update(pair.second);
- return pair;
- }
- public static Node merge(Node first, Node second) {
- if (first == null || second == null) {
- return first == null ? second : first;
- } else {
- if (first.dataSecond > second.dataSecond) {
- first.right = merge(first.right, second);
- update(first);
- return first;
- } else {
- second.left = merge(first, second.left);
- update(second);
- return second;
- }
- }
- }
- public static void update(Node root) {
- if (root == null) {
- return;
- } else {
- root.dataSecond = 1 + getSum(root.left) + getSum(root.right);
- }
- }
- public static long getSum(Node current) {
- if (current == null) {
- return 0;
- } else {
- return current.dataSecond;
- }
- }
- public static class Pair {
- Node first;
- Node second;
- public Pair(Node first, Node second) {
- this.first = first;
- this.second = second;
- }
- }
- public static void insert(long data) {
- Node forInsert = new Node(data, null);
- if (exists(forInsert.dataFirst)) {
- return;
- } else {
- Pair pair;
- pair = split(root, data);
- pair.first = merge(pair.first, forInsert);
- root = merge(pair.first, pair.second);
- size++;
- }
- }
- public static boolean exists(long data) {
- boolean contains = false;
- Pair pair = split(root, data);
- Pair secondPair = split(pair.second, data + 1);
- if (secondPair.first != null) {
- contains = true;
- }
- Node kek = merge(pair.first, secondPair.first);
- root = merge(kek, secondPair.second);
- return contains;
- }
- public static long sum(long dataFirst, long dataSecond) {
- Pair first, second;
- first = split(root, dataFirst);
- second = split(first.second, dataSecond + 1);
- long answer = getSum(second.first);
- Node current = merge(second.first, second.second);
- root = merge(first.first, current);
- return answer;
- }
- public static void delete(long data) {
- Pair first, second;
- first = split(root, data);
- second = split(first.second, data + 1);
- root = merge(first.first, second.second);
- size--;
- }
- public static void next(long data) {
- Pair pair = split(root, data + 1);
- Node current = pair.second;
- if (current == null) {
- System.out.println("none");
- return;
- }
- while (current.left != null) {
- current = current.left;
- }
- root = merge(pair.first, pair.second);
- System.out.println(current.dataFirst);
- }
- public static void prev(long data) {
- Pair pair = split(root, data);
- Node current = pair.first;
- if (current == null) {
- System.out.println("none");
- return;
- }
- while (current.right != null) {
- current = current.right;
- }
- root = merge(pair.first, pair.second);
- System.out.println(current.dataFirst);
- }
- public static long findNumberMax(Node root, long data) {
- long currentNumber = 1;
- try {
- if (root.left != null) {
- currentNumber += root.left.dataSecond;
- }
- if (currentNumber == data) {
- return root.dataFirst;
- } else if (currentNumber < data) {
- return findNumberMax(root.right, data - currentNumber);
- } else {
- return findNumberMax(root.left, data);
- }
- } catch (NullPointerException e) {
- return root.dataFirst;
- }
- }
- public static void inOrderTraversal(Node root) {
- if (root != null) {
- inOrderTraversal(root.left);
- System.out.println(root.dataFirst + " " + root.dataSecond);
- inOrderTraversal(root.right);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement