Cod sursa(job #1838816)

Utilizator geniucosOncescu Costin geniucos Data 1 ianuarie 2017 20:34:28
Problema Secv8 Scor 100
Compilator java Status done
Runda Arhiva de probleme Marime 5.07 kb
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;

public class Main {
	static class node {
		public int size, key, priority;
		public boolean rev;
		public node l, r;
		public node(int key, int priority, int size, boolean rev, node l, node r) {
			super();
			this.key = key;
			this.priority = priority;
			this.size = size;
			this.rev = rev;
			this.l = l;
			this.r = r;
		}
		public void refresh () {
			this.size = l.size + r.size + 1;
		}
		public void split () {
			if (rev == false) return ;
			node aux = l;
			l = r;
			r = aux;
			l.rev ^= true;
			r.rev ^= true;
			rev = false;
		}
	}
	
	static class nodePair {
		public node first, second;
		public nodePair(node first, node second) {
			super();
			this.first = first;
			this.second = second;
		}
	}
	
	/*static Random rand0 = new Random ();
	static int[] toReturn = {7039603, 426805826, 146176864, 189461996};
	static class Random2 {
//		private int nr = 0;
		public int nextInt (int N) {
			int val = rand0.nextInt (N);
//			int val = toReturn[nr ++];
			System.out.println ("Rand generated " + val);
			return val;
		}
	}*/
	static Random rand = new Random ();
	static node nil = new node (0, 0, 0, false, null, null), root = nil;
	static int INF = 1 << 29;
	
	static node rot_left (node n) {
		node t = n.l;
		n.l = t.r;
		t.r = n;
		n.refresh ();
		t.refresh ();
		return t;
	}
	
	static node rot_right (node n) {
		node t = n.r;
		n.r = t.l;
		t.l = n;
		n.refresh ();
		t.refresh ();
		return t;
	}
	
	static node balance (node n) {
		n.split ();
		if (n.l.priority > n.priority) {
			n.l.split ();
			n = rot_left (n);
		}
		else
		if (n.r.priority > n.priority) {
			n.r.split ();
			n = rot_right (n);
		}
		return n;
	}
	
	static node insert (node n, int pos, int val, int priority) {
		if (n == nil) {
			n = new node (val, priority, 1, false, nil, nil);
			return n;
		}
		n.split ();
		if (pos <= n.l.size) n.l = insert (n.l, pos, val, priority);
		else n.r = insert (n.r, pos - n.l.size - 1, val, priority);
		n.refresh ();
		n = balance (n);
		return n;
	}
	
	static node erase (node n, int pos) {
		n.split ();
		if (pos <= n.l.size) n.l = erase (n.l, pos);
		else
		if (n.l.size + 1 < pos) n.r = erase (n.r, pos - n.l.size - 1);
		else {
			if (n.l == nil && n.r == nil) return nil;
			if (n.l.priority > n.r.priority) {
				n.l.split ();
				n = rot_left (n);
			}
			else {
				n.r.split ();
				n = rot_right (n);
			}
			n = erase (n, pos);
		}
		if (n != nil) n.refresh ();
		return n;
	}
	
	static int access (node n, int pos) {
		n.split ();
		if (pos == n.l.size + 1) return n.key;
		if (pos <= n.l.size) return access (n.l, pos);
		return access (n.r, pos - n.l.size - 1);
	}
	
	static node join (node l, node r) {
		node aux = new node (0, 0, l.size + r.size + 1, false, l, r);
		return erase (aux, l.size + 1);
	}
	
	static nodePair split (node n, int pos) {
		node aux = insert (n, pos, 0, INF);
		return new nodePair (aux.l, aux.r);
	}
	
	static PrintWriter out;
	static void print (node n) {
		if (n == nil) return ;
		n.split ();
		print (n.l);
		out.print (n.key + " ");
		print (n.r);
	}
	
	public static void main (String arguments[]) throws IOException{
		Scanner sc = new Scanner (new FileInputStream ("secv8.in"));
        out = new PrintWriter ("secv8.out");
        
        /*int N = sc.nextInt ();
        for (int i=1; i<=N; i++) {
        	int val = sc.nextInt ();
        	root = insert (root, 0, val, rand.nextInt (INF - 2) + 1);
        }
        print (root);
        for (int i=1; i<=N; i++) {
        	root = erase (root, 1);
        	System.out.println();
        	print (root);
        }*/
        
        int M = sc.nextInt (); sc.nextInt ();
        while (M > 0) {
        	//print (root);System.out.println ();
        	M --;
        	char type = sc.next().charAt(0);
        	//out.print(type + " ");
        	if (type == 'I') {
        		int pos = sc.nextInt(), val = sc.nextInt();
        		//out.print(pos + " " + val + '\n');
        		root = insert (root, pos - 1, val, rand.nextInt (INF - 2) + 1);
        		continue;
        	}
        	if (type == 'A') {
        		int pos = sc.nextInt ();
        		//out.print(" " + pos + '\n');
        		out.println(access (root, pos));
        		continue;
        	}
        	int st = sc.nextInt(), dr = sc.nextInt();
    		//out.print(st + " " + dr + '\n');
        	node aux, r1, r2, r3;
    		nodePair p1, p2;
    		p1 = split (root, dr);
    		r3 = p1.second;
    		p2 = split (p1.first, st - 1);
    		r1 = p2.first;
    		r2 = p2.second;
    		//print (r1);System.out.print("| ");print (r2);System.out.print ("| ");print (r3);System.out.println();
        	if (type == 'R') {
        		r2.rev ^= true;
        		aux = join (r1, r2);
        		root = join (aux, r3);
        	}
        	else root = join (r1, r3);
        }
        print (root);
        out.println();
        sc.close ();
        out.close ();
	}
}