Guest User

Untitled

a guest
Jul 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.util.TreeMap;
  7.  
  8. public class quack implements Runnable {
  9. BufferedReader in;
  10. PrintWriter out;
  11. int[] queueArr = new int[10];
  12. int[] queueNew;
  13. int queueSize = 10;
  14. int queueLSize = 10;
  15. int queueRSize = 0;
  16. int queueH = 9;
  17. int queueT = 9;
  18. int mod = 65536;
  19. int[] regs = new int[26];
  20. String[] commands = new String[100000];
  21. int pos = 0;
  22. int n = 0;
  23. TreeMap<String, Integer> labels = new TreeMap<String, Integer>();
  24.  
  25. public static void main(String[] args) {
  26. new Thread(new quack()).run();
  27. }
  28.  
  29. public void run() {
  30. try {
  31. in = new BufferedReader(new FileReader("input.txt"));
  32. out = new PrintWriter(new FileWriter("output.txt"));
  33. solve();
  34. //System.out.println(Integer.MIN_VALUE);
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. } finally {
  38. out.flush();
  39. out.close();
  40. }
  41. }
  42.  
  43. void put(int num) {
  44. if (queueLSize == 0) {
  45. queueSize = (queueH - queueT) * 2;
  46. queueNew = new int[queueSize];
  47. for (int i = queueH; i > queueT; i--) {
  48. queueNew[queueSize - (queueH - i) - 1] = queueArr[i];
  49. }
  50. queueLSize = queueSize / 2;
  51. queueRSize = 0;
  52. queueH = queueSize - 1;
  53. queueT = queueSize / 2 - 1;
  54. queueArr = queueNew;
  55. }
  56. queueArr[queueT--] = ((mod + num) % mod);
  57. queueLSize--;
  58. }
  59.  
  60. int get() {
  61. if (queueRSize >= queueSize / 2) {
  62. queueSize = (queueH - queueT) * 2;
  63. queueNew = new int[queueSize];
  64. for (int i = queueH; i > queueT; i--) {
  65. queueNew[queueSize - (queueH - i) - 1] = queueArr[i];
  66. }
  67. queueLSize = queueSize / 2;
  68. queueRSize = 0;
  69. queueH = queueSize - 1;
  70. queueT = queueSize / 2 - 1;
  71. queueArr = queueNew;
  72. }
  73. queueRSize++;
  74. return ((mod + queueArr[queueH--]) % mod);
  75. }
  76.  
  77. void add() {
  78. put(get() + get());
  79. }
  80.  
  81. void substract() {
  82. put(get() - get());
  83. }
  84.  
  85. void multiply() {
  86. put((int)(((long)get() * (long)get()) % (long)mod));
  87. }
  88.  
  89. void divide() {
  90. int x = get(), y = get();
  91. put((y == 0) ? 0 : (x / y));
  92. }
  93.  
  94. void modulo() {
  95. int x = get(), y = get();
  96. put((y == 0) ? 0 : (x % y));
  97. }
  98.  
  99. void setReg(char id) {
  100. regs[id - 'a'] = get();
  101. }
  102.  
  103. void getReg(char id) {
  104. put(regs[id - 'a']);
  105. }
  106.  
  107. void P() {
  108. out.print(Integer.toString(get()) + (char)10);
  109. }
  110.  
  111. void P(char id) {
  112. out.print(Integer.toString(regs[id - 'a']) + (char)10);
  113. }
  114.  
  115. void C() {
  116. out.print((char) (get() % 256));
  117. }
  118.  
  119. void C(char id) {
  120. out.print((char) (regs[id - 'a'] % 256));
  121. }
  122.  
  123. void label(String id) {
  124. labels.put(id, n - 1);
  125. }
  126.  
  127. void J(String id) {
  128. pos = labels.get(id);
  129. }
  130.  
  131. void Z(char regId, String labelId) {
  132. if (regs[regId - 'a'] == 0)
  133. J(labelId);
  134. }
  135.  
  136. void E(char regId1, char regId2, String labelId) {
  137. if (regs[regId1 - 'a'] == regs[regId2 - 'a'])
  138. J(labelId);
  139. }
  140.  
  141. void G(char regId1, char regId2, String labelId) {
  142. if (regs[regId1 - 'a'] > regs[regId2 - 'a'])
  143. J(labelId);
  144. }
  145.  
  146. void Q() {
  147. pos = n + 1;
  148. }
  149.  
  150. void solve() throws NumberFormatException, IOException {
  151. String command = in.readLine();
  152. while (command != null) {
  153. commands[n++] = command;
  154. if (command.charAt(0) == ':')
  155. label(command.substring(1));
  156. command = in.readLine();
  157. }
  158. for (pos = 0; pos < n; pos++) {
  159. char c = commands[pos].charAt(0);
  160. switch (c) {
  161. case '+':
  162. add();
  163. break;
  164. case '-':
  165. substract();
  166. break;
  167. case '*':
  168. multiply();
  169. break;
  170. case '/':
  171. divide();
  172. break;
  173. case '%':
  174. modulo();
  175. break;
  176. case '>':
  177. setReg(commands[pos].charAt(1));
  178. break;
  179. case '<':
  180. getReg(commands[pos].charAt(1));
  181. break;
  182. case 'P': {
  183. if (commands[pos].length() == 1)
  184. P();
  185. else
  186. P(commands[pos].charAt(1));
  187. break;}
  188. case 'C': {
  189. if (commands[pos].length() == 1)
  190. C();
  191. else
  192. C(commands[pos].charAt(1));
  193. break;}
  194. case ':':
  195. break;
  196. case 'J':
  197. J(commands[pos].substring(1));
  198. break;
  199. case 'Z':
  200. Z(commands[pos].charAt(1), commands[pos].substring(2));
  201. break;
  202. case 'E':
  203. E(commands[pos].charAt(1), commands[pos].charAt(2),
  204. commands[pos].substring(3));
  205. break;
  206. case 'G':
  207. G(commands[pos].charAt(1), commands[pos].charAt(2),
  208. commands[pos].substring(3));
  209. break;
  210. case 'Q':
  211. Q();
  212. break;
  213. default:
  214. put(Integer.parseInt(commands[pos]));
  215. break;
  216. }
  217. }
  218. }
  219. }
Add Comment
Please, Sign In to add comment