记录编号 422339 评测结果 AAAAAAAAAA
题目名称 [HNOI 2004] 宠物收养所 最终得分 100
用户昵称 GravatarHeHe 是否通过 通过
代码语言 C++ 运行时间 0.069 s
提交时间 2017-07-09 16:38:32 内存使用 0.82 MiB
显示代码纯文本
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. namespace IO{
  7. inline char getc(void);
  8. inline int in(void);
  9. inline void write(int x);
  10. inline void write_all(void);
  11.  
  12. char ops[1 << 18], *opt = ops, *const opt_end = ops + (1 << 18);
  13.  
  14. inline char getc(void) {
  15. static char buf[1 << 18], *fs, *ft;
  16. return (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 18, stdin)), fs == ft) ? EOF : *fs++;
  17. }
  18.  
  19. inline int in(void) {
  20. register char tmp = getc();
  21. register int res = 0;
  22. register bool f = true;
  23.  
  24. while(!isdigit(tmp) && tmp ^ '-') tmp = getc();
  25. if(tmp == '-') f = false, tmp = getc();
  26. while(isdigit(tmp))
  27. res = ((res + (res << 2)) << 1) + (tmp ^ 48),
  28. tmp = getc();
  29. if(f) return res;
  30. return ~res + 1;
  31. }
  32.  
  33. inline void write(int x) {
  34. static char *p = new char[20]();
  35. if(x & 0x80000000) {
  36. *(opt++) = '-';
  37. if(opt == opt_end) write_all();
  38. x = -x;
  39. }
  40.  
  41. do{
  42. *(++p) = (x % 10) | 0x30;
  43. x /= 10;
  44. }while(x);
  45.  
  46. while(*p) {
  47. *(opt++) = *(p--);
  48. if(opt == opt_end) write_all();
  49. }
  50.  
  51. *(opt++) = ' ';
  52. if(opt == opt_end) write_all();
  53. return ;
  54. }
  55.  
  56. inline void write_all(void) {
  57. fwrite(ops, 1, opt - ops, stdout);
  58. opt = ops;
  59. return ;
  60. }
  61. }
  62. using namespace IO;
  63.  
  64. const int INF = 0x6fffffff;
  65. const int MOD = 1e6;
  66.  
  67. struct NODE{
  68. int s;
  69. int height, size;
  70. NODE *ls, *rs;
  71.  
  72. NODE() { ;}
  73. NODE(int x) {
  74. s = x;
  75. height = size = 1;
  76. ls = rs = NULL;
  77. }
  78.  
  79. int hgt(void) {
  80. if(this) return height;
  81. return 0;
  82. }
  83.  
  84. int siz(void) {
  85. if(this) return size;
  86. return 0;
  87. }
  88.  
  89. void update(void) {
  90. height = max(ls->hgt(), rs->hgt()) + 1;
  91. size = ls->siz() + rs->siz() + 1;
  92. }
  93. };
  94.  
  95. inline int Abs(int x);
  96. inline void Left(NODE *&k2);
  97. inline void Right(NODE *&k2);
  98. void Insert(NODE *&node, int x);
  99. void Delete(NODE *&node, int x);
  100. inline int lower_bound(NODE *node, int x);
  101. inline int upper_bound(NODE *node, int x);
  102.  
  103. NODE *root0, *root1;
  104. int N, arg, temp1, temp2, ANS;
  105.  
  106. int main() {
  107. #ifndef LOCAL
  108. freopen("pet.in", "r", stdin);
  109. freopen("pet.out", "w", stdout);
  110. #endif
  111. N = in();
  112.  
  113. for(int i = 1; i <= N; ++i) {
  114. if(in()) {
  115. arg = in();
  116. if(root0) {
  117. temp1 = lower_bound(root0, arg);
  118. temp2 = upper_bound(root0, arg);
  119. if(arg - temp1 <= temp2 - arg) {
  120. Delete(root0, temp1);
  121. (ANS += arg - temp1) %= MOD;
  122. } else {
  123. Delete(root0, temp2);
  124. (ANS += temp2 - arg) %= MOD;
  125. }
  126. } else Insert(root1, arg);
  127. } else {
  128. arg = in();
  129. if(root1) {
  130. temp1 = lower_bound(root1, arg);
  131. temp2 = upper_bound(root1, arg);
  132. if(arg - temp1 <= temp2 - arg) {
  133. Delete(root1, temp1);
  134. (ANS += arg - temp1) %= MOD;
  135. } else {
  136. Delete(root1, temp2);
  137. (ANS += temp2 - arg) %= MOD;
  138. }
  139. } else Insert(root0, arg);
  140. }
  141. }
  142.  
  143. printf("%d", ANS);
  144. return 0;
  145. }
  146.  
  147. inline void Left(NODE *&k2) {
  148. register NODE *k1 = k2->ls;
  149. k2->ls = k1->rs;
  150. k1->rs = k2;
  151.  
  152. k2->update(); k1->update();
  153. k2 = k1;
  154. return ;
  155. }
  156.  
  157. inline void Right(NODE *&k2) {
  158. register NODE *k1 = k2->rs;
  159. k2->rs = k1->ls;
  160. k1->ls = k2;
  161.  
  162. k2->update(); k1->update();
  163. k2 = k1;
  164. return ;
  165. }
  166.  
  167. inline int Abs(int x) {
  168. if(x & 0x80000000) return ~x + 1;
  169. return x;
  170. }
  171.  
  172. void Insert(NODE *&node, int x) {
  173. if(node) {
  174. if(x < node->s) {
  175. Insert(node->ls, x);
  176. if(node->ls->hgt() - node->rs->hgt() == 2) {
  177. if(node->ls->rs->hgt() > node->ls->ls->hgt()) Right(node->ls);
  178. Left(node);
  179. } else node->update();
  180. } else {
  181. Insert(node->rs, x);
  182. if(node->rs->hgt() - node->ls->hgt() == 2) {
  183. if(node->rs->ls->hgt() > node->rs->rs->hgt()) Left(node->rs);
  184. Right(node);
  185. } else node->update();
  186. }
  187. } else node = new NODE(x);
  188. return ;
  189. }
  190.  
  191. void Delete(NODE *&node, int x) {
  192. if(x ^ node->s) {
  193. if(x < node->s) {
  194. Delete(node->ls, x);
  195. if(node->rs->hgt() - node->ls->hgt() == 2) {
  196. if(node->rs->ls->hgt() > node->rs->rs->hgt()) Left(node->rs);
  197. Right(node);
  198. } else node->update();
  199. } else {
  200. Delete(node->rs, x);
  201. if(node->ls->hgt() - node->rs->hgt() == 2) {
  202. if(node->ls->rs->hgt() > node->ls->ls->hgt()) Right(node->ls);
  203. Left(node);
  204. } else node->update();
  205. }
  206. } else {
  207. if(node->ls && node->rs) {
  208. register NODE *temp = node->rs;
  209. while(temp->ls) temp = temp->ls;
  210. node->s = temp->s;
  211. Delete(node->rs, node->s);
  212. if(node->ls->hgt() - node->rs->hgt() == 2) {
  213. if(node->ls->rs->hgt() > node->ls->ls->hgt()) Right(node->ls);
  214. Left(node);
  215. } else node->update();
  216. } else {
  217. register NODE *temp = node;
  218. if(node->ls) node = node->ls;
  219. else node = node->rs;
  220. delete temp;
  221. }
  222. }
  223. return ;
  224. }
  225.  
  226. inline int lower_bound(NODE *node, int x) {
  227. register int res = -INF;
  228.  
  229. while(node) {
  230. if(x == node->s) return x;
  231. else if(x < node->s) node = node->ls;
  232. else {
  233. res = node->s;
  234. node = node->rs;
  235. }
  236. }
  237.  
  238. return res;
  239. }
  240.  
  241. inline int upper_bound(NODE *node, int x) {
  242. register int res = INF;
  243.  
  244. while(node) {
  245. if(x == node->s) return x;
  246. else if(x > node->s) node = node->rs;
  247. else {
  248. res = node->s;
  249. node = node->ls;
  250. }
  251. }
  252.  
  253. return res;
  254. }