Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.Random;
- import java.util.StringTokenizer;
- public class C {
- static PrintWriter writer;
- static void inorderTraversal(Node x) {
- if (x != null) {
- inorderTraversal(x.L);
- writer.print(x.x + " ");
- inorderTraversal(x.R);
- }
- }
- public static void main(String[] args) {
- try {
- Reader reader = new Reader("movetofront.in");
- writer = new PrintWriter("movetofront.out");
- Treap treap = new Treap();
- Random rnd = new Random();
- int[] a = new int[100010];
- for (int i = 0; i < 100010; i++) {
- a[i] = i;
- }
- for (int i = 0; i < 100010; i++) {
- int t = a[i];
- int k = rnd.nextInt(100010);
- a[i] = a[k];
- a[k] = t;
- }
- int n = reader.getInt();
- int m = reader.getInt();
- Node t = new Node(1, a[0]);
- for (int i = 1; i < n; i++) {
- t = treap.merge(t, new Node(i + 1, a[i]));
- }
- Node[] split1 = new Node[2];
- Node[] split2 = new Node[2];
- for (int i = 0; i < m; i++) {
- int l = reader.getInt();
- int r = reader.getInt() + 1;
- split1 = treap.split(t, l - 1);
- split2 = treap.split(split1[1], r - l);
- /*System.out.println("first part: " );
- inorderTraversal(split1[0]);
- System.out.println("second part: " );
- inorderTraversal(split2[0]);
- System.out.println("third part" );
- inorderTraversal(split2[1]);*/
- t = treap.merge(split2[0], split1[0]);
- t = treap.merge(t, split2[1]);
- //writer.println("");
- }
- inorderTraversal(t);
- writer.close();
- } catch (IOException e) {
- System.out.println(2);
- }
- }
- }
- class Treap {
- Node root;
- public Treap(Node root) {
- this.root = root;
- }
- public Treap() {
- this.root = null;
- }
- public Node merge(Node t1, Node t2) {
- if (t1 == null) {
- return t2;
- }
- if (t2 == null) {
- return t1;
- }
- Node t;
- if (t1.y < t2.y) {
- t = new Node(t2.x, t2.y, merge(t1, t2.L), t2.R);
- } else {
- t = new Node(t1.x, t1.y, t1.L, merge(t1.R, t2));
- }
- t.size = 1;
- if (t.L != null) {
- t.size += t.L.size;
- }
- if (t.R != null) {
- t.size += t.R.size;
- }
- return t;
- }
- private int size(Node t) {
- if (t == null) {
- return 0;
- }
- return t.size;
- }
- public Node[] split(Node t, int x) {
- Node[] splitting = new Node[2];
- if (t == null) {
- splitting[0] = splitting[1] = null;
- return splitting;
- }
- int index = size(t.L) + 1;
- if (index <= x) {
- splitting[0] = new Node();
- splitting[0].L = t.L;
- splitting[0].x = t.x;
- splitting[0].y = t.y;
- Node[] splitting2 = new Node[2];
- splitting2 = split(t.R, x - index);
- splitting[0].R = splitting2[0];
- splitting[1] = splitting2[1];
- if (splitting[0] != null) {
- splitting[0].size = size(splitting[0].L) + size(splitting[0].R) + 1;
- }
- if (splitting[1] != null) {
- splitting[1].size = size(splitting[1].L) + size(splitting[1].R) + 1;
- }
- } else {
- splitting[1] = new Node();
- splitting[1].R = t.R;
- //System.out.println(splitting[1].R.x);
- splitting[1].x = t.x;
- splitting[1].y = t.y;
- Node[] splitting2 = new Node[2];
- splitting2 = split(t.L, x);
- splitting[1].L = splitting2[1];
- splitting[0] = splitting2[0];
- if (splitting[0] != null) {
- splitting[0].size = size(splitting[0].L) + size(splitting[0].R) + 1;
- }
- if (splitting[1] != null) {
- splitting[1].size = size(splitting[1].L) + size(splitting[1].R) + 1;
- }
- }
- return splitting;
- }
- }
- class Node {
- public Node L;
- public Node R;
- int x;
- int y;
- int c;
- int size = 1;
- public Node() {
- }
- public Node(int x, int y) {
- this.L = null;
- this.R = null;
- this.y = y;
- this.x = x;
- }
- public Node(int x, int y, Node L, Node R) {
- this.x = x;
- this.y = y;
- this.L = L;
- this.R = R;
- }
- }
- class Reader {
- BufferedReader bReader;
- StringTokenizer st;
- public Reader(InputStream iStream) throws IOException {
- bReader = new BufferedReader(new InputStreamReader(iStream));
- st = new StringTokenizer(bReader.readLine());
- }
- public Reader(String name) throws IOException {
- this(new FileInputStream(name));
- }
- public String getToken() throws IOException {
- while (!st.hasMoreElements()) {
- String m = bReader.readLine();
- if (m != null) {
- st = new StringTokenizer(m);
- } else {
- return null;
- }
- }
- return st.nextToken();
- }
- public int getInt() throws IOException {
- return Integer.parseInt(getToken());
- }
- public char getChar() throws IOException {
- return getToken().charAt(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement