Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <iomanip>
  5. using namespace std;
  6. #define VERSION "0.1.5"
  7.  
  8. #include <cassert>
  9. #include <algorithm>
  10.  
  11. /** Fast allocation */
  12.  
  13. #ifdef FAST_ALLOCATOR_MEMORY
  14. int allocator_pos = 0;
  15. char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
  16. inline void * operator new ( size_t n ) {
  17. char *res = allocator_memory + allocator_pos;
  18. allocator_pos += n;
  19. assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  20. return (void *)res;
  21. }
  22. inline void operator delete ( void * ) noexcept { }
  23. //inline void * operator new [] ( size_t ) { assert(0); }
  24. //inline void operator delete [] ( void * ) { assert(0); }
  25. #endif
  26.  
  27. /** Fast input-output */
  28.  
  29. template <class T = int> inline T readInt();
  30. inline double readDouble();
  31. inline int readUInt();
  32. inline int readChar(); // first non-blank character
  33. inline void readWord( char *s );
  34. inline bool readLine( char *s ); // do not save '\n'
  35. inline bool isEof();
  36. inline int getChar();
  37. inline int peekChar();
  38. inline bool seekEof();
  39. inline void skipBlanks();
  40.  
  41. template <class T> inline void writeInt( T x, char end = 0, int len = -1 );
  42. inline void writeChar( int x );
  43. inline void writeWord( const char *s );
  44. inline void writeDouble( double x, int len = 0 ); // works correct only for |x| < 2^{63}
  45. inline void flush();
  46.  
  47. static struct buffer_flusher_t {
  48. ~buffer_flusher_t() {
  49. flush();
  50. }
  51. } buffer_flusher;
  52.  
  53. /** Read */
  54.  
  55. static const int buf_size = 4096;
  56.  
  57. static unsigned char buf[buf_size];
  58. static int buf_len = 0, buf_pos = 0;
  59.  
  60. inline bool isEof() {
  61. if (buf_pos == buf_len) {
  62. buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
  63. if (buf_pos == buf_len)
  64. return 1;
  65. }
  66. return 0;
  67. }
  68.  
  69. inline int getChar() {
  70. return isEof() ? -1 : buf[buf_pos++];
  71. }
  72.  
  73. inline int peekChar() {
  74. return isEof() ? -1 : buf[buf_pos];
  75. }
  76.  
  77. inline bool seekEof() {
  78. int c;
  79. while ((c = peekChar()) != -1 && c <= 32)
  80. buf_pos++;
  81. return c == -1;
  82. }
  83.  
  84. inline void skipBlanks() {
  85. while (!isEof() && buf[buf_pos] <= 32U)
  86. buf_pos++;
  87. }
  88.  
  89. inline int readChar() {
  90. int c = getChar();
  91. while (c != -1 && c <= 32)
  92. c = getChar();
  93. return c;
  94. }
  95.  
  96. inline int readUInt() {
  97. int c = readChar(), x = 0;
  98. while ('0' <= c && c <= '9')
  99. x = x * 10 + c - '0', c = getChar();
  100. return x;
  101. }
  102.  
  103. template <class T>
  104. inline T readInt() {
  105. int s = 1, c = readChar();
  106. T x = 0;
  107. if (c == '-')
  108. s = -1, c = getChar();
  109. else if (c == '+')
  110. c = getChar();
  111. while ('0' <= c && c <= '9')
  112. x = x * 10 + c - '0', c = getChar();
  113. return s == 1 ? x : -x;
  114. }
  115.  
  116. inline double readDouble() {
  117. int s = 1, c = readChar();
  118. double x = 0;
  119. if (c == '-')
  120. s = -1, c = getChar();
  121. while ('0' <= c && c <= '9')
  122. x = x * 10 + c - '0', c = getChar();
  123. if (c == '.') {
  124. c = getChar();
  125. double coef = 1;
  126. while ('0' <= c && c <= '9')
  127. x += (c - '0') * (coef *= 1e-1), c = getChar();
  128. }
  129. return s == 1 ? x : -x;
  130. }
  131.  
  132. inline void readWord( char *s ) {
  133. int c = readChar();
  134. while (c > 32)
  135. *s++ = c, c = getChar();
  136. *s = 0;
  137. }
  138.  
  139. inline bool readLine( char *s ) {
  140. int c = getChar();
  141. while (c != '\n' && c != -1)
  142. *s++ = c, c = getChar();
  143. *s = 0;
  144. return c != -1;
  145. }
  146.  
  147. /** Write */
  148.  
  149. static int write_buf_pos = 0;
  150. static char write_buf[buf_size];
  151.  
  152. inline void writeChar( int x ) {
  153. if (write_buf_pos == buf_size)
  154. fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  155. write_buf[write_buf_pos++] = x;
  156. }
  157.  
  158. inline void flush() {
  159. if (write_buf_pos) {
  160. fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
  161. fflush(stdout);
  162. }
  163. }
  164.  
  165. template <class T>
  166. inline void writeInt( T x, char end, int output_len ) {
  167. if (x < 0)
  168. writeChar('-'), x = -x;
  169.  
  170. char s[24];
  171. int n = 0;
  172. while (x || !n)
  173. s[n++] = '0' + x % 10, x /= 10;
  174. while (n < output_len)
  175. s[n++] = '0';
  176. while (n--)
  177. writeChar(s[n]);
  178. if (end)
  179. writeChar(end);
  180. }
  181.  
  182. inline void writeWord( const char *s ) {
  183. while (*s)
  184. writeChar(*s++);
  185. }
  186.  
  187. inline void writeDouble( double x, int output_len ) {
  188. if (x < 0)
  189. writeChar('-'), x = -x;
  190. assert(x <= (1LLU << 63) - 1);
  191. long long t = (long long)x;
  192. writeInt(t), x -= t;
  193. writeChar('.');
  194. for (int i = output_len - 1; i > 0; i--) {
  195. x *= 10;
  196. t = std::min(9, (int)x);
  197. writeChar('0' + t), x -= t;
  198. }
  199. x *= 10;
  200. t = std::min(9, (int)(x + 0.5));
  201. writeChar('0' + t);
  202. }
  203.  
  204. #include <bits/stdc++.h>
  205.  
  206. const int maxN = 1e5;
  207.  
  208. struct Heap {
  209. int n, h[maxN + 1];
  210. void add( int x ) {
  211. h[++n] = x;
  212. for (int i = n; i > 1 && h[i >> 1] > h[i]; i >>= 1)
  213. swap(h[i], h[i >> 1]);
  214. }
  215. int down( int v ) {
  216. while (1) {
  217. int l = 2 * v;
  218. if (l < n && h[l + 1] < h[l]) l++;
  219. if (l > n || h[l] >= h[v]) break;
  220. swap(h[l], h[v]), v = l;
  221. }
  222. return v;
  223. }
  224. int pop() {
  225. swap(h[1], h[n--]);
  226. down(1);
  227. return h[n + 1];
  228. }
  229. int min() { return n ? h[1] : INT_MAX; }
  230. };
  231.  
  232. Heap mi, mi_del;
  233. Heap ma, ma_del;
  234. #define SKIP(a, b) while (b.min() == a.min()) a.pop(), b.pop();
  235.  
  236. int main() {
  237. char s[99];
  238. readLine(s);
  239. while (!isEof()) {
  240. readLine(s);
  241. if (s[0] == 'I') {
  242. int x = atoi(s + 7);
  243. mi.add(x);
  244. ma.add(-x);
  245. } else if (s[4] == 'i') {
  246. SKIP(mi, mi_del);
  247. int x = mi.pop();
  248. writeInt(x, '\n');
  249. ma_del.add(-x);
  250. } else {
  251. SKIP(ma, ma_del);
  252. int x = -ma.pop();
  253. writeInt(x, '\n');
  254. mi_del.add(x);
  255. }
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement