记录编号 23240 评测结果 AAAAAAAAAA
题目名称 [HNOI 2004] 宠物收养所 最终得分 100
用户昵称 Gravatardonny 是否通过 通过
代码语言 Pascal 运行时间 0.417 s
提交时间 2011-03-04 14:00:32 内存使用 1.94 MiB
显示代码纯文本
  1. program pet;
  2. var
  3. t,head:longint;
  4. a,p,left,right,typ,ran:array[1..80000]of longint;
  5. n,i,s,r,max,m:longint;
  6.  
  7. procedure turnleft(const x:longint);
  8. var
  9. o,q,s:longint;
  10. begin
  11. o:=p[x];
  12. q:=left[right[x]];
  13. s:=right[x];
  14. if left[o]=x then left[o]:=s
  15. else right[o]:=s;
  16. p[s]:=o;
  17. if head=x then head:=s;
  18. left[s]:=x;
  19. p[x]:=s;
  20. right[x]:=q;
  21. p[q]:=x;
  22. end;
  23.  
  24. procedure turnright(const x:longint);
  25. var
  26. o,q,s:longint;
  27. begin
  28. o:=p[x];
  29. q:=left[x];
  30. s:=right[q];
  31. if left[o]=x then left[o]:=q
  32. else right[o]:=q;
  33. if head=x then head:=q;
  34. p[q]:=o;
  35. right[q]:=x;
  36. p[x]:=q;
  37. p[s]:=x;
  38. left[x]:=s;
  39. end;
  40.  
  41. function pre(const x:longint):longint;
  42. var
  43. o,q:longint;
  44. begin
  45. if left[x]<>0 then
  46. begin
  47. o:=left[x];
  48. q:=right[o];
  49. while q<>0 do
  50. begin
  51. o:=q;
  52. q:=right[o];
  53. end;
  54. exit(o);
  55. end
  56. else
  57. begin
  58. o:=p[x];
  59. q:=x;
  60. while (o<>0)and(left[o]=q) do
  61. begin
  62. q:=o;
  63. o:=p[q];
  64. end;
  65. exit(o);
  66. end;
  67. end;
  68.  
  69. function suc(const x:longint):longint;
  70. var
  71. o,q:longint;
  72. begin
  73. if right[x]<>0 then
  74. begin
  75. o:=right[x];
  76. q:=left[o];
  77. while q<>0 do
  78. begin
  79. o:=q;
  80. q:=left[o];
  81. end;
  82. exit(o);
  83. end
  84. else
  85. begin
  86. o:=p[x];
  87. q:=x;
  88. while (o<>0)and(right[o]=q) do
  89. begin
  90. q:=o;
  91. o:=p[q];
  92. end;
  93. exit(o);
  94. end;
  95. end;
  96.  
  97. procedure balanceup(const x:longint);
  98. begin
  99. while (ran[x]<ran[p[x]])and(p[x]<>0) do
  100. begin
  101. if left[p[x]]=x then turnright(p[x])
  102. else turnleft(p[x]);
  103. end;
  104. end;
  105.  
  106. procedure insert(const x,y:longint);
  107. var
  108. o,q:longint;
  109. begin
  110. inc(m);
  111. a[m]:=y;
  112. typ[m]:=x;
  113. ran[m]:=random(1000000);
  114. o:=head;
  115. if a[m]>a[o] then q:=right[o]
  116. else q:=left[o];
  117. while q<>0 do
  118. begin
  119. o:=q;
  120. if a[m]>a[o] then q:=right[o]
  121. else q:=left[o];
  122. end;
  123. p[m]:=o;
  124. if a[m]>a[o] then right[o]:=m
  125. else left[o]:=m;
  126. balanceup(m);
  127. end;
  128.  
  129. procedure delete(const x:longint);
  130. var
  131. o,q:longint;
  132. begin
  133. if (left[x]=0)or(right[x]=0) then
  134. begin
  135. if (left[x]=0)and(right[x]=0) then
  136. begin
  137. if head=x then
  138. begin
  139. t:=2;
  140. head:=0;
  141. end
  142. else
  143. begin
  144. o:=p[x];
  145. if left[o]=x then left[o]:=0
  146. else right[o]:=0;
  147. end;
  148. end
  149. else
  150. begin
  151. if left[x]<>0 then q:=left[x]
  152. else q:=right[x];
  153. if x=left[p[x]] then left[p[x]]:=q
  154. else right[p[x]]:=q;
  155. p[q]:=p[x];
  156. if head=x then head:=q;
  157. end;
  158. end
  159. else
  160. begin
  161. o:=ran[x] mod 2;
  162. if o=1 then q:=pre(x)
  163. else q:=suc(x);
  164. a[x]:=a[q];
  165. delete(q);
  166. end;
  167. end;
  168.  
  169. procedure search(const x,y:longint);
  170. var
  171. o,q,s,t:longint;
  172. begin
  173. o:=head;
  174. if y>a[o] then q:=right[o]
  175. else q:=left[o];
  176. while q<>0 do
  177. begin
  178. o:=q;
  179. if y>a[o] then q:=right[o]
  180. else q:=left[o];
  181. end;
  182. if (left[o]=0)and(right[o]=0)and(head=o) then
  183. begin
  184. max:=(max+abs(a[o]-y))mod 1000000;
  185. delete(o);
  186. exit;
  187. end;
  188. if y>a[o] then
  189. begin
  190. s:=suc(o);
  191. if s=0 then
  192. begin
  193. s:=pre(o);
  194. t:=o;
  195. o:=s;
  196. s:=t;
  197. end;
  198. end
  199. else
  200. begin
  201. s:=pre(o);
  202. if s=0 then s:=suc(o)
  203. else
  204. begin
  205. t:=o;
  206. o:=s;
  207. s:=t;
  208. end;
  209. end;
  210. t:=abs(y-a[o]);
  211. if abs(a[s]-y)<t then
  212. begin
  213. max:=(max+abs(a[s]-y)) mod 1000000;
  214. delete(s);
  215. end
  216. else
  217. begin
  218. max:=(max+t) mod 1000000;
  219. delete(o);
  220. end;
  221. end;
  222. procedure main(const x,y:longint);
  223. begin
  224. if t=2 then
  225. begin
  226. t:=x;
  227. inc(m);
  228. a[m]:=y;
  229. p[m]:=0;
  230. typ[m]:=x;
  231. ran[m]:=random(1000000);
  232. head:=m;
  233. end
  234. else
  235. begin
  236. if t=x then
  237. insert(x,y)
  238. else
  239. begin
  240. search(x,y);
  241. end;
  242. end;
  243. end;
  244.  
  245. begin
  246. assign(input,'pet.in');
  247. reset(input);
  248. assign(output,'pet.out');
  249. rewrite(output);
  250. readln(n);
  251. t:=2;
  252. head:=0;
  253. randomize;
  254. m:=0;
  255. max:=0;
  256. for i:=1 to n do
  257. begin
  258. readln(s,r);
  259. main(s,r);
  260. end;
  261. writeln(max);
  262. close(input);
  263. close(output);
  264. end.