Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.util.Random;
  8. import java.util.StringTokenizer;
  9.  
  10. public class C {
  11.  
  12. static PrintWriter writer;
  13.  
  14. static void inorderTraversal(Node x) {
  15. if (x != null) {
  16. inorderTraversal(x.L);
  17. writer.print(x.x + " ");
  18. inorderTraversal(x.R);
  19.  
  20. }
  21. }
  22.  
  23. public static void main(String[] args) {
  24. try {
  25. Reader reader = new Reader("movetofront.in");
  26.  
  27. writer = new PrintWriter("movetofront.out");
  28. Treap treap = new Treap();
  29. Random rnd = new Random();
  30. int[] a = new int[100010];
  31. for (int i = 0; i < 100010; i++) {
  32. a[i] = i;
  33. }
  34. for (int i = 0; i < 100010; i++) {
  35. int t = a[i];
  36. int k = rnd.nextInt(100010);
  37. a[i] = a[k];
  38. a[k] = t;
  39. }
  40. int n = reader.getInt();
  41. int m = reader.getInt();
  42. Node t = new Node(1, a[0]);
  43. for (int i = 1; i < n; i++) {
  44. t = treap.merge(t, new Node(i + 1, a[i]));
  45.  
  46.  
  47. }
  48.  
  49. Node[] split1 = new Node[2];
  50. Node[] split2 = new Node[2];
  51.  
  52.  
  53. for (int i = 0; i < m; i++) {
  54. int l = reader.getInt();
  55. int r = reader.getInt() + 1;
  56. split1 = treap.split(t, l - 1);
  57. split2 = treap.split(split1[1], r - l);
  58.  
  59. /*System.out.println("first part: " );
  60. inorderTraversal(split1[0]);
  61. System.out.println("second part: " );
  62. inorderTraversal(split2[0]);
  63. System.out.println("third part" );
  64. inorderTraversal(split2[1]);*/
  65. t = treap.merge(split2[0], split1[0]);
  66. t = treap.merge(t, split2[1]);
  67.  
  68. //writer.println("");
  69. }
  70.  
  71. inorderTraversal(t);
  72. writer.close();
  73. } catch (IOException e) {
  74. System.out.println(2);
  75. }
  76.  
  77. }
  78.  
  79. }
  80.  
  81. class Treap {
  82. Node root;
  83.  
  84. public Treap(Node root) {
  85. this.root = root;
  86. }
  87.  
  88. public Treap() {
  89. this.root = null;
  90. }
  91.  
  92. public Node merge(Node t1, Node t2) {
  93. if (t1 == null) {
  94. return t2;
  95. }
  96. if (t2 == null) {
  97. return t1;
  98. }
  99. Node t;
  100. if (t1.y < t2.y) {
  101. t = new Node(t2.x, t2.y, merge(t1, t2.L), t2.R);
  102.  
  103. } else {
  104. t = new Node(t1.x, t1.y, t1.L, merge(t1.R, t2));
  105. }
  106.  
  107. t.size = 1;
  108. if (t.L != null) {
  109. t.size += t.L.size;
  110. }
  111. if (t.R != null) {
  112. t.size += t.R.size;
  113. }
  114.  
  115. return t;
  116.  
  117. }
  118.  
  119. private int size(Node t) {
  120. if (t == null) {
  121. return 0;
  122. }
  123. return t.size;
  124. }
  125.  
  126. public Node[] split(Node t, int x) {
  127. Node[] splitting = new Node[2];
  128.  
  129.  
  130.  
  131. if (t == null) {
  132. splitting[0] = splitting[1] = null;
  133. return splitting;
  134. }
  135. int index = size(t.L) + 1;
  136. if (index <= x) {
  137.  
  138. splitting[0] = new Node();
  139. splitting[0].L = t.L;
  140.  
  141. splitting[0].x = t.x;
  142. splitting[0].y = t.y;
  143. Node[] splitting2 = new Node[2];
  144. splitting2 = split(t.R, x - index);
  145. splitting[0].R = splitting2[0];
  146. splitting[1] = splitting2[1];
  147. if (splitting[0] != null) {
  148. splitting[0].size = size(splitting[0].L) + size(splitting[0].R) + 1;
  149. }
  150. if (splitting[1] != null) {
  151. splitting[1].size = size(splitting[1].L) + size(splitting[1].R) + 1;
  152. }
  153. } else {
  154.  
  155. splitting[1] = new Node();
  156. splitting[1].R = t.R;
  157. //System.out.println(splitting[1].R.x);
  158. splitting[1].x = t.x;
  159. splitting[1].y = t.y;
  160. Node[] splitting2 = new Node[2];
  161. splitting2 = split(t.L, x);
  162. splitting[1].L = splitting2[1];
  163. splitting[0] = splitting2[0];
  164. if (splitting[0] != null) {
  165. splitting[0].size = size(splitting[0].L) + size(splitting[0].R) + 1;
  166. }
  167. if (splitting[1] != null) {
  168. splitting[1].size = size(splitting[1].L) + size(splitting[1].R) + 1;
  169. }
  170. }
  171.  
  172. return splitting;
  173.  
  174. }
  175.  
  176. }
  177.  
  178. class Node {
  179. public Node L;
  180. public Node R;
  181. int x;
  182. int y;
  183. int c;
  184. int size = 1;
  185.  
  186. public Node() {
  187. }
  188.  
  189. public Node(int x, int y) {
  190. this.L = null;
  191. this.R = null;
  192. this.y = y;
  193. this.x = x;
  194. }
  195.  
  196. public Node(int x, int y, Node L, Node R) {
  197. this.x = x;
  198. this.y = y;
  199. this.L = L;
  200. this.R = R;
  201. }
  202. }
  203.  
  204. class Reader {
  205. BufferedReader bReader;
  206. StringTokenizer st;
  207.  
  208. public Reader(InputStream iStream) throws IOException {
  209.  
  210. bReader = new BufferedReader(new InputStreamReader(iStream));
  211. st = new StringTokenizer(bReader.readLine());
  212. }
  213.  
  214. public Reader(String name) throws IOException {
  215. this(new FileInputStream(name));
  216. }
  217.  
  218. public String getToken() throws IOException {
  219. while (!st.hasMoreElements()) {
  220. String m = bReader.readLine();
  221. if (m != null) {
  222. st = new StringTokenizer(m);
  223. } else {
  224. return null;
  225. }
  226. }
  227. return st.nextToken();
  228. }
  229.  
  230. public int getInt() throws IOException {
  231. return Integer.parseInt(getToken());
  232. }
  233.  
  234. public char getChar() throws IOException {
  235. return getToken().charAt(0);
  236. }
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement