Cod sursa(job #1839217)

Utilizator Andrei1998Andrei Constantinescu Andrei1998 Data 2 ianuarie 2017 16:46:16
Problema Secv8 Scor 100
Compilator java Status done
Runda Arhiva de probleme Marime 4.97 kb
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Random;
import java.util.StringTokenizer;

public class Main {
	
	public static class Treap {
		public int val, pr;
		public boolean rev;
		int sz;
		
		public Treap left, right;
	
		Treap(int _val, int _pr, Treap _left, Treap _right) {
			val = _val;
			pr = _pr;
			rev = false;
			sz = 1;
			left = _left;
			right = _right;
		}
		
		public static Treap nil;
		
		void computeDp() {
			if (this != nil)
				sz = 1 + left.sz + right.sz;
		}
		
		public void makeLazy() {
			if (this != nil) {
				if (rev == false)
					rev = true;
				else
					rev = false;
				
				Treap aux = left;
				left = right;
				right = aux;
			}
		}
		
		public void propagate() {
			if (rev == true) {
				left.makeLazy();
				right.makeLazy();
				rev = false;
			}
		}
	}
	
	public static Treap root;
	
	public static class PairT {
		public Treap l, r;
	
		PairT(Treap _l, Treap _r) {
			l = _l;
			r = _r;
		}
	}
	
	static PairT split(Treap t, int k) {
		if (t == Treap.nil)
			return new PairT(t, t);
		t.propagate();
		
		PairT aux;
		if (k <= t.left.sz) {
			aux = split(t.left, k);
			t.left = aux.r;
			aux.r = t;
		}
		else {
			aux = split(t.right, k - t.left.sz - 1);
			t.right = aux.l;
			aux.l = t;
		}

		t.computeDp();
		return aux;
	}
	
	static Treap join(Treap l, Treap r) {
		if (l == Treap.nil)
			return r;
		if (r == Treap.nil)
			return l;
		
		if (l.pr > r.pr) {
			l.propagate();
			l.right = join(l.right, r);
			l.computeDp();
			return l;
		}
		else {
			r.propagate();
			r.left = join(l, r.left);
			r.computeDp();
			return r;
		}
	}

	static int access(Treap t, int k) {
		if (k == 1 + t.left.sz)
			return t.val;
		t.propagate();
		if (k <= t.left.sz)
			return access(t.left, k);
		else
			return access(t.right, k - t.left.sz - 1);
	}
	
	static Treap insert(Treap t, int k, int val, int pr) {
		t.propagate();
		if (pr > t.pr) {
			PairT aux = split(t, k - 1);
			t = new Treap(val, pr, aux.l, aux.r);
			t.computeDp();
			return t;
		}
		else if (k <= t.left.sz + 1)
			t.left = insert(t.left, k, val, pr);
		else
			t.right = insert(t.right, k - t.left.sz - 1, val, pr);
		
		t.computeDp();
		return t;
	}
	
	static Treap erase(Treap t, int k) {
		t.propagate();
		if (k == t.left.sz + 1)
			return join(t.left, t.right);
		else if (k <= t.left.sz)
			t.left = erase(t.left, k);
		else
			t.right = erase(t.right, k - t.left.sz - 1);
		
		t.computeDp();
		return t;
	}
	
	static Treap erase(Treap t, int l, int r) {
		int cnt = r - l + 1;
		while (cnt --> 0)
			t = erase(t, l);
		return t;
	}
	
	static Treap reverse(Treap t, int l, int r) {
		PairT aux1 = split(t, r);
		PairT aux2 = split(aux1.l, l - 1);
		
		aux2.r.makeLazy();
		
		aux1.l = join(aux2.l, aux2.r);
		t = join(aux1.l, aux1.r);
		
		return t;
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		MyScanner sc = new MyScanner(new FileInputStream("secv8.in"));
		PrintWriter out = new PrintWriter("secv8.out");
		
		Treap.nil = new Treap(-1, -1, null, null);
		Treap.nil.left = Treap.nil.right = Treap.nil;
		Treap.nil.sz = 0;
		root = Treap.nil;
		
		int m; String op;
		m = sc.nextInt(); op = sc.next();
		
		Random rand = new Random();
		
		while (m --> 0) {
			op = sc.next();
			if (op.equals("I")) {
				int k, e;
				k = sc.nextInt();
				e = sc.nextInt();
				root = insert(root, k, e, rand.nextInt((1 << 31) - 1));
			}
			else if (op.equals("A")) {
				int k;
				k = sc.nextInt();
				out.println(access(root, k));
			}
			else if (op.equals("R")) {
				int l, r;
				l = sc.nextInt();
				r = sc.nextInt();
				root = reverse(root, l, r);
			}
			else {
				int l, r;
				l = sc.nextInt();
				r = sc.nextInt();
				root = erase(root, l, r);
			}
		}
		
		for (int i = 1; i < root.sz; ++ i)
			out.print(access(root, i) + " ");
		out.println(access(root, root.sz));
		
		out.close();
	}
	
	public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;
	 
	      public MyScanner(FileInputStream fis) {
	         br = new BufferedReader(new InputStreamReader(fis));
	      }
	 
	      String next() {
	          while (st == null || !st.hasMoreElements()) {
	              try {
	                  st = new StringTokenizer(br.readLine());
	              }
	              catch (IOException e) {
	                  e.printStackTrace();
	              }
	          }
	          return st.nextToken();
	      }
	 
	      int nextInt() {
	          return Integer.parseInt(next());
	      }
	 
	      String nextLine(){
	          String str = "";
		      try {
		    	  str = br.readLine();
			  } catch (IOException e) {
			     e.printStackTrace();
			  }
		      return str;
	      }
	}
}